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.

My plugin, WP Coda Slider, uses a shortcode to get_posts and add them to the slider. If one of the posts contains shortcode the shortcode in that post will not work.

$my_wpcodaslider = new wpcodaslider();
class wpcodaslider{

    var $shortcode_name = 'wpcodaslider';
    var $pattern = '<!-- wpcodaslider -->';
    var $posts_content = '';

    function wpcodaslider() {
        add_shortcode( $this->shortcode_name, array( &$this, 'shortcode' ) );
        add_action( 'the_posts', array( &$this, 'wpcodaslider_scripts' ) );
    }

    // insert the shortcode in any page ie: [wpcodaslider id=slidername cat=4 show=3] will show first three post in category with id of 4
    function shortcode( $atts, $content = null ) {
        extract( shortcode_atts( array(
            'cat' => null,
            'id'  =>  null,
            'show' => null,
            'args' => null
        ), $atts ) );

        //Make sure there is a query and name
        if (! $cat || ! $id)
            return 'Could not load slider. Mallformed shortcode.';
    $o = '
        <div class="coda-slider-wrapper">
            <div class="coda-slider preload" id="'. $id .'">';

        $posts = get_posts('post_type=post&order=desc&cat= '. $cat . '&numberposts= ' . $show . '');
        foreach($posts as $post){

            $o.=
            '<div class="panel" id="post-' . $post->ID . '">
                <div class="panel-wrapper">
                    <h2 class="title">' . $post->post_title . '</h2>
                    ' . $post->post_content . '
                </div> <!-- .panel-wrapper -->
            </div><!-- .panel #post-$id -->';
        }


        $o.='
                </div><!-- .coda-slider .preload -->
            </div><!-- coda-slider-wrapper -->

            <script type="text/javascript">
    jQuery(document).ready(function($){
                $().ready(function() {
                    $(\'#'. $id .'\').codaSlider({' . $args .'});
                });
    });
            </script>';


        return $o;
    }

It also does another foreach loop to check for its shortcode before enqueing the scripts.

function wpcodaslider_scripts($posts) {


        if (empty($posts)) return $posts;

        $shortcode_found = false; // use this flag to see if styles and scripts need to be enqueued
        foreach ($posts as $post): {
            if (stripos($post->post_content, 'wpcodaslider')) {
                $shortcode_found = true; //shortcode found so lets load the scripts
                break;
            }
        }

        if ($shortcode_found) {
            //enqueue scripts
            wp_enqueue_script('coda_slider', WP_PLUGIN_URL . '/wp-coda-slider/js/coda.slider.js',
            array('jquery'));

            //enqueue style sheet
            wp_enqueue_style('coda_slider', WP_PLUGIN_URL . '/wp-coda-slider/css/coda-slider-2.0.css');
        }

        return $posts;
    }

}

Any ideas on how I can get shortcodes within the posts to work? I have tested it with the [gallery] shortcode and a few others from various plugins.

share|improve this question

2 Answers 2

The issue is you are echoing raw post_content field from object. It isn't what usually gets to front-end. Try:

apply_filters( 'the_content', $post->post_content )

If you want to run shortcodes alone (without bunch of other filters) you can do:

do_shortcode( $post->post_content )

Edit re-read your question... Is there a possibility that this thing gets recursive? Post containing slider shortocde that calls itself?

share|improve this answer
    
I haven't tried it on a post. All the testing was done with the shortcode added to a page and including posts within a category id. –  Chris_O Sep 27 '10 at 18:32

Rarst is correct, using do_shortcode() will do the trick. Not sure about apply_filters(), but probably.

If there is an issue with recursion, just de-register your shortcode before the loop and re-register it afterward so that your shortcode won't get evaluated by do_shortcode() in the loop. (Or just hack up the content you pass to do_shortcode to delete text matching /[wpcodaslider.*]/ - note [ and ] need to be escaped in regexes)

share|improve this answer

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.