Opened 13 months ago
Closed 3 months ago
#59945 closed defect (bug) (fixed)
About the feed name specified in the add_feed()
Reported by: | tmatsuur | Owned by: | DrewAPicture |
---|---|---|---|
Milestone: | 6.7 | Priority: | normal |
Severity: | normal | Version: | 1.5 |
Component: | Feeds | Keywords: | changes-requested has-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 &rsaquo; Error]]></title> <message><![CDATA[<strong>Error:</strong> 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 (17)
#2
@
13 months ago
Thanks @hellofromTonya,
It is deeply moving to realize that this issue goes back 18 years.
#3
follow-up:
↓ 4
@
13 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
@
13 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.
This ticket was mentioned in PR #6661 on WordPress/wordpress-develop by @snehapatil02.
7 months ago
#6
- Keywords has-patch added; needs-patch removed
## Ticket: https://core.trac.wordpress.org/ticket/59945
- 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
@
6 months 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'.
This ticket was mentioned in Slack in #core by oglekler. View the logs.
6 months ago
#9
@
6 months ago
The patch itself looks very simple, but it has a string, and we have just few days before RC1 with hard string freeze. So, we need to hurry up with this one.
Do we need Unit tests for this, or/and can they be added via another ticket?
#10
@
6 months ago
- Keywords changes-requested needs-patch added; has-patch needs-testing removed
Thanks for the patch @snehapatil02 but as per comments 3 and 4, it was suggested to only update the function docblock, to avoid breaking change.
This ticket was mentioned in PR #6880 on WordPress/wordpress-develop by @narenin.
6 months ago
#11
- Keywords has-patch added; needs-patch removed
Trac ticket: https://core.trac.wordpress.org/ticket/59945
#12
follow-up:
↓ 13
@
6 months ago
Hi @audrasjb
I have suggest PR with suggested doc changes and also created PR to update in doc function.
https://github.com/WordPress/Documentation-Issue-Tracker/issues/1616
https://github.com/WordPress/wordpress-develop/pull/6880
#13
in reply to:
↑ 12
@
6 months ago
Replying to narenin:
https://github.com/WordPress/Documentation-Issue-Tracker/issues/1616 was closed.
Below sentence was added to More Information section of add_feed() reference so that it can be reflected without any code changes. After the merge of PR 6880, we can remove this sentence.
$feedname parameter should not start with ‘_‘. Please refer #59945.
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 theVersion
to match.