WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 13 months ago

Last modified 12 months ago

#23692 closed enhancement (fixed)

feed_links should have a parameter to choose which feed to display

Reported by: Confridin Owned by: SergeyBiryukov
Milestone: 4.4 Priority: normal
Severity: minor Version:
Component: Feeds Keywords: has-patch needs-docs good-first-bug
Focuses: docs Cc:

Description

Hello,

feed_links is a function located in wp_includes/general_template.php. It displays 2 feeds in the header : the main feed and the comment feed. The function is activated along with feed_links_extra when automatic-feed-links support is activated.

Currently, developers can't choose if they want both feeds to be displayed, or just one of them. I think this function should be improved.

Here is the current function :

function feed_links( $args = array() ) {
	if ( !current_theme_supports('automatic-feed-links') )
		return;

	$defaults = array(
		/* translators: Separator between blog name and feed type in feed links */
		'separator'	=> _x('»', 'feed link'),
		/* translators: 1: blog title, 2: separator (raquo) */
		'feedtitle'	=> __('%1$s %2$s Feed'),
		/* translators: %s: blog title, 2: separator (raquo) */
		'comstitle'	=> __('%1$s %2$s Comments Feed'),
	);

	$args = wp_parse_args( $args, $defaults );

	echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr(sprintf( $args['feedtitle'], get_bloginfo('name'), $args['separator'] )) . '" href="' . get_feed_link() . "\" />\n";
	echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr(sprintf( $args['comstitle'], get_bloginfo('name'), $args['separator'] )) . '" href="' . get_feed_link( 'comments_' . get_default_feed() ) . "\" />\n";
}

Currently, the only way to display only one feed (the main post feed or the main comment feed) so is to deactivate feed_links with :

remove_action('wp_head', 'feed_links', 2);

and then to create our own function to only display one feed.

Do you think this should be improved ?

Regards,

Attachments (5)

general-template.patch (1.4 KB) - added by joostdevalk 2 years ago.
Patch
23692.patch (2.1 KB) - added by juliobox 2 years ago.
My patch :)
23963.diff (1.5 KB) - added by wonderboymusic 13 months ago.
23692.2.diff (1.7 KB) - added by joostdevalk 13 months ago.
Updated version of earlier general-template patch
23692.3.diff (1.7 KB) - added by SergeyBiryukov 13 months ago.

Download all attachments as: .zip

Change History (24)

#1 @alexvorn2
4 years ago

to improve:

  1. to choose what to show: blog feed or/and comments feed
  1. to include a filter for each feed: maybe someone wants a custom feed like a feedburner url...
Last edited 4 years ago by alexvorn2 (previous) (diff)

#2 @alexvorn2
4 years ago

  • Keywords needs-patch added

#3 @Confridin
4 years ago

I think it could be improved this way :

// Default :
add_theme_support( 'automatic-feed-links');
// only main feed
add_theme_support( 'automatic-feed-links', true, false );
// only comment feed
add_theme_support( 'automatic-feed-links', false, true);
// new url for main feed
add_theme_support( 'automatic-feed-links', 'http://website.com/new-feed-url', true);
// new url for comment feed
add_theme_support( 'automatic-feed-links', true, 'http://website.com/new-feed-url');

What do you think ?

#4 @juliobox
4 years ago

Hello i propose a simple filter :

function feed_links( $args = array() ) {
	if ( !current_theme_supports('automatic-feed-links') )
		return;

	$defaults = array(
		/* translators: Separator between blog name and feed type in feed links */
		'separator'	=> _x('&raquo;', 'feed link'),
		/* translators: 1: blog title, 2: separator (raquo) */
		'feedtitle'	=> __('%1$s %2$s Feed'),
		/* translators: %s: blog title, 2: separator (raquo) */
		'comstitle'	=> __('%1$s %2$s Comments Feed'),
	);

	$args = wp_parse_args( $args, $defaults );
	$feed_and_coms = apply_filters( 'feed_links_types', 3 ); // 1 = feed, 2 = coms, 3 = both
	if( $feed_and_coms==1 || $feed_and_coms==3 )
		echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr(sprintf( $args['feedtitle'], get_bloginfo('name'), $args['separator'] )) . '" href="' . get_feed_link() . "\" />\n";
	if( $feed_and_coms==2 || $feed_and_coms==3 )
	echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr(sprintf( $args['comstitle'], get_bloginfo('name'), $args['separator'] )) . '" href="' . get_feed_link( 'comments_' . get_default_feed() ) . "\" />\n";
}

To show only feed :

add_filter( 'feed_links_types', 'baw_show_only_feed' );
function baw_show_only_feed(){
  return 1;
}

To show only coms :

add_filter( 'feed_links_types', 'baw_show_only_coms' );
function baw_show_only_feed(){
  return 2;
}

To show both, do nothing ;)

Last edited 4 years ago by SergeyBiryukov (previous) (diff)

#5 @Confridin
4 years ago

Great solution Julio ;)

#6 follow-up: @SergeyBiryukov
4 years ago

The filter makes sense, however the values to check should be actual words (I'd suggest 'posts', 'comments', and 'all'), rather than numbers.

#7 in reply to: ↑ 6 @nacin
3 years ago

Replying to SergeyBiryukov:

The filter makes sense, however the values to check should be actual words (I'd suggest 'posts', 'comments', and 'all'), rather than numbers.

Or it should filter an actual array of arguments, so one could set 'comments' => false. Or each feed can have its own filter.

#8 @juliobox
3 years ago

  • Cc juliobosk@… added

@nacin : yes much better to filter an $args var. So, maybe 3.7 ? :]

@joostdevalk
2 years ago

Patch

#9 @joostdevalk
2 years ago

  • Keywords has-patch added; needs-patch removed
  • Owner set to joostdevalk
  • Status changed from new to assigned

Just added a patch, with each feed having its own filter. Would appreciate a review :)

@juliobox
2 years ago

My patch :)

#10 @jrf
23 months ago

My two pennies: I'd prefer Joost's patch as it'll be simpler in practical usage. See the below comparison of code needed to disable the comments feed.

Joost's patch:

add_filter( 'automatic_feed_links_show_comments_feed', '__return_false' );

Julio's patch:

add_filter( 'feed_links_types', 'my_feed_filter_function' );
function my_feed_filter_function( $array ) {
	$array['comments'] = false;
	return $array;
}

#11 @scamartist26
13 months ago

I agree with the patch submitted by @joostdevalk.

#12 @SergeyBiryukov
13 months ago

  • Focuses docs added
  • Keywords needs-patch added; has-patch removed
  • Milestone changed from Awaiting Review to 4.4

New filters need to be documented as per the documentation standards.

Last edited 12 months ago by SergeyBiryukov (previous) (diff)

#13 @wonderboymusic
13 months ago

  • Keywords has-patch needs-docs good-first-bug added; dev-feedback needs-patch removed
  • Owner joostdevalk deleted

#14 @juliobox
13 months ago

23963.diff​ on the #23962 ticket, let's play a game x)
So my solution was pretty fine? Great news. You just changed the array type from keys to indexes, ok fine too.

Thank you for approving this and thanks to Daniel for his idea!

#15 @SergeyBiryukov
13 months ago

I actually think general-template.patch would be easier to use, as demonstrated in comment:10.

@joostdevalk
13 months ago

Updated version of earlier general-template patch

#16 @joostdevalk
13 months ago

Added a documented version of my earlier patch, still think that's the way to go.

#17 @scamartist26
13 months ago

I still favor the filter @joostdevalk offers in this scenario. An obvious solution for most devs. The code abides.

#18 @SergeyBiryukov
13 months ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from assigned to closed

In 33838:

Add filters to feed_links() to choose whether to display the links to posts feed and comments feed, separately.

props joostdevalk, juliobox.
fixes #23692.

#19 @SergeyBiryukov
13 months ago

In 33839:

Correct @since version for [33838].

props JustinSainton.
see #23692.

Note: See TracTickets for help on using tickets.