Show only Shortcodes in Sidebar

View: New views
7 Messages — Rating Filter:   Alert me  

Show only Shortcodes in Sidebar

by navjotjsingh :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a blog where in sidebar, I need to fetch posts belonging to video
category. Now Videos are embedded using shortcodes. But since the_content is
used to show the posts in sidebar, any text and even image in a video post
gets shown in sidebar which makes it messy.

How to filter the_content so that only shortcodes display in that sidebar
and rest of content gets filtered out? I am noob with regular expressions
and such stuff.

Regards

Navjot Singh
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Show only Shortcodes in Sidebar

by Andy Skelton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> How to filter the_content so that only shortcodes display in that sidebar
> and rest of content gets filtered out?

$content = strip_shortcodes($post->post_content);

Andy
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Show only Shortcodes in Sidebar

by navjotjsingh :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This does the reverse. It removes the shortcodes. I want to display the
shortcodes only and anything except shortcodes should be filtered out.

Sent from Delhi, DL, India

On Mon, Oct 26, 2009 at 5:46 PM, Andy Skelton <skeltoac@...> wrote:

> > How to filter the_content so that only shortcodes display in that sidebar
> > and rest of content gets filtered out?
>
> $content = strip_shortcodes($post->post_content);
>
> Andy
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Show only Shortcodes in Sidebar

by Andy Skelton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>> > How to filter the_content so that only shortcodes display in that sidebar
>> > and rest of content gets filtered out?
>>
>> $content = strip_shortcodes($post->post_content);
>>
> This does the reverse. It removes the shortcodes. I want to display the
> shortcodes only and anything except shortcodes should be filtered out.

Sorry, I flipped a bit. Look at the source for strip_shortcodes. It
gets the regex pattern from get_shortcode_regex(). You can use that in
preg_replace. Something like this extempore and untested snippet:

$pattern = get_shortcode_regex();
$pattern = str_replace('(.?)', '', $pattern);
$pattern = str_replace('2', '3', $pattern);
$content = $post->post_content;
$shortcodes = preg_replace("/.*($pattern).*?/s", '$1', $content);

Andy
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Show only Shortcodes in Sidebar

by navjotjsingh :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thnx, this code does show the shortcodes, but how to reparse the shortcodes.
Afterall I want the shortcodes values (i.e. the videos) to show in sidebar.
do_shortcode($shortcodes) does not help.

Sent from Delhi, DL, India

On Mon, Oct 26, 2009 at 6:03 PM, Andy Skelton <skeltoac@...> wrote:

> >> > How to filter the_content so that only shortcodes display in that
> sidebar
> >> > and rest of content gets filtered out?
> >>
> >> $content = strip_shortcodes($post->post_content);
> >>
> > This does the reverse. It removes the shortcodes. I want to display the
> > shortcodes only and anything except shortcodes should be filtered out.
>
> Sorry, I flipped a bit. Look at the source for strip_shortcodes. It
> gets the regex pattern from get_shortcode_regex(). You can use that in
> preg_replace. Something like this extempore and untested snippet:
>
> $pattern = get_shortcode_regex();
> $pattern = str_replace('(.?)', '', $pattern);
> $pattern = str_replace('2', '3', $pattern);
> $content = $post->post_content;
> $shortcodes = preg_replace("/.*($pattern).*?/s", '$1', $content);
>
> Andy
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Show only Shortcodes in Sidebar

by chrisbliss18 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Looks like Andy beat me to the punch, but here's my solution just in
case it helps:

    add_filter( 'the_content', 'just_shortcodes' );

    function just_shortcodes( $content ) {
        global $shortcode_tags;
       
        $tagnames = array_keys( $shortcode_tags );
    //  $tagnames = array( 'video_shortcode' );
       
        $tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) );
       
        $regex = '\[('.$tagregexp.')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?';
       
       
        preg_match_all( '/' . $regex . '/s', $content, $matches,
    PREG_PATTERN_ORDER );
       
        return implode( '', $matches[0] );
    }

I yanked most of this code from the get_shortcode_regex function in
wp-includes/shortcodes.php.

There are a few things of note here:

   1. This is built as a filter, so all you have to do is use the
      add_filter call before your sidebar code and then run
      remove_filter( 'the_content', 'just_shortcodes' ) after the
      sidebar code to return the_content calls to normal.
   2. Since the $tagnames array is built inside the filter function, you
      can tailor it to your needs to only include those shortcodes that
      you wish to display. I show this with the commented out line that
      manually sets the array. If you use this method, simply remove the
      line above it as it is no longer needed.
   3. The shortcodes are just slapped together with no spacing,
      newlines, or breaks between them. You can change this by supplying
      a different divider string in the first argument of the implode
      function.

I hope this helps.

Chris Jean
http://gaarai.com/
http://wp-roadmap.com/



Andy Skelton wrote:

>>>> How to filter the_content so that only shortcodes display in that sidebar
>>>> and rest of content gets filtered out?
>>>>        
>>> $content = strip_shortcodes($post->post_content);
>>>
>>>      
>> This does the reverse. It removes the shortcodes. I want to display the
>> shortcodes only and anything except shortcodes should be filtered out.
>>    
>
> Sorry, I flipped a bit. Look at the source for strip_shortcodes. It
> gets the regex pattern from get_shortcode_regex(). You can use that in
> preg_replace. Something like this extempore and untested snippet:
>
> $pattern = get_shortcode_regex();
> $pattern = str_replace('(.?)', '', $pattern);
> $pattern = str_replace('2', '3', $pattern);
> $content = $post->post_content;
> $shortcodes = preg_replace("/.*($pattern).*?/s", '$1', $content);
>
> Andy
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>  
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Show only Shortcodes in Sidebar

by scribu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Oct 26, 2009 at 2:43 PM, Chris Jean <gaarai@...> wrote:

> How to filter the_content so that only shortcodes display in that sidebar
> and rest of content gets filtered out?
>

Depending on the shortcode, it might be easier to call the function that
it's generating it. For example, if your shortcode is like this:

add_shortcode('myshortcode', 'myshortcode_callback');

Instead of writing:

the_content();

you can write

myshortcode_callback($args);


--
http://scribu.net
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers