Simply put: Should custom meta boxes be able to output shortcodes the same way as Wordpress' native post editor?

Reason I ask is because Ive created my own custom meta box for inputting a shortcode from the Cart66 plugin in order to output an "Add To Cart" button but when I try - it only prints the text from the shortcode rather than rendering the actual button.

My current meta box:

// Register Custom Post Type Meta Box -- Add To Cart Button Field   //
add_action( 'add_meta_boxes', 'store_button_meta_box_add' );
function store_button_meta_box_add()
    add_meta_box( 'epr_store_button_meta_id', 'Add To Cart Button [shortcode]', 'store_button_meta_box_cb', 'epr_store', 'side', 'high' );

function store_button_meta_box_cb( $post )
    $values = get_post_custom( $post->ID );
    $button = isset( $values['meta_box_button_shortcode'] ) ? esc_attr( $values['meta_box_button_shortcode'][0] ) : '';
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
        <textarea name="meta_box_button_shortcode" id="meta_box_button_shortcode" row="5" style="width:100%; height:100px;text-align:left;"><?php echo esc_textarea($button); ?></textarea>

add_action( 'save_post', 'epr_button_meta_box_save' );
function epr_button_meta_box_save( $post_id )
    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

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

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

    // now we can actually save the data
    $allowed = array( 
        'a' => array( // on allow a tags
            'href' => array() // and those anchords can only have href attribute

    // Probably a good idea to make sure your data is set
    if( isset( $_POST['meta_box_button_shortcode'] ) )
        update_post_meta( $post_id, 'meta_box_button_shortcode', wp_kses( $_POST['meta_box_button_shortcode'], $allowed ) );

Short answer: no.

Shortcodes are for injecting content directly in the Post Content, by the user, via the Post Edit screen.

If your data are coming from a metabox, you have other, better, more efficient, and easier-to-control means of injecting content into the Post Content: primarily, the the_content filter (for general usage), or custom action hooks (for Themes that provide them).

Using the the_content filter to add your code is simple:

function my_plugin_filter_the_content( $content ) {
    // Get post custom meta
    $values = get_post_custom( $post->ID );
    // Determine if shortcode meta is set
    $my_custom_content = isset( $values['meta_box_button_shortcode'] ) ? esc_attr( $values['meta_box_button_shortcode'][0] ) : '';
    // Tell WordPress to execute the shortcode
    $custom_shortcode_output = do_shortcode( $my_custom_content );
    // Append the executed shortcode to $content
    $content .= $custom_shortcode_output;
    // Return modified $content
    return $content;
add_filter( 'the_content', 'my_plugin_filter_the_content' );

No need for a shortcode.

Also, using a filter, you don't have to worry about your shortcode being left orphaned if the user deactivates your Plugin.

Note that, if you need to output a specific shortcode, you can use do_shortcode( '[shortcode_name att="value"]' )

  • Interesting...I wouldnt really know how to hook into each product id though that cart66 provides as individual shortcodes from a dropdown menu in the editor however. – Mr.Brown Sep 26 '11 at 21:21
  • You would query your custom post meta, of course - just like you're (presumably) doing now. – Chip Bennett Sep 26 '11 at 21:26
  • See updated answer. I think you need to use do_shortcode(), in addition to my previous answer. – Chip Bennett Sep 26 '11 at 21:33
  • Sorry for delayed response, been a busy day...Thanks for the help Chip, will this still be applicable even if each shortcodes values differ from one another, I havent tried it yet but it appears that the code is only trying to output 1 specific shortcode button each time which will not work if thats the case. --- In retrospect, I suppose what would be even more useful would be hooking directly into cart66's dropdown menu that the plugin generates and somehow placing it into a meta box. That would be ideal anyways, just wasnt sure if that would even be possible. – Mr.Brown Sep 27 '11 at 0:53
  • This method should work, if you know the name of the post custom meta key to query. You query it (via get_post_custom()), and then use its output (if set), to pass as the shortcode string to do_shortcode(). So, it looks to me like it should work. – Chip Bennett Sep 27 '11 at 1:52

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .