widget_title
Filters the title of a widget before it's displayed.
add_filter( 'widget_title', $callback, 10, 1 );
Description
Filters the title displayed for a widget. This hook allows developers to modify the widget's title before it's rendered, offering customization for display, translation, or adding dynamic elements. It applies to the title output within the widget rendering process.
Usage
add_filter( 'widget_title', 'your_function_name', 10, 1 );
Parameters
-
$instance(mixed) - This parameter is the title string intended to be displayed for the widget.
Return Value
The filtered value.
Examples
<?php
/**
* Modifies the widget title to prepend a user-specific indicator if they are logged in.
*
* The original 'widget_title' filter is applied within a widget that displays certificates.
* This example demonstrates how to conditionally modify that title based on user login status.
*
* @param string $title The widget title.
* @param array $instance The widget instance settings.
* @return string The modified widget title.
*/
function my_custom_widget_title_filter( $title, $instance ) {
// Check if the user is logged in.
if ( is_user_logged_in() ) {
// Get the current user object.
$current_user = wp_get_current_user();
// Prepend the user's display name to the title for a personalized touch.
$title = sprintf( __( 'Hello %s, your certificates:', 'my-text-domain' ), esc_html( $current_user->display_name ) );
}
// Return the modified (or original) title.
return $title;
}
// Add the filter to the 'widget_title' hook.
// The second parameter '2' indicates that our callback function accepts two arguments: $title and $instance.
add_filter( 'widget_title', 'my_custom_widget_title_filter', 10, 2 );
?>
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/widget-cert.php:133
if ( ! is_user_logged_in() || true !== self::dependants_exist() ) {
return '';
}
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
self::$order = ! empty( $instance['order'] ) ? $instance['order'] : 'ASC';
self::$order_by = ! empty( $instance['order_by'] ) ? $instance['order_by'] : 'title';
/* GET Certificates For Courses*/
$post_args = array(