2

I've got a custom post type curiculum and a metabox that saves a value to a custom field. However this custom field is added to another custom post type (created by an e-commerce plugin) wpsc-product.

This is a snippet of the code that deals with the metabox. The question is, how to prevent the custom field being added to the wpsc-product?

/* Curiculum Date Metabox */
function curiculum_date_fields() {
if ( get_post_type() == 'curiculum'):
    global $post;
    $custom = get_post_custom( $post->ID );
    $curiculum_date_from = $custom["curiculum_date_from"][0];
    ?>
    <p>
        <label for="curiculum_date_from">Date:</label><br />
        <input size="32" type="text" name="curiculum_date_from" id="curiculum_date_from" value="<?php echo $curiculum_date_from; ?>" />
<?php
endif;
}

function add_curiculum_date_box() {
    add_meta_box("curiculum_date_info", "Event Date", "curiculum_date_fields", "curiculum", "side");
}

function save_curiculum_date() {
    global $post;
    update_post_meta($post->ID, "curiculum_date_from", $_POST["curiculum_date_from"]);
}
add_action('admin_init', 'add_curiculum_date_box');
add_action('save_post', 'save_curiculum_date');
add_action('publish_post', 'save_curiculum_date');

EDIT: Code for the curiculum custom post type:

add_action('init', 'curiculum_register');

function curiculum_register() {

$labels = array(
    'name' => _x('CV Events', 'post type general name'),
    'singular_name' => _x('CV Event', 'post type singular name'),
    'add_new' => _x('Add New', 'testimonials item'),
    'add_new_item' => __('Add New Event'),
    'edit_item' => __('Edit Event'),
    'new_item' => __('New Event'),
    'view_item' => __('View Event'),
    'search_items' => __('Search Events'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => ''
);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => 7,
    'exclude_from_search' => true,
    'supports' => array('title','editor', 'custom-fields')
      );

    register_post_type( 'curiculum' , $args );
 }
  • Have you solved this? – iEmanuele Aug 22 '13 at 23:37
0

EDIT: I also recommand you to use nonces to check your field before saving.

EDIT2: Nonces

Just before field, define a nonce :

wp_nonce_field( 'curiculum_meta_box_nonce', 'meta_box_nonce' );

Your saving function is not correct, use this instead :

  function save_curiculum_date($postid) {
    //stop if autosave
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // if our nonce isn't there, or we can't verify it, stop
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'curiculum_meta_box_nonce' ) ) return;

    // if our current user can't edit this post, stop
    if( !current_user_can( 'edit_post' ) ) return;

update_post_meta($postid, "curiculum_date_from", $_POST["curiculum_date_from"]);
}

The fields function is not correct, use this instead :

$curiculum_date_from = get_post_meta(get_the_ID(),'curiculum_date_from',true);
?>
<p>
    <label for="curiculum_date_from">Date:</label><br />
    <input size="32" type="text" name="curiculum_date_from" id="curiculum_date_from" value="<?php echo $curiculum_date_from; ?>" />

  • It's still there in the wpsc-product. I mean, I edit the product, delete the custom field, update it and it's still there. Thanks for the tip about the nounces, I find it a bit difficult to get my head around these. The documentation in codex is rather incomplete, could you recommend another source, that would give me an insight into the topic? – crs1138 Aug 22 '13 at 11:45
  • I've edited code. – JMau Aug 22 '13 at 12:09
  • +JMau: Thanks for that, have you got a link, where I could learn the ins and outs of the nounces. Now, let's focus on the original question - any ideas, how to convince WP not to add custom field to the wpsc-product? – crs1138 Aug 22 '13 at 14:19
  • I can't reproduce this bug, can you post the code you use for custom post type? – JMau Aug 22 '13 at 14:58
  • Ok, I've edited the original post and included the custom post type registration. – crs1138 Aug 22 '13 at 22:14
0

After removing the action:

add_action('admin_init', 'add_curiculum_date_box');

You can try this:

function add_curiculum_date_box($post){
      add_meta_box(
        "curriculum_date_info", 
        "Event Date", 
        "curiculum_date_fields", 
        $post->post_type,
        "side"
      );
}

And add this to your CPT $args:

'register_meta_box_cb' => 'add_curiculum_date_box'

When this post type is registered, it also sets into motion events that will create the meta box in the appropriate write screen. It saves the trouble of having to use a separate call to add_action to set up the meta box. It also keeps all of the code for the individual post type in one place. Functionally, it does the exact same thing as the “add_meta_boxes” variable hook. It is just a different way to accomplish the same task.

Credits

  • Thanks, I quite like your soulution as it's 'cleaner' code, but I got the same result, the CF still appears in the wpsc-product. – crs1138 Aug 23 '13 at 7:51
  • i'm sorry, this issue is causing headache.. : ) have you seen the link in the answer? there is a specific hook for specific custom post type. it looks like add_meta_boxes_{$post_type} – iEmanuele Aug 23 '13 at 7:59
  • Hi, thanks for pointing me in a direction. Really good article regarding meta boxes. Will have a play with it over the weekend. – crs1138 Aug 23 '13 at 23:21
  • 1
    There might be a little confusion. I'm not seeing the actual metabox, it's just the custom field is being generated for wrong CPT, thus appearing in listing of post meta names (value is empty) when listing a single product. – crs1138 Aug 24 '13 at 12:44
  • 1
0

just in case you didn't manage to find a simple but elegant fix for this, or if anybody else has stumbled upon this (or a similar issue), with WordPress Creation Kit from wordpress.org users can easily create CPT's or add meta-boxes them (if in any doubt, feel free to read the description of the plugin).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.