Advertisement
Guest User

Wordpress Meta saving problem

a guest
Nov 2nd, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.61 KB | None | 0 0
  1.  
  2.    /**
  3.  * Adds a box to the main column on the Post and Page edit screens.
  4.  */
  5. function labishop_add_meta_box() {
  6.         $screens = array( 'post', 'page' );
  7.         foreach ( $screens as $screen ) {
  8.                 add_meta_box(
  9.                         'labishop_sectionid',
  10.                         __( 'ADDING ITEM', 'labishop_textdomain' ),
  11.                         'labishop_meta_box_callback',
  12.                         $screen
  13.                 );
  14.         }
  15. }
  16. add_action( 'add_meta_boxes', 'labishop_add_meta_box' );
  17. /**
  18.  * Prints the box content.
  19.  *
  20.  * @param WP_Post $post The object for the current post/page.
  21.  */
  22. function labishop_meta_box_callback( $post ) {
  23.         // Add a nonce field so we can check for it later.
  24.         wp_nonce_field( 'labishop_save_meta_box_data', 'labishop_meta_box_nonce' );
  25.         /*
  26.          * Use get_post_meta() to retrieve an existing value
  27.          * from the database and use the value for the form.
  28.          */
  29.         $value = get_post_meta( $post->ID, 'ls_itemname', true );
  30.         $value2 = get_post_meta( $post->ID, 'ls_price', true );
  31.         echo '<label for="labishop_new_field">';
  32.         _e( 'Enter your items here:', 'labishop_textdomain' );
  33.         echo '</label> <br>';
  34.         echo '<label for="labishop_itemname">';
  35.         _e( 'Item NAME', 'labishop_textdomain' );
  36.         echo '</label> ';
  37.         echo '<input type="text" id="labishop_itemname" name="labishop_itemname" value="' . esc_attr( $value ) . '" size="50" />';
  38.        
  39.         echo '<label for="labishop_price"><br>';
  40.         _e( 'Item PRICE: (IRR/AMD/MYR)', 'labishop_textdomain' );
  41.         echo '</label> ';
  42.         echo '<input type="text" id="labishop_price" name="labishop_price" value="' . esc_attr( $value2 ) . '" size="50" />';
  43. }
  44. /**
  45.  * When the post is saved, saves our custom data.
  46.  *
  47.  * @param int $post_id The ID of the post being saved.
  48.  */
  49. function labishop_save_meta_box_data( $post_id ) {
  50.         /*
  51.          * We need to verify this came from our screen and with proper authorization,
  52.          * because the save_post action can be triggered at other times.
  53.          */
  54.         // Check if our nonce is set.
  55.         if ( ! isset( $_POST['labishop_meta_box_nonce'] ) ) {
  56.                 return;
  57.         }
  58.         // Verify that the nonce is valid.
  59.         if ( ! wp_verify_nonce( $_POST['labishop_meta_box_nonce'], 'labishop_save_meta_box_data' ) ) {
  60.                 return;
  61.         }
  62.         // If this is an autosave, our form has not been submitted, so we don't want to do anything.
  63.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  64.                 return;
  65.         }
  66.         // Check the user's permissions.
  67.         if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
  68.                 if ( ! current_user_can( 'edit_page', $post_id ) ) {
  69.                         return;
  70.                 }
  71.         } else {
  72.                 if ( ! current_user_can( 'edit_post', $post_id ) ) {
  73.                         return;
  74.                 }
  75.         }
  76.         /* OK, it's safe for us to save the data now. */
  77.        
  78.         // Make sure that it is set.
  79.         if ( ! isset( $_POST['labishop_new_field'] ) ) {
  80.                 return;
  81.         }
  82.         // Sanitize user input.
  83.         $ls_itemname = sanitize_text_field( $_POST['labishop_itemname'] );
  84.         $ls_price = sanitize_text_field( $_POST['labishop_price'] );
  85.         // Update the meta field in the database.
  86.         update_post_meta( $post_id, 'ls_itemname', $ls_itemname);
  87.         update_post_meta( $post_id, 'ls_price', $ls_price);
  88. }
  89. add_action( 'save_post', 'labishop_save_meta_box_data' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement