Advertisement
firoze

add custom Metabox in WordPress without any Plugin

Nov 3rd, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. // call the below all code into functions.php
  2. // How to add custom Metabox in WordPress without any Plugin?
  3.  
  4. // Create the function to retrieve custom fields value
  5. function fc_get_custom_field_jokes( $value ) {
  6. global $post;
  7.  
  8. $custom_field = get_post_meta( $post->ID, $value, true );
  9. if ( !empty( $custom_field ) )
  10. return is_array( $custom_field ) ? stripslashes_deep( $custom_field ) : stripslashes( wp_kses_decode_entities( $custom_field ) );
  11.  
  12. return false;
  13. }
  14.  
  15.  
  16. // Register the Metabox for Post Type
  17. function fc_add_meta_box_for_jokes() {
  18.  
  19. add_meta_box( 'fc-meta-box-for-jokes', __( 'Custom meta box', 'textdomain' ), 'fc_meta_box_output_for_jokes', 'jokes', 'sides', 'high' ); //show in custom post
  20. }
  21. add_action( 'add_meta_boxes', 'fc_add_meta_box_for_jokes' );
  22.  
  23.  
  24. // Metabox Output
  25. function fc_meta_box_output_for_jokes( $post ) {
  26. // create a wp nonce field
  27. wp_nonce_field( 'fc_nonce_jokes', 'my_nonce_jokes' ); ?>
  28.  
  29. <p>
  30. <label for="jokes_contributor_name"><?php _e( 'Contributor Name', 'textdomain' ); ?>:</label>
  31. <input type="text" name="jokes_contributor_name" id="jokes_contributor_name" value="<?php echo fc_get_custom_field_jokes( 'jokes_contributor_name' ); ?>" size="25" />
  32. </p>
  33. <p>
  34. <label for="jokes_contributor_state"><?php _e( 'Contributor State', 'textdomain' ); ?>:</label>
  35. <input type="text" name="jokes_contributor_state" id="jokes_contributor_state" value="<?php echo fc_get_custom_field_jokes( 'jokes_contributor_state' ); ?>" size="25" />
  36. </p>
  37. <p>
  38. <label for="jokes_contributor_city"><?php _e( 'Contributor City', 'textdomain' ); ?>:</label>
  39. <input type="text" name="jokes_contributor_city" id="jokes_contributor_city" value="<?php echo fc_get_custom_field_jokes( 'jokes_contributor_city' ); ?>" size="25" />
  40. </p>
  41. <p>
  42. <label for="jokes_contributor_des"><?php _e( 'Contributor Description', 'textdomain' ); ?>:</label>
  43. <textarea style="width:100%;height:100px" type="text" name="jokes_contributor_des" id="jokes_contributor_des" value="<?php echo fc_get_custom_field_jokes( 'jokes_contributor_des' ); ?>" size="25" ><?php echo fc_get_custom_field_jokes( 'jokes_contributor_des' ); ?></textarea>
  44. </p>
  45.  
  46. <?php
  47. }
  48.  
  49.  
  50. // Save the Metabox values with Post ID
  51. function fc_save_metabox_jokes( $post_id ) {
  52. // Stop the script when doing autosave
  53. if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
  54.  
  55. // Verify the nonce. If is not there, stop the script
  56. if( !isset( $_POST['my_nonce_jokes'] ) || !wp_verify_nonce( $_POST['my_nonce_jokes'], 'fc_nonce_jokes' ) ) return;
  57.  
  58. // Stop the script if the user does not have edit permissions
  59. if( !current_user_can( 'edit_post' ) ) return;
  60.  
  61. // Save the Contributor Name Field
  62. if( isset( $_POST['jokes_contributor_name'] ) )
  63. update_post_meta( $post_id, 'jokes_contributor_name', esc_attr( $_POST['jokes_contributor_name'] ) );
  64.  
  65. // Save the Contributor State Field
  66. if( isset( $_POST['jokes_contributor_state'] ) )
  67. update_post_meta( $post_id, 'jokes_contributor_state', esc_attr( $_POST['jokes_contributor_state'] ) );
  68.  
  69. // Save the Contributor City Field
  70. if( isset( $_POST['jokes_contributor_city'] ) )
  71. update_post_meta( $post_id, 'jokes_contributor_city', esc_attr( $_POST['jokes_contributor_city'] ) );
  72.  
  73. // Save the Contributor Description Field
  74. if( isset( $_POST['jokes_contributor_des'] ) )
  75. update_post_meta( $post_id, 'jokes_contributor_des', esc_attr( $_POST['jokes_contributor_des'] ) );
  76.  
  77. }
  78. add_action( 'save_post', 'fc_save_metabox_jokes' );
  79.  
  80.  
  81. // post type
  82. add_action( 'init', 'create_post_type' );
  83. function create_post_type() {
  84. register_post_type( 'jokes',
  85. array(
  86. 'labels' => array(
  87. 'name' => __( 'Metabox' ),
  88. 'singular_name' => __( 'Product' )
  89. ),
  90. 'public' => true,
  91. 'has_archive' => true,
  92. 'supports'=> array(''),
  93. )
  94. );
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101. // source link: http://tipstolearn.com/add-custom-metabox-wordpress-without-plugin.html
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement