The get_self_link() WordPress PHP function returns the link for the currently displayed feed.
Usage
To use the function, simply call it like this:
$link = get_self_link();
Parameters
The get_self_link() function has no parameters.
More information
See WordPress Developer Resources: get_self_link()
Examples
Display the self link of the feed
In this example, we display the self link of the current feed.
$link = get_self_link(); echo "The feed's self link is: " . $link;
Add self link to the head section
This example adds the self link to the <head> section of the site using the wp_head action.
function add_self_link_to_head() {
$link = get_self_link();
echo '<link rel="self" href="' . esc_url($link) . '" />';
}
add_action('wp_head', 'add_self_link_to_head');
Creating a custom RSS feed
In this example, we create a custom RSS feed that includes the self link.
header('Content-Type: ' . feed_content_type('rss-http') . '; charset=' . get_option('blog_charset'), true);
echo '<?xml version="1.0" encoding="' . get_option('blog_charset') . '"?' . '>';
?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
<?php do_action('rss2_ns'); ?>>
<channel>
<atom:link href="<?php echo get_self_link(); ?>" rel="self" type="application/rss+xml" />
...
</channel>
</rss>
Customizing the feed link element
In this example, we customize the feed link element to include a custom title attribute using the feed_links_extra action.
function custom_feed_links_extra($feed_type) {
$link = get_self_link();
echo '<link rel="alternate" type="application/rss+xml" title="' . esc_attr(get_bloginfo('name')) . ' - Custom Feed" href="' . esc_url($link) . '" />';
}
add_action('feed_links_extra', 'custom_feed_links_extra');
Displaying the self link in a widget
In this example, we create a custom widget that displays the self link of the current feed.
class Self_Link_Widget extends WP_Widget {
public function __construct() {
parent::__construct('self_link_widget', 'Self Link Widget');
}
public function widget($args, $instance) {
echo $args['before_widget'];
$link = get_self_link();
echo "The feed's self link is: " . $link;
echo $args['after_widget'];
}
}
add_action('widgets_init', function() {
register_widget('Self_Link_Widget');
});