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 am trying to add a shortcode from a plugin to my custom metabox. I have read it isn't the done thing but in this case i do need it to work. The client might not add a video but might add an image into it or something is so need to make sure it can still work for standard content.

The issue I am having is that it is outputting just the shortcode like so - [youtube id="vfGZZJnoJ0U"]

I have tried adding filters to my metabox but still showing that.

This is my custom metabox setup:

add_action('add_meta_boxes', 'testimonials_meta_box');
function testimonials_meta_box() {
    add_meta_box('testimonials_content_id', 'Testimonial Image/Video', 'testimonials_meta_box_cb', 'kleiser_testimonials', 'normal', 'high');
}

function testimonials_meta_box_cb($post) {
    global $post;
    wp_nonce_field('testimonial_content_nonce', 'testimonial_content_meta_box_nonce');
    $values = get_post_custom($post->ID);

    $left_content = isset($values['testimonial_content']) ? $values['testimonial_content'][0]: '';

    wp_editor($left_content, 'testimonial-content');
}

function testimonial_content_save($post_id) {
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    if(!isset($_POST['testimonial_content_meta_box_nonce']) || !wp_verify_nonce($_POST['testimonial_content_meta_box_nonce'], 'testimonial_content_nonce')) return;
    if(!current_user_can('edit_pages')) return;

    if(isset($_POST['testimonial-content'])) {
        update_post_meta($post_id, 'testimonial_content', $_POST['testimonial-content']);
    }
}
add_action('save_post', 'testimonial_content_save');

The only odd thing I am doing is I am storing each posts value of the metabox in an array and then doing a for loop and outputting them. That code is:

Edit:

This is my template page which has all my code:

<div id='hear_what_say_container'>
<?php $wp_query = new WP_Query(array('post_type' => 'kleiser_testimonials', 'posts_per_page'=> -1 )); ?>
<?php if ($wp_query->have_posts()) :
    $counter = 0;
    $total = $wp_query->post_count;
    $testimonialsContentArray = array();
?>
<!-- <div class='testimonial'></div> -->
<div id='main_testimonials'>
    <div class='testimonial_row'>
        <?php while ($wp_query->have_posts()) : the_post(); ?>
        <?php
            $media = get_post_meta(get_the_ID(), 'testimonial_content', true);
        ?>
        <?php array_push($testimonialsContentArray, array('id' => get_the_ID(), 'title' => get_the_title(), 'content' => get_the_content(), 'testimonialMedia' => $media )); ?>

        <div class='testimonial' data-id='<?php the_ID(); ?>'>
            <div class='testimonail_padding'>
                <?php
                    if ( has_post_thumbnail() ) {
                        the_post_thumbnail();
                    }
                  ?>
                <div class='arrow_indicator'></div>
            </div>
        </div>
        <?php $counter++; ?>

        <?php if($counter % 5 == 0 || $counter == $total ): ?>
    </div>

    <?php endif; ?>
    <?php
        foreach($testimonialsContentArray as $testimoninalContent) {
    ?>
        <div class='testimonial_details' data-testimonial='<?php echo $testimoninalContent['id']; ?>'>
            <div class='detail_left testimonial_content'>
                <div class='detail_padding'>
                    <?php
                        echo wpautop($testimoninalContent['testimonialMedia']);
                    ?>
                </div>
            </div>
            <div class='detail_right testimonial_content'>
                <div class='detail_padding'>
                    <h1><?php echo $testimoninalContent['title'] ?></h1>
                    <?php echo wpautop( $testimoninalContent['content'] ); ?>
                </div>
            </div>
        </div>
    <?php
        }
    ?>
    <?php //endif; ?>
        <?php if($counter % 5 == 0 && $counter < $total ): ?>
        <div class='testimonial_row'>
            <?php $testimonialsContentArray = array(); ?>
        <?php endif; ?>
    <?php endwhile; ?>
</div></div>

I am scratching my head for quite some time and not working out why I can't get the shortcode to work.

Any help would be really appreciated

Thanks in advance

share|improve this question
    
If the answers below fail to solve things for you, please post your code is such a way that it can be easily copied into a dev stack and tested. –  s_ha_dum Nov 23 '13 at 16:22
    
would you like me to add all my html for the page? –  asaunders Nov 23 '13 at 16:23
    
Well, no. I don't think I need all of the code but it looks like you've created a metabox and callback to save data but it is not clear where/what the connection is to the last two blocks of code. –  s_ha_dum Nov 23 '13 at 16:27
    
Well it might actually be easier to add my html as that will show that i do a loop and grab the metabox content then put it into an array and out put the arrays content lower down the html –  asaunders Nov 23 '13 at 16:28
    
Alright. I assume that you have tried the suggestions below? –  s_ha_dum Nov 23 '13 at 16:29

3 Answers 3

up vote 2 down vote accepted

I noticed that your are using get_the_content(), but it will return unfiltered content.

The shortcodes are generated via the the_content filter with priority 11.

So if you want

  • only the shortcodes generated, try:

    'content' => do_shortcode( get_the_content() )
    
  • to filter it through the_content filter, try:

    'content' => apply_filters( 'the_content', get_the_content() )
    
  • to simulate the_content() function, try:

    'content' => str_replace(']]>', ']]&gt;', apply_filters( 'the_content', get_the_content() ) )
    
share|improve this answer
    
Hi Thanks for replying. I tried all 3 of those options you gave me but none worked :(. I don't know what to do –  asaunders Nov 23 '13 at 16:19
    
And you are sure this is a real shortcode you are testing? –  birgire Nov 23 '13 at 16:24
    
Yer. As for some reason If i add it to the about page it works but add it to another page or this custom metabox it just outputs the shortcode. It's really strange. Unfortunatly I cant give access to the dev site. but can show you html/code you need –  asaunders Nov 23 '13 at 16:26
    
I noticed recently that the [embed] shortcode is not generated via do_shortcode(), you have to use $GLOBALS['wp_embed']->run_shortcode() instead. Not sure if that's a similar issue here? Where does your [youtube] shortcode comes from? –  birgire Nov 23 '13 at 16:32
    
i just tried changing the do_shortcode to what you suggested but still not working :(. I get that youtube shortcode from the plugin called Advanced Responsive Video Embedder –  asaunders Nov 23 '13 at 16:40

Use do_shortcode() function. In your case this would be something like this:

<?php
     echo do_shortcode(wpautop($testimoninalContent['testimonialMedia']));
?>
share|improve this answer
    
Hi Thanks for the reply. I tried this and still only outputs the shortcode code. –  asaunders Nov 23 '13 at 16:09

wpautop just add breakspaces and paragraph to content.

Could you try, instead of

echo wpautop($testimoninalContent['testimonialMedia']);

to use the whole content filters with

echo apply_filters( 'the_content', $testimoninalContent['testimonialMedia'] );

This way, whatever contains the custom field (a link to a video, a image, some text, or a shortcode, it will be treated as the_content() (and so, executing shortcodes).

I may be wrong here, so let us know ?

share|improve this answer
    
Hi thanks for your response. I have tried doing this myself but not worked. Im now thinking my issue isnt to do with a shortcode as when i place the shortcode in some pages in the page editor it works and on some pages it doesn't. Any idea why that would be? –  asaunders Nov 23 '13 at 16:13

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.