SHARE
TWEET

Untitled

5ally Dec 4th, 2018 (edited) 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /* Define the custom box */
  3. add_action( 'add_meta_boxes', 'wpse_source_link' );
  4.  
  5. /* Do something with the data entered */
  6. add_action( 'save_post', 'wpse_source_link_save' );
  7.  
  8. /* Adds a box to the main column on the Post and Page edit screens */
  9. function wpse_source_link() {
  10.  
  11.     add_meta_box(
  12.         'source_link',
  13.         __( 'Source-link', 'myplugin_textdomain' ),
  14.         'wpse_source_meta_box',
  15.         'post',
  16.         'side'
  17.     );
  18. }
  19.  
  20. /* Prints the box content */
  21. function wpse_source_meta_box( $post ) {
  22.  
  23.   // Use nonce for verification
  24.   wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
  25.  
  26.   // The actual fields for data entry
  27.   echo '<label for="source-link">URL:</label> ';
  28.   echo '<input type="text" id="source-link"" name="source_link" value="'.
  29.     get_post_meta( $post->ID, '_source_link', true ) .'" size="25" />';
  30.  
  31.   echo '<label for="source-link-label">Label:</label> ';
  32.   echo '<input type="text" id="source-link-label"" name="source_link_label" value="'.
  33.     get_post_meta( $post->ID, '_source_link_label', true ) .'" size="25" />';
  34. }
  35.  
  36. /* When the post is saved, saves our custom data */
  37. function wpse_source_link_save( $post_id ) {
  38.   // verify if this is an auto save routine.
  39.   // If it is our form has not been submitted, so we dont want to do anything
  40.   if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  41.       return;
  42.  
  43.   // verify this came from the our screen and with proper authorization,
  44.   // because save_post can be triggered at other times
  45.  
  46.   if ( ! wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
  47.       return;
  48.  
  49.  
  50.   // Check permissions
  51.  
  52.   if ( current_user_can( 'edit_post', $post_id ) ) {
  53.  
  54.       update_post_meta( $post_id, '_source_link', sanitize_text_field( $_POST['source_link'] ) );
  55.       update_post_meta( $post_id, '_source_link_label', sanitize_text_field( $_POST['source_link_label'] ) );
  56.  
  57.    }
  58. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top