WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#34259 closed enhancement (fixed)

Dynamic 'do_feed_{$feed}' action should have a static equivalent

Reported by: slimndap Owned by: SergeyBiryukov
Milestone: 4.4 Priority: normal
Severity: normal Version: 4.3.1
Component: Feeds Keywords: has-patch
Focuses: template Cc:

Description

There should be a static do_feed action in do_feed().

This way developers can generate content based on the feed name. This is not possible with the current 'do_feed_{$feed}' action.

Example:

I have an events plugin and I want my users to set up different feeds with date and category filters. They can do this on an admin options page.

The hook could look like this:

do_action( 'do_feed', $feed, $wp_query->is_comment_feed );

Attachments (2)

34259.diff (888 bytes) - added by johnbillion 5 years ago.
34259.2.diff (449 bytes) - added by johnbillion 5 years ago.

Download all attachments as: .zip

Change History (15)

#1 @johnbillion
5 years ago

  • Keywords needs-patch needs-docs reporter-feedback added

Thanks for the ticket, slimndap.

Can you explain a little more about how this hook would help your plugin? As far as I can tell, your plugin still needs to check the value of $feed, otherwise it risks outputting data when it shouldn't, for example with custom feeds provided by other plugins (a JSON feed being a good example), especially ones that don't output XML.

#2 @slimndap
5 years ago

Yes, that is exactly the point. The plugin needs to check the value of $feed to know which feed to show.

This is not possible with the existing dynamic hook, nor with the callback function that you set with add_feed().
$feed is not available.

So for example:

  1. My users sets up a feed that always shows events for the coming week. He creates a new feed named 'Events this week'. He then sets 'Next Monday' as the start date and 'Next Monday + 1 week' as the end date.
  2. My plugin uses add_feed(sanitize_title($feed['name']), 'my_plugin_add_feed')) to add the feed.
  3. The my_plugin_add_feed() function will render the feed.

The problem is that my_plugin_add_feed() needs to know which feed it is supposed to show in order to use the proper start and end dates. This information is not available. Currently you need to dig into the query vars to figure it out.

#3 @slimndap
5 years ago

  • Keywords reporter-feedback removed

#4 @johnbillion
5 years ago

  • Milestone changed from Awaiting Review to Future Release

Got it. Thanks for the info.

With dynamic hook names like this, the dynamic portion of the hook name is often passed as an argument to help with situations just like this. For example {type}_template, add_option_{option}, etc.

So, $feed should be added as a parameter to the do_feed_{feed} action. I don't think there's need for a new action here.

@johnbillion
5 years ago

#5 @johnbillion
5 years ago

  • Keywords has-patch added; needs-patch needs-docs removed
  • Milestone changed from Future Release to 4.4

34259.diff adds the $feed parameter and includes the patch on #34264.

#6 @slimndap
5 years ago

Cool. Should add_feed() also be updated?

#7 @johnbillion
5 years ago

Yep. Good catch.

#8 @stevenkword
5 years ago

I definitely prefer adding the parameter to the action instead of creating a new action. This will keep implementation more obvious for future theme developers.

Last edited 5 years ago by stevenkword (previous) (diff)

#9 @SergeyBiryukov
5 years ago

  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#10 @SergeyBiryukov
5 years ago

In 35115:

Feeds: Pass the feed name to do_feed_{$feed} action.

Props johnbillion.
See #34259.

@johnbillion
5 years ago

#11 @SergeyBiryukov
5 years ago

In 35118:

Feeds: Pass the second parameter of "do_feed_{$feed}" action, feed name, to callbacks added via add_feed().

The $feed parameter was added in [35115].

Props slimndap.
See #34259.

#12 @SergeyBiryukov
5 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 35119:

Docs: Describe the $feedname argument of add_feed().

Fixes #34259.

#13 @SergeyBiryukov
5 years ago

In 35121:

Feeds: Remove a confusing comment from add_feed(), added in [3638].

It always overrides the default callback if it's set, regardless of the in_array() check.

See #34259.

Note: See TracTickets for help on using tickets.