2
votes

Trying to add a custom meta box to a custom post type in wordpress and nothing seems to work.

When I copy and paste a whole code snippet from tutorials it works just fine. When I try and add it to my already made custom post type, I get nothing. The page doesn't break, the custom meta box just doesn't display. Would love some help as I'm pulling my hair out.

At this point, I just want the darn thing to show up in the post editing interface!

// Register Custom Post Type
function generate_shows() {

  $labels = array(
    'name'                => _x( 'Shows', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'Show', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Shows', 'text_domain' ),
    'parent_item_colon'   => __( 'Parent Item:', 'text_domain' ),
    'all_items'           => __( 'All Shows', 'text_domain' ),
    'view_item'           => __( 'View Item', 'text_domain' ),
    'add_new_item'        => __( 'Add New Show', 'text_domain' ),
    'add_new'             => __( 'Add New', 'text_domain' ),
    'edit_item'           => __( 'Edit Item', 'text_domain' ),
    'update_item'         => __( 'Update Item', 'text_domain' ),
    'search_items'        => __( 'Search Shows', 'text_domain' ),
    'not_found'           => __( 'Not found', 'text_domain' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' )
  );
  $args = array(
    'label'               => __( 'enk_show', 'text_domain' ),
    'description'         => __( 'An individual ENK Shows', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
    'taxonomies'          => array( 'category', 'post_tag' ),
    'hierarchical'        => true,
    'rewrite'             => array( 'slug' => 'shows', 'with_front' => false ),
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'register_meta_box_cb' => 'add_enk_metaboxes',
    'capability_type'     => 'post'
  );
  register_post_type( 'enk_show', $args );

}

// Hook into the 'init' action
add_action( 'init', 'generate_shows', 0 );


// Add the Events Meta Boxes
function add_enk_metaboxes() {
  add_meta_box('wpt_events_location', 'Event Location', 'wpt_events_location', 'events', 'side', 'default');
}
3

3 Answers

0
votes
add_action('add_meta_boxes', 'add_enk_metaboxes')

You also need to create a function for the html for the box

e.g.

function wpt_events_location($post) { // your 3rd parameter is the function callback
     $metavalue= get_post_meta($post->id,'metakey',true);
     echo '<input type="text" name="formfield" value="'.$metavalue.'" >';
}

and a callback to save

function save_post_meta($post_id){
  if($_POST['formfield'])
  update_post_meta($post_id,'metakey',$_POST['formfield'];

}

add_action('save_post', 'save_post_meta');

The codex has some good examples as i remember including nonces and OOP style. PS note there is no security above, you still need to sanitize values etc.

0
votes

You Must try this:

First you create a meta box for your post type

<?php    
// Add the Events Meta Boxes
    function add_enk_metaboxes($post) {
      add_meta_box('wpt_events_location', 'Event Location', 'wpt_events_location', 'events', 'side', 'default');
    }
    add_action('add_meta_boxes','add_enk_metaboxes');

Then you make a function for meta fields:

function wpt_events_location($post){

    $txtEventLocation = get_post_meta($post->ID, 'txtEventLocation', true);
?>
    <table width="100%" border="0" cellspacing="4" cellpadding="0">
        <tr>
            <td width="16%">
                <strong>Event Location:</strong>
            </td>
            <td width="84%">
                <input type="text" name="txtEventLocation" id="txtEventLocation" size="72%" value="<?php echo $txtEventLocation ?>" />
            </td>
        </tr>
    </table>
<?php
}

Then you save the meta field data:

add_action('save_post','save_event_location');
function save_event_location(){
    global $post;

    $txtEventLocation = $_POST['txtEventLocation'];

    update_post_meta( $post->ID, 'txtEventLocation', $txtEventLocation);
}

?>

Hope you find your solution.

0
votes

Code will help you to create custom metabox in wordpress

<?php

add_action( 'add_meta_boxes', 'add_custom_metaboxes' );


// Add the Events Meta Boxes

function add_custom_metaboxes() {
    //add_meta_box('wpt_events_date', 'Event Date', 'wpt_events_date', 'wpstore', 'side', 'default');
    add_meta_box('wpt_events_location', 'Address Location', 'wpt_events_location', 'post_tyme_name', 'normal', 'high');
}

// The Event Location Metabox

function wpt_events_location() {
    global $post;

    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

    // Get the location data if its already been entered
    $addressline1 = get_post_meta($post->ID, '_addressline1', true);
    $addressline2 = get_post_meta($post->ID, '_addressline2', true);


    // Echo out the field
        echo '<p>Address Line 1:</p>';
        echo '<input type="text" name="_addressline1" value="' . $addressline1  . '" class="widefat" />';
        echo '<p>Address Line 2</p>';
        echo '<input type="text" name="_addressline2" value="' . $addressline2  . '" class="widefat" />';
}


// Save the Metabox Data

function wpt_save_events_meta($post_id, $post) {

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }

    // Is the user allowed to edit the post or page?
    if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;

    // OK, we're authenticated: we need to find and save the data
    // We'll put it into an array to make it easier to loop though.

    $events_meta['_addressline1'] = $_POST['_addressline1'];
    $events_meta['_addressline2'] = $_POST['_addressline2'];

    // Add values of $events_meta as custom fields

    foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }

}

add_action('save_post', 'wpt_save_events_meta', 1, 2); // save the custom fields