How to get shortcode working from custom meta field

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 🙂

Leave a Comment