Advertisement
munirmahmud6

Metabox

Feb 17th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. function tp_meta_box_register() {
  2. add_meta_box( 'tp-recipe-metabox', 'Recipe Settings', 'tp_meta_box_fields', 'recipes', 'normal', 'high', null );
  3. }
  4. add_action( 'add_meta_boxes', 'tp_meta_box_register' );
  5.  
  6.  
  7. function tp_meta_box_fields( $post ){
  8. wp_nonce_field( basename( __FILE__ ), 'tp-metabox-nonce' );
  9. ?>
  10. <div>
  11.  
  12. <label for="calories">Calories: </label>
  13. <input type="text" id="calories" name="calories" value="<?php echo get_post_meta( $post->ID, 'calories', true ); ?>">
  14. <br>
  15.  
  16.  
  17. </div>
  18. <?php
  19. }
  20.  
  21.  
  22. //Save Metabox data
  23. function tp_save_metabox( $post_id ) {
  24. if ( ! isset( $_POST['tp-metabox-nonce']) || ! wp_verify_nonce( $_POST['tp-metabox-nonce'], basename( __FILE__ ) ) )
  25. return $post_id;
  26.  
  27. if( ! current_user_can( 'edit_post', $post_id ))
  28. return $post_id;
  29.  
  30. if( defined( "DOING_AUTOSAVE") && DOING_AUTOSAVE )
  31. return $post_id;
  32.  
  33. if ( isset( $_POST['calories'] ) ) {
  34. update_post_meta( $post_id, 'calories', sanitize_text_field( $_POST['calories'] ) );
  35. }
  36. }
  37. add_action( 'save_post', 'tp_save_metabox' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement