Make WordPress Core

Opened 7 months ago

Last modified 8 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: has-patch needs-testing
Focuses: docs Cc:


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"

add_feed( '_feed2', 'feed2_callback' );

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

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

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 (7)

#1 @hellofromTonya
7 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
7 months ago

Thanks @hellofromTonya,

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

#3 follow-up: @jorbin
7 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
7 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.

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
4 months ago

  • Milestone changed from 6.5 to 6.6

This ticket was mentioned in PR #6661 on WordPress/wordpress-develop by @snehapatil02.

3 weeks ago

  • Keywords has-patch added; needs-patch removed

## Ticket:

  • This PR addresses an issue in the add_feed function where feed names starting with an underscore are not handled properly, leading to errors.
  • The function now includes validation to prevent feed names from starting with an underscore, improving overall robustness and developer experience.

## Background:

  • The add_feed function allows developers to register custom feed types in WordPress. However, if the feed name starts with an underscore, it is stripped by the do_feed function, causing unexpected errors.
  • This PR introduces a validation step to ensure feed names do not start with an underscore, thereby avoiding these issues.

## Changes Made:
### Validation in add_feed Function:

  • Added a check to ensure the feed name does not start with an underscore (_).
  • If the feed name is invalid, an error is logged using error_log, and the function returns false.

### Proper Initialization Check:

  • Ensured that the $wp_rewrite global variable is initialized before proceeding with feed registration.

#7 @oglekler
8 days ago

  • Keywords needs-testing added

PR needs to be tested. There should be the version of WordPress where the message was added (Now it can be 6.6.0) instead of '6.2.0'.

Note: See TracTickets for help on using tickets.