2

I'm trying to add some meta fields to a WPSC product using the following code:

/**
 * data callback
 */
function abc_callback($object, $box)
{
    echo 'callback executed!';
}

/**
 * add custom fields
 */
function abc_load_post($post_id)
{
    add_meta_box('abc_post_id', 'abc', 'abc_callback', 'post', 'normal', 'default', array());
}
add_action('load-post.php', 'abc_load_post', 10, 2);
add_action('load-post-new.php', 'abc_load_post', 10, 2);

The abc_load_post function gets called fine but nothing actually appears on the product editing page (i.e. /wp-admin/post.php)

Can someone explain what I'm doing wrong?

5

I can see 2 problems with your code. You seem to want that meta box to appear on product pages, but you actually add it to the post type and not the product post type. The other problem is the hook you attach your function to. Try this:

add_action( 'add_meta_boxes_{post_type}', 'abc_load_post' );

Substitute {post_type} with whatever post type you actually want to target, e.g. product, post or page. The fourth argument for add_meta_box() is the post type, btw, which you have set to 'post'.

  • The reason I was using 'load-post.php' is that the products are stored as posts. – Matthew Apr 3 '12 at 14:12
  • I'm now using 'add_meta_boxes_wpsc-product' (as per a search of the shopping cart code base where I found that tag being sed) but it still doesn't work. – Matthew Apr 3 '12 at 14:15
  • 1
    Have you changed add_meta_box('abc_post_id', 'abc', 'abc_callback', 'post', 'normal', 'default', array()); as well to add_meta_box('abc_post_id', 'abc', 'abc_callback', 'wpsc-product', 'normal', 'default', array()); ? – shabushabu Apr 3 '12 at 14:26
  • I have now - and that's done it! Thank you so much! – Matthew Apr 3 '12 at 14:29
1

Use the action 'add_meta_boxes' to add a meta box. See my demo plugin Basic Meta Box for an example.

  • I've replaced 'load-post.php' and 'load-post-new.php' with 'add_meta_boxes' but it makes no difference. Any other ideas? – Matthew Apr 3 '12 at 14:10

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.