/*
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
/*
* Adds a box to the main column on the Post and Page edit screens.
*/
function jma_add_linkedin_input_box()
{
$screens = array('volunteer');
foreach ($screens as $screen) {
add_meta_box(
'jma_linkedin_input_section',
__('Linkedin address', 'jma_textdomain'),
'jma_linkedin_input_box',
$screen,
'normal',
'high'
);
}
}
add_action('add_meta_boxes', 'jma_add_linkedin_input_box');
/*
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function jma_linkedin_input_box($post)
{
// Add an nonce field so we can check for it later.
wp_nonce_field('jma_linkedin_input_box', 'jma_linkedin_input_box_nonce');
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$linkedin_values = array();
if (get_post_meta($post->ID, '_jma_linkedin_data_key', true)) {
$linkedin_values = get_post_meta($post->ID, '_jma_linkedin_data_key', true);
}
$linkedin_html = isset($linkedin_values['linkedin_text'])? $linkedin_values['linkedin_text']:'';
echo '<p></p>';
echo '<label for="linkedin_text">';
_e('linkedin address.', 'jma_textdomain');
echo '</label><br/>';
echo '<input name="linkedin_text" size="80" type="text" value="'.$linkedin_html.'"/>';
}
function jma_save_linkedin_postdata($post_id)
{
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times.
*/
// Check if our nonce is set.
if (!isset($_POST['jma_linkedin_input_box_nonce'])) {
return $post_id;
}
$nonce = $_POST['jma_linkedin_input_box_nonce'];
// Verify that the nonce is valid.
if (!wp_verify_nonce($nonce, 'jma_linkedin_input_box')) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// Check the user's permissions.
if ('page' === $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} else {
if (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
}
/* OK, its safe for us to save the data now. */
// Sanitize user input.
$jma_data['linkedin_text'] = sanitize_text_field($_POST['linkedin_text']);
// Update the meta field in the database.
update_post_meta($post_id, '_jma_linkedin_data_key', $jma_data);
}
add_action('save_post', 'jma_save_linkedin_postdata');