Make WordPress Core

Opened 3 months ago

Last modified 9 days ago

#59945 new defect (bug)

About the feed name specified in the add_feed()

Reported by: tmatsuur's profile tmatsuur Owned by:
Milestone: 6.6 Priority: normal
Severity: normal Version: 1.5
Component: Feeds Keywords: needs-patch
Focuses: docs Cc:

Description

There is no restriction on the first parameter of the add_feed function, but if it starts with "_" it will not function properly.

function feed2_callback( $is_comment_feed, $feed ) {
	header( 'Content-Type: application/xml; charset=UTF-8', true );
	echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
>
	<title>Test</title>
</rss>
<?php
}

add_feed( '_feed2', 'feed2_callback' );

Next, update the rewrite rule and access "http://localhost/_feed2", which will return the following:

<error>
    <code>wp_die</code>
    <title><![CDATA[WordPress &amp;rsaquo; Error]]></title>
    <message><![CDATA[&lt;strong&gt;Error:&lt;/strong&gt; This is not a valid feed template.]]></message>
    <data>
        <status>404</status>
    </data>
</error>

This is not a problem with the code I tried, but because the "_" at the beginning of the feed name is removed in the do_feed function.

function do_feed() {
	global $wp_query;

	$feed = get_query_var( 'feed' );

	// Remove the pad, if present.
	$feed = preg_replace( '/^_+/', '', $feed );

This process results in an action name of 'do_feed_' . 'feed2' when $feed is '_feed2'.

Since this is not done in the add_feed function and the action name specified in the add_action function is 'do_feed_' . '_feed2', it appears that the two action names do not match, resulting in the error indicated in the response.

Should I remove the leading '_' in the feed name in the add_feed function or not remove the '_' in the do_feed function?

In any case, I would like to see consistency in the handling of feed names.

Change History (5)

#1 @hellofromTonya
3 months ago

  • Version changed from 6.4 to 2.1

Hello @tmatsuur,

Welcome to WordPress Core's Trac :)

I'm doing some ticket triage to help each move forward. The preg_replace() in question and the 2 functions were introduced in WordPress 2.1.0 via [3638] / #2433. Updating the Version to match.

#2 @tmatsuur
3 months ago

Thanks @hellofromTonya,

It is deeply moving to realize that this issue goes back 18 years.

#3 follow-up: @jorbin
3 months ago

With the knowledge that this has been in place for so long, my gut leans towards not wanting to make code changes but instead make this a documented part of the function.

#4 in reply to: ↑ 3 @hellofromTonya
3 months ago

  • Focuses docs added
  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to 6.5
  • Version changed from 2.1 to 1.5

Drilling further backwards in time, the RegEx that strips the _ was first introduced in [1376], 20 years ago. https://core.trac.wordpress.org/browser/trunk/wp-feed.php?rev=1376#L9

I'm not finding a ticket for it, i.e. to review the discussion of why it was needed.

Replying to jorbin:

With the knowledge that this has been in place for so long, my gut leans towards not wanting to make code changes but instead make this a documented part of the function.

Given this feed naming limitation has existed for 20 years, I agree with @jorbin. Though I'd like to know why it's needed, changing this area of the code is risky, i.e. likely will have unintended side effects.

Pulling into 6.5 and adding needs-patch to add the better documentation.

#5 @swissspidy
9 days ago

  • Milestone changed from 6.5 to 6.6
Note: See TracTickets for help on using tickets.