automator_notification_content_display
Filters the content of notifications displayed within the Automator plugin.
add_filter( 'automator_notification_content_display', $callback, 10, 1 );
Description
This filter hook allows developers to modify the content of notifications before they are displayed. It fires after WordPress's `wpautop` function has been applied, meaning the content is already formatted with paragraphs. Developers can use this hook to further manipulate the HTML, add custom elements, or remove unwanted content from notification messages. It's important to note that this hook operates on the notification content itself, not the entire notification array.
Usage
add_filter( 'automator_notification_content_display', 'your_function_name', 10, 1 );
Parameters
-
$notifications(mixed) - This parameter contains an array of notifications, each with a 'content' key, which is then processed to format its output.
Return Value
The filtered value.
Examples
<?php
/**
* Example: Sanitize and shorten notification content for display.
*
* This filter hook is applied to the notification content after it has been
* processed by wpautop. This example demonstrates how to further sanitize
* the content by removing specific HTML tags and then truncating it
* if it exceeds a certain length, adding an ellipsis.
*/
add_filter( 'automator_notification_content_display', function( $content ) {
// Remove potentially harmful or unwanted HTML tags.
// Be cautious with this - ensure you're not removing necessary formatting.
$allowed_tags = '<p><a><strong><em><u>'; // Example: allow paragraphs, links, bold, italics, underline
$sanitized_content = strip_tags( $content, $allowed_tags );
// Define a maximum length for the notification content.
$max_length = 150;
// If the sanitized content is longer than the max length, truncate it.
if ( strlen( $sanitized_content ) > $max_length ) {
$truncated_content = substr( $sanitized_content, 0, $max_length );
// Add ellipsis if truncated.
$display_content = $truncated_content . '...';
} else {
$display_content = $sanitized_content;
}
// Return the modified content.
return $display_content;
}, 10, 1 ); // Priority 10, accepts 1 argument
?>
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/core/admin/notifications/notifications.php:314
public function get_notifications_with_formatted_content( $notifications ) {
if ( ! is_array( $notifications ) || empty( $notifications ) ) {
return $notifications;
}
foreach ( $notifications as $key => $notification ) {
if ( ! empty( $notification['content'] ) ) {
$notifications[ $key ]['content'] = wpautop( $notification['content'] );
$notifications[ $key ]['content'] = apply_filters( 'automator_notification_content_display', $notifications[ $key ]['content'] );
}
}
return $notifications;
}