Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // call the below all code into functions.php
- // How to add custom Metabox in WordPress without any Plugin?
- // Create the function to retrieve custom fields value
- function fc_get_custom_field_jokes( $value ) {
- global $post;
- $custom_field = get_post_meta( $post->ID, $value, true );
- if ( !empty( $custom_field ) )
- return is_array( $custom_field ) ? stripslashes_deep( $custom_field ) : stripslashes( wp_kses_decode_entities( $custom_field ) );
- return false;
- }
- // Register the Metabox for Post Type
- function fc_add_meta_box_for_jokes() {
- 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
- }
- add_action( 'add_meta_boxes', 'fc_add_meta_box_for_jokes' );
- // Metabox Output
- function fc_meta_box_output_for_jokes( $post ) {
- // create a wp nonce field
- wp_nonce_field( 'fc_nonce_jokes', 'my_nonce_jokes' ); ?>
- <p>
- <label for="jokes_contributor_name"><?php _e( 'Contributor Name', 'textdomain' ); ?>:</label>
- <input type="text" name="jokes_contributor_name" id="jokes_contributor_name" value="<?php echo fc_get_custom_field_jokes( 'jokes_contributor_name' ); ?>" size="25" />
- </p>
- <p>
- <label for="jokes_contributor_state"><?php _e( 'Contributor State', 'textdomain' ); ?>:</label>
- <input type="text" name="jokes_contributor_state" id="jokes_contributor_state" value="<?php echo fc_get_custom_field_jokes( 'jokes_contributor_state' ); ?>" size="25" />
- </p>
- <p>
- <label for="jokes_contributor_city"><?php _e( 'Contributor City', 'textdomain' ); ?>:</label>
- <input type="text" name="jokes_contributor_city" id="jokes_contributor_city" value="<?php echo fc_get_custom_field_jokes( 'jokes_contributor_city' ); ?>" size="25" />
- </p>
- <p>
- <label for="jokes_contributor_des"><?php _e( 'Contributor Description', 'textdomain' ); ?>:</label>
- <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>
- </p>
- <?php
- }
- // Save the Metabox values with Post ID
- function fc_save_metabox_jokes( $post_id ) {
- // Stop the script when doing autosave
- if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
- // Verify the nonce. If is not there, stop the script
- if( !isset( $_POST['my_nonce_jokes'] ) || !wp_verify_nonce( $_POST['my_nonce_jokes'], 'fc_nonce_jokes' ) ) return;
- // Stop the script if the user does not have edit permissions
- if( !current_user_can( 'edit_post' ) ) return;
- // Save the Contributor Name Field
- if( isset( $_POST['jokes_contributor_name'] ) )
- update_post_meta( $post_id, 'jokes_contributor_name', esc_attr( $_POST['jokes_contributor_name'] ) );
- // Save the Contributor State Field
- if( isset( $_POST['jokes_contributor_state'] ) )
- update_post_meta( $post_id, 'jokes_contributor_state', esc_attr( $_POST['jokes_contributor_state'] ) );
- // Save the Contributor City Field
- if( isset( $_POST['jokes_contributor_city'] ) )
- update_post_meta( $post_id, 'jokes_contributor_city', esc_attr( $_POST['jokes_contributor_city'] ) );
- // Save the Contributor Description Field
- if( isset( $_POST['jokes_contributor_des'] ) )
- update_post_meta( $post_id, 'jokes_contributor_des', esc_attr( $_POST['jokes_contributor_des'] ) );
- }
- add_action( 'save_post', 'fc_save_metabox_jokes' );
- // post type
- add_action( 'init', 'create_post_type' );
- function create_post_type() {
- register_post_type( 'jokes',
- array(
- 'labels' => array(
- 'name' => __( 'Metabox' ),
- 'singular_name' => __( 'Product' )
- ),
- 'public' => true,
- 'has_archive' => true,
- 'supports'=> array(''),
- )
- );
- }
- // source link: http://tipstolearn.com/add-custom-metabox-wordpress-without-plugin.html
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement