Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

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'.

share|improve this answer
    
The reason I was using 'load-post.php' is that the products are stored as posts. –  Matt 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. –  Matt 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! –  Matt Apr 3 '12 at 14:29
add comment

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

share|improve this answer
    
I've replaced 'load-post.php' and 'load-post-new.php' with 'add_meta_boxes' but it makes no difference. Any other ideas? –  Matt Apr 3 '12 at 14:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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