0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | /* * 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'); |