• tl;dr version: How can I determine the type of feed and the current post/page context for the_excerpt and the_content when invoked in response to a feed?

    More detailed version …

    When coding a filter hook for the_content or the_excerpt, the WordPress API gives me relatively fine grained control over the type of post or page being handled via the is_* series of template tags … is_front_page(), is_archive() and so on.

    In one of my plugins, all of this allows me to offer a very flexible set of configuration options for when to add and when not to add …. stuff … to a post or page’s content or excerpt. As I can detect the type of post/page and the context that the_content or the_excerpt is being invoked in I do do things along the lines of add stuff to the excerpt and the content if being displayed on the front page or in category archives or on pages but don’t add stuff anywhere else.

    What I can’t work out is how to determine the type of feed that the_content and/or the_excerpt is being invoked in. I can detect a feed in general via is_feed() but that’s as far as I can work this out. Moreover, when is_feed() returns true within the context of a feed, all the other is_* template tags (seem to) return false.

    As a result I can’t determine if, for example, the_content is being invoked in the context of a feed for the front page, or a feed for a category archive, or so on.

    Is this even possible, or am I just overlooking something? Much general Googling, Googling of the Codex and searching through the WordPress StackExchange has proved unsuccessful, so far.

    -Gary

Viewing 1 replies (of 1 total)
  • Gary,

    You cannot determine the context from the_content filter beyond what the basic conditional functions provide. Even those like is_page_template technically doesn’t work inside the loop (the workaround is to create own is_mycustom_template() to return whatever boolean based on a global that can be set at get_header or even the wp hooks).

    Now, is_feed() helps a ton. It can provide you a bail or an early return within your filtering of the_content:

    add_filter( 'the_content', 'prefix_the_content_filter' );
    /**
     * Filter the_content except for Feeds
     *
     * @param  string $content Post content
     * @return string $content Maybe modified post content
     */
    function prefix_the_content_filter( $content ) {
        If ( true === is_feed() ) return $content;
        // do something for non-feeds
        return $content;
    }

    Then you would hook into the_content_feed. Similarly, for the_excerpt, you would hook into the_excerpt_rss.

    add_filter( 'the_content_feed', 'prefix_the_content_feed_filter', 10, 2 );
    /**
     * Filter the_content for Feeds
     *
     * @param  string $content   Post content
     * @param  string $feed_type Feed type: rss, rss2, atom, rdf
     * @return string $content   Modified post content
     */
    function prefix_the_content_feed_filter( $content, $feed_type ) {
        // do something for feeds content
        return $content;
    }

    Some others of interest are:

    • comment_author_rss
    • comment_text_rss
    • the_category_rss
    • rss_enclosure
    • atom_enclosure
    • feed_content_type
    • the_title_rss
    • wp_title_rss & get_wp_title_rss
    • bloginfo_rss & get_bloginfo_rss

    These should get you on your way!

Viewing 1 replies (of 1 total)
  • The topic ‘How to work out the feed type that the_content/the_excerpt is being invoked in?’ is closed to new replies.