WordPress.org

Ready to get started?Download WordPress

Forums

executing shortcodes through get_the_excerpt(); (15 posts)

  1. Joshuwar
    Member
    Posted 3 years ago #

    Hi, thanks for reading.

    I'm trying to execute a shortcode which exists in an excerpt on a site I'm working on.

    This works fine automatically using the_excerpt();

    But the_excerpt(); inserts an unnecessary <p>, which I do not want.

    So I've been using echo (get_the_excerpt();, which is fine except that it doesn't execute the shortcode.

    I've played around with some filters and the do_shortcode($blah) stuff, all to zero effect.

    So if anybody could help me either:

    1. stop the_excerpt(); from inserting p tags, or
    2. make get_the_excerpt(); execute a shortcode

    I would be very much obliged.

    J

  2. Joshuwar
    Member
    Posted 3 years ago #

    Any ideas at all?

  3. Joshuwar
    Member
    Posted 3 years ago #

    Is this in the wrong place on the forum? Should I repost in another section?

  4. vtxyzzy
    Member
    Posted 3 years ago #

    This is the right forum but I think that do_shortcode() should work and nobody has a better answer.

  5. Joshuwar
    Member
    Posted 3 years ago #

    OK. Well, for the record, here are things I've tried. Please let me know if anything doesn't look right so I can try again.

    Replacing the string replace function in the get_the_excerpt function (I commented out the bit of trim_excerpt which gets rid of script etc):

    function improved_trim_excerpt($text) {
            global $post;
            if ( '' == $text ) {
                    $text = get_the_content('');
                    /*$text = apply_filters('the_content', $text);
                     $text = str_replace('\]\]\>', ']]>', $text);
                     $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
                     $text = strip_tags($text, '<p>'); */
                    $excerpt_length = 80;
                    $words = explode(' ', $text, $excerpt_length + 1);
                    if (count($words)> $excerpt_length) {
                            array_pop($words);
                            array_push($words, '[...]');
                            $text = implode(' ', $words);
                    }
            }
            return $text;
    }
    
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'improved_trim_excerpt');

    I also tried this, which I saw suggested in a few places:

    add_filter( 'get_the_excerpt', 'shortcode_unautop');
    add_filter( 'get_the_excerpt', 'do_shortcode');

    And I tried this, with and without an echo:

    do_shortcode(get_the_excerpt());

    None of these are working for me. I will either dig out the reason, or create a workaround which fulfils my requirement, and post it back here.

  6. vtxyzzy
    Member
    Posted 3 years ago #

    I think you should try the do_shortcode with an echo:

    echo do_shortcode(get_the_excerpt());

  7. Joshuwar
    Member
    Posted 3 years ago #

    I tried that- it just echoes the unexecuted shortcode, like this:

    [shortcode] the text of the excerpt

    Will keep digging when I have a chance...

  8. Joshuwar
    Member
    Posted 3 years ago #

    PS. Thanks! (:

  9. vtxyzzy
    Member
    Posted 3 years ago #

    'echo do_shortcode()' should work. What code did you use to add the shortcode function?

  10. Joshuwar
    Member
    Posted 3 years ago #

    Sorry I don't follow- on my page template I tried:

    do_shortcode(get_the_excerpt());

    and

    echo do_shortcode(get_the_excerpt());

    But neither execute the shortcode. Without the echo nothing is output at all, with the echo I get the text of the shortcode included in the excerpt, as described above.

    The shortcode I'm using is created by a plugin: Star-Rating-for-Reviews. This shortcode is executed perfectly when I call the_excerpt();, but not when using do_shortcode(get_the_excerpt);

    Am I missing something?

  11. vtxyzzy
    Member
    Posted 3 years ago #

    The shortcode is probably registered by the plugin when you call the_excerpt and so is not registered otherwise.

    You should find the 'add_shortcode()' function call in the plugin and add it to your code.

  12. Joshuwar
    Member
    Posted 3 years ago #

    Interesting... So I searched for the add_shortcode() call in the plugin, and it wasn't there. So I had a look for the mechanism it was using. It seems that it was doing a preg_replace on the shortcode, and dumping the results back into the content via a couple of filters:

    add_filter('the_content', 'sr_addstar');
    add_filter('the_excerpt', 'sr_addstar');

    I realised all I needed to do was add another line:

    add_filter('get_the_excerpt', 'sr_addstar');

    And hey- it worked.

    So it was the plugin. Thanks vtxyzzy for pointing me in the right direction.

  13. fringer
    Member
    Posted 2 years ago #

    Any idea how to transform that code for wp-filebase and NextGen gallery?

  14. Fringer? You would be better served starting your own topic and providing more detail in that new topic.

  15. fringer
    Member
    Posted 2 years ago #

    Well, I thought it would be better to keep it all in one topic, since I'm trying to achieve the same thing as the original poster, only with different plugins.

Topic Closed

This topic has been closed to new replies.

About this Topic