WordPress.org

Forums

Working with get_the_excerpt() in plugin (5 posts)

  1. Blythe
    Member
    Posted 9 years ago #

    I'm writing my first plugin and hit a wierd snag. I'm using get_the_excerpt(), which automatically uses the first 120 (I think) characters of the post if there isn't a specified excerpt. However, when it does this, it doesn't seem to go through the same set of filters as a specified excerpt would. Some of the characters are different and less URL-encoding friendly. Any ideas why or how to fix this?

    Hope this question makes sense. Rereading it's a bit confusing. Thanks for any help you might be able to offer.

  2. Kafkaesqui

    Posted 9 years ago #

    The reason is there are a number of filters (formatting functions) which run on the_excerpt() but do not hit get_the_excerpt(), wptexturize() being one example.

    You can work around this by passing it through the_excerpt filters:

    $excerpt = apply_filters('the_excerpt', get_the_excerpt());

  3. Blythe
    Member
    Posted 9 years ago #

    Thanks, but for some reason this doesn't work for what I want to do. Here's what I tried with your code:

    $ap_message = apply_filters('the_excerpt', get_the_excerpt());
    echo '<a href="message.php/message=' . urlencode($ap_message) . '">link</a>';

    With this, the urlencoding doesn't seem to work completely. It doesn't create apostrophes correctly and in generated (not specified) excerpts, double space comes out as the  character. I'm so confused :(

    I'm gonna keep playing with it, let me know if I've misinterpreted something. I really appreciate the help.

  4. Blythe
    Member
    Posted 9 years ago #

    Oh, and the following code works beautifully for specified excerpts but not for generated ones:

    $ap_message = get_the_excerpt();
    echo '<a href="message.php/message=' . urlencode($ap_message) . '">link</a>';

  5. Blythe
    Member
    Posted 9 years ago #

    Still can't figure this out. Is there a way to manually apply the filters without using apply_filters()? That might be barking up the wrong tree anyway. At this point I'm worried I might have to generate my own excerpts.

Topic Closed

This topic has been closed to new replies.

About this Topic