WordPress has a built-in widget by default called “Meta”. The functionality of this widget is quite simple. It displays a set of links that change dynamically depending on the user's login status. That is, whether the user is logged in or not. This widget displays the following links — site admin (or registration), login (or logout), post RSS, comment RSS, and .
In one of my projects, I needed to limit the number of links in the menu. I wanted to keep only the first two — registration and login — and remove the other three — RSS + WP — as unnecessary.
I thought it would take five minutes to do. Turned out to be 15)… Fine. To solve this task, I expected WordPress to provide a ready-made filter that could be used to modify or even add links to the Meta widget. But it turns out — there is no such filter.
To address the issue, I had to find the widget’s source code in WordPress core, copy and rename the class and widget ID.
The PHP code of the native meta widget can be found here — wp-includes/widgets/class-wp-widget-meta.php. From there, you can either copy the code into your theme’s functions.php file, or copy the file into your theme directory and include it using the include_once function.
By default, the widget class is named WP_Widget_Meta. To avoid class name conflicts, let’s rename our version to something like My_Widget_Meta.
if statements using class_exists, and use function_exists for functions.Next, in the constructor, replace:
parent::__construct( 'meta', __( 'Meta' ), $widget_ops );
With:
parent::__construct( 'my_meta', 'My Meta', $widget_ops );
The first constructor argument is the widget ID — it must be unique for every widget. Otherwise, you may not see your widget in the admin interface.
Now replace the entire widget() method with the following code, removing the RSS and WordPress.org markup:
public function widget( $args, $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Meta' );
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
?>
<ul>
<?php wp_register(); ?>
<li><?php wp_loginout(); ?></li>
</ul>
<?php
echo $args['after_widget'];
}Save the file, go to “Appearance → Widgets”, and you’ll see the widget available. You can now add it to your desired sidebar and check it on the frontend.
Yes… as they say — good ideas come after the fact. Same here: why didn’t I just extend the WP_Widget_Meta class and override the widget() method and constructor?
