3

I've created a simple text custom meta field for pages. I can successfully enter plain text which prints on the frontend of the site with no problem.

I want to add shortcodes also. If I add a shortcode to the field, the shortcode prints to the front end as plain text.

The code I'm using:

<?php $meta = get_post_meta($post->ID, 'intSlider', true); ?>
   <div id="sliderWrap">
     <div id="slider" class="floatLeft">
        <? echo $meta; ?>
     </div>
   </div>

I've looked into using the below code but not having much luck:

<?php echo ( do_shortcode( get_post_meta( $post->ID , 'intSlider' , true ) ) ); ?>

Any help much appreciated

Thanks

  • This looks wrong, should be echo do_shortcode( get_post_meta( $post->ID , 'intSlider' , true ) ); – Dan Dec 15 '15 at 21:42
1

It looks like you have all the pieces you need to make this work - you just need to connect them. Try the snippet below

/**
 * get_post_meta returns either the value of the custom field or false
 * so we need to be sure we have the string before trying to output the shortcode
 */

$meta = get_post_meta($post->ID, 'intSlider', true);
?>
<div id="sliderWrap">
  <div id="slider" class="floatLeft">
    <?php
    //this will just echo the value saved
    // -- echo $meta;

    // this should render the shortcode if available - as long asthe $meta has the square brackets i.e [shortcode-name]
    if( $meta ) {
      echo do_shortcode( $meta );
    }else{
      //this is just in place for debugging
      echo '$meta was empty';
    }
    ?>
</div>
  • Thank for the advice. Unfortunately the above code supplied hasn't worked. The shortcode is still printing on the frontend as plain text. – rikardo85 Nov 3 '14 at 14:47
  • do_shortcode() will return the text passed to it if the shortcode is not active or defined. I'm thinking that it may be an issue with the shortcode string being stored. What is the value of intSlider in the case above? – Welcher Nov 3 '14 at 17:32
  • The value is [nivoslider id="24"] I've managed to find a workaround but would be good to know how to do this in future if it ever crops up again. Thanks again. – rikardo85 Nov 4 '14 at 9:14
1

You can do this by using 'the_content' filter. That way, Wordpress will treat the content as it was came from the editor field and execute all the shortcodes:

<?php $meta = get_post_meta($post->ID, 'intSlider', true); ?>
<div id="sliderWrap">
    <div id="slider" class="floatLeft">
        <? echo apply_filters('the_content', $meta); ?>
    </div>
</div>

Just be careful because it will wrap the content arround P tags. To solve that, you can do a simple replace to remove it:

...
<?php
    $content = apply_filters('the_content', $meta);
    $content = str_replace(array('<p>', '</p>'), '', $content);
?>
...

Hope it helps :)

  • why not just: remove_filter('the_content', 'wpautop'); $content = apply_filters('the_content', $meta); You can always re-add the wpautop filter back in after. – Hybrid Web Dev Oct 3 '17 at 11:09

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.