User Notices
The User Notices plugin has a number of useful hooks for developers to use to further enhance its functionality.
- usernotices_after_dismissing_notice
- usernotices_after_logging_action
- usernotices_after_saving_metakey
- usernotices_save_metakey_value
- usernotices_notice_args
- usernotices_show_notice
- usernotices_content
- usernotices_styles
- usernotices_site_location_types
- usernotices_screen_location_types
- usernotices_audience_types
- usernotices_merge_tags
- usernotices_roles
- usernotices_post_types
- usernotices_ip_lookup_path
usernotices_after_dismissing_notice
TYPE ⇢ Action
This hook is triggered after a user dismisses a notice. It allows developers to perform additional actions when a notice is dismissed, such as triggering analytics events or integrating with third-party systems.
Example Usage
/**
* Send a webhook notification when a notice is dismissed.
*
* @param int $notice_id → The ID of the dismissed notice.
* @param int|null $user_id → The ID of the user who dismissed the notice, or null if guest.
*/
add_action( 'usernotices_after_dismissing_notice', function( $notice_id, $user_id ) {
wp_remote_post( 'https://example.com/webhook', [
'body' => json_encode( [
'notice_id' => $notice_id,
'user_id' => $user_id,
'dismissed_at' => current_time( 'mysql' ),
] ),
'headers' => [ 'Content-Type' => 'application/json' ],
] );
} );
usernotices_after_logging_action
TYPE ⇢ Action
This hook is triggered after a user clicks on a button on the notice.
Example Usage
/**
* Send an email notification when a notice action is logged.
*
* @param int $notice_id → The ID of the notice associated with the action.
* @param string $action → The action performed (The exact button text - case sensitive).
* @param int|null $user_id → The ID of the user who performed the action, or null if guest.
*/
add_action( 'usernotices_after_logging_action', function( $notice_id, $action, $user_id ) {
$admin_email = get_option( 'admin_email' );
$subject = "Notice Action Logged";
if ( $user_id ) {
$message = "User ID {$user_id} performed action '{$action}' on notice ID {$notice_id}.";
} else {
$message = "A guest user performed action '{$action}' on notice ID {$notice_id}.";
}
wp_mail( $admin_email, $subject, $message );
} );
usernotices_after_saving_metakey
TYPE ⇢ Action
This hook is triggered after a meta key is saved for a user. It allows developers to extend functionality when a specific meta key is stored, typically used to track user interactions with notices.
Example Usage
/**
* Log when a meta key is saved for a user.
*
* @param int $notice_id → The ID of the notice associated with the meta key.
* @param string $action → The action that triggered the meta key save.
* @param string $meta_key → The meta key being saved.
* @param int|null $user_id → The ID of the user for whom the meta key is saved, or null if guest.
*/
add_action( 'usernotices_after_saving_metakey', function( $notice_id, $action, $meta_key, $user_id ) {
error_log( "Meta key '{$meta_key}' saved for user ID {$user_id} on notice ID {$notice_id} (Action: {$action})" );
} );
usernotices_save_metakey_value
TYPE ⇢ Filter
By default, the "Meta Key" field on the Action Buttons section saves a TRUE
(1) value to the meta key you provide. This filter allows you to modify the value being saved based on specific requirements.
Example Usage
/**
* Change the user meta key value for notice 999 if the user clicks on "OK"
*
* @param mixed $value → The current value to be saved (default is TRUE).
* @param int $notice_id → The ID of the notice associated with the meta key.
* @param string $action → The action that triggered the meta key save.
* @param string $meta_key → The meta key being saved.
* @param int|null $user_id → The ID of the user for whom the meta key is saved, or null if guest.
*
* @return mixed → Any value that you want
*/
add_filter( 'usernotices_save_metakey_value', function( $value, $notice_id, $action, $meta_key, $user_id ) {
// Check if the notice ID is 999 and the action (button text) is "OK"
if ( $notice_id == 999 && $action == 'OK' ) {
return 'specific_value'; // Replace with the desired value.
}
return $value; // Return the original value for all other cases.
}, 10, 5 );
usernotices_notice_args
TYPE ⇢ Filter
This filter allows developers to modify the notice arguments before they are displayed on the page. It provides an opportunity to add, remove, or change any of the notice data.
Default Values
ID
– The notice ID.title
– The title of the notice.content
– The body content of the notice.status
– The post status of the notice. Defaults:publish
,future
,draft
,pending
,private
buttons
– An array of buttons with their properties; each button has the following properities:text
– The button text.bgcolor
– The background color. Accepts a hex color code including #.color
– The font color. Accepts a hex color code including #.url
– The redirect URL (optional).metakey
– The user meta key (optional).
site_location_type
– The site location type. Defaults:all
,homepage
,post_types
,ids
,shortcode
.site_location
– The specific site location. Forpost_types
, it is a comma-separated string of post-type slugs. Forids
, it is a comma-separated string of post IDs.screen_location_type
– The screen location type. Defaults:top
,above_content
,below_content
,bottom
,modal
,alert
,console
.audience_type
– The audience type. Defaults:everyone
,logged_in
,logged_out
,roles
,users
.audience
– The specific audience. For
, it is a comma-separated string of role slugs. Forroles
, it is a comma-separated string of user IDs, email addresses and/or IP addressesusers
start_date
– The start date and time as a timestamp.end_date
– The end date and time as a timestamp.dismissable
– Whether the notice should allow being dismissed. Accepts:true
orfalse
.hide_after_action
– Whether the notice should be hidden for the user after a button has been clicked. Accepts:true
orfalse
.delay
– The number of seconds it takes for the notice to appear after the page loads. Default:0
.show_user_actions
– Used to determine if the edit screen should look up and display the users that dismissed notices or clicked any buttons. This does not affect notices on the front-end at all. Accepts:true
orfalse
.debug
– Whether the reason why a notice is not displayed should be shown in the developer console. Accepts:true
orfalse
.bg_color
– The background color of the notice. Accepts a hex color code including #.
Example Usage
/**
* Change the background color for notice 999 if the page ID is 333
*
* This filter allows developers to modify the notice data.
*
* @param array $notice_array → An associative array containing the notice data.
* @param int $notice_id → The ID of the notice being retrieved.
*
* @return array → The notice arguments array
*/
add_filter( 'usernotices_notice_args', function( $notice_array, $notice_id ) {
// Change the background color if notice ID is 999 and the page ID is 333
if ( $notice_id === 999 && get_the_ID() == 333 ) {
$notice_array[ 'bg_color' ] = '#FF0000'; // Change to red
}
return $notice_array; // Return the modified notice array.
}, 10, 2 );
usernotices_show_notice
TYPE ⇢ Filter
This filter determines whether a notice should be shown on the page. It allows developers to conditionally hide or show notices based on various criteria, including custom site location types. If the notice should not be shown, a message explaining the reason will be returned instead. Returns true
otherwise.
Example Usage
/**
* Hides a post type-specific notice from page 333.
*
* This filter allows developers to modify the display conditions of a notice.
*
* @param string|bool $should_show_notice → Either `true` if it should be displayed, or a message explaining why it is hidden.
* @param array $notice → The notice data, including meta values that determine its visibility.
*
* @return true|string → TRUE if the notice should show or reason for hiding the notice.
*/
add_filter( 'usernotices_show_notice', function( $should_show_notice, $notice ) {
// Hide the notice if its ID is 999 and the user is on page ID 333
if ( $notice[ 'ID' ] == 999 && get_the_ID() == 333 ) {
return __( 'This notice is hidden on page 333.', 'user-notices' );
}
// Conditions for a custom site location type: taxonomy
// Use the 'usernotices_site_location_types' hook to add the events option to the notice settings
if ( $notice[ 'site_location_type' ] == 'events' && !has_term( 'event', 'category' ) ) {
return __( 'This post does not have the "event" category.', 'user-notices' );
}
return $should_show_notice; // Return the original visibility status or reason.
}, 10, 2 );
usernotices_content
TYPE ⇢ Filter
This filter allows modification of the notice content before it is displayed on the front-end. It can be used to customize the message, add dynamic content, or alter formatting.
Example Usage
/**
* Filter the notice content before display.
*
* @param string $content → The raw content of the notice before processing.
* @param array $notice → The notice data, including meta values that determine its content and appearance.
*
* @return string → The actual content of the arguments not including action buttons.
*/
add_filter( 'usernotices_content', function( $content, $notice ) {
// Append additional text to all notices
$content .= '<p>' . __( 'This is an important notice.', 'user-notices' ) . '</p>';
// Conditionally modify content for a specific notice
if ( $notice['ID'] === 123 ) {
$content = '<strong>' . __( 'Special Announcement:', 'user-notices' ) . '</strong> ' . $content;
}
return $content;
}, 10, 2 );
usernotices_styles
TYPE ⇢ Filter
This filter allows modification of the CSS inline styles applied to a notice before it is displayed. You can also just use CSS to override what's there, but if you're having trouble doing so, it might be helpful to have access to edit the inline styles as well.
Example Usage
/**
* Filters the styles of a user notice.
*
* @param array $styles → An associative array of CSS properties and values for the notice.
* @param string $screen_location_type → The type of screen location where the notice is displayed (types include: 'top', 'above_content', 'below_content', 'bottom', 'modal', 'console').
* @param array $notice → The notice data, including meta values that determine its visibility and behavior.
*
* @return array → The associate array of inline CSS properties
*/
add_filter( 'usernotices_styles', function( $styles, $screen_location_type, $notice ) {
// Target a specific notice
if ( $notice[ 'ID' ] == 999 ) {
if ( $screen_location_type === 'modal' ) {
$styles[ 'padding' ] = '50px';
}
}
// Change the font color for console notices
if ( $screen_location_type === 'console' ) {
$styles[ 'color' ] = '#ff6600';
}
return $styles;
}, 10, 3 );
usernotices_site_location_types
TYPE ⇢ Filter
This filter allows modification of the available site location types where notices can be displayed. Developers can use this to add, modify, or remove location types based on their needs. If you are adding a new location, it should be in conjunction with the usernotices_show_notice
filter to apply the conditions.
Default Values
all
– All Pageshomepage
– Homepage Onlypost_types
– Specific Post Typesids
– Specific Posts & Pagesshortcode
– Shortcode
Example Usage
/**
* Filter the available site location types where notices can be displayed.
*
* @param array $site_locations → An associative array where keys represent location types and values are their labels.
*
* @return array → The modified list of site location types.
*/
add_filter( 'usernotices_site_location_types', function( $site_locations ) {
// Add a new site location type for the events category
// Use the 'usernotices_show_notice' hook to apply conditions
$site_locations[ 'events' ] = __( 'Events', 'user-notices' );
// Rename the "Homepage Only" option
$site_locations[ 'homepage' ] = __( 'Front Page', 'user-notices' );
return $site_locations;
} );
usernotices_screen_location_types
TYPE ⇢ Filter
This filter allows developers to modify the available screen location options for displaying notices. Developers can rename or remove options as needed.
Default Values
top
– Topabove_content
– Above Contentbelow_content
– Below Contentbottom
– Bottommodal
– Pop-Up Modalalert
– Pop-Up Alert (Plain Text Only, Does Not Track)console
– Developer Console (Limited Styling & No Buttons)
Example Usage
/**
* Filter the available screen location options for displaying notices.
*
* @param array $screen_locations → An associative array of screen location types, where keys are the identifiers and values are their labels.
*
* @return array → The modified list of screen location types.
*/
add_filter( 'usernotices_screen_location_types', function( $screen_locations ) {
// Rename the "Pop-Up Modal" option
$screen_locations[ 'modal' ] = __( 'Dialog Box', 'user-notices' );
// Remove the "Developer Console" option
unset( $screen_locations[ 'console' ] );
return $screen_locations;
} );
usernotices_audience_types
TYPE ⇢ Filter
This filter allows developers to modify the available audience types for notices. Developers can rename existing audience types or remove options as needed.
Default Values
everyone
– Everyonelogged_in
– Logged-In Memberslogged_out
– Logged-Out Visitorsroles
– Specific Rolesusers
– Specific Users (By ID, Email, or IP Address)
Example Usage
/**
* Filter the available audience types for notices.
*
* @param array $audience_types → An associative array of audience types, where keys are the identifiers and values are their labels.
*
* @return array → The modified list of audience types.
*/
add_filter( 'usernotices_audience_types', function( $audience_types ) {
// Rename the "Logged-In Members" option
$audience_types[ 'logged_in' ] = __( 'Registered Users', 'user-notices' );
// Remove the "Specific Users" option
unset( $audience_types[ 'users' ] );
return $audience_types;
} );
usernotices_merge_tags
TYPE ⇢ Filter
This filter allows developers to modify the available merge tags that can be used within notice content. Developers can add, rename or remove existing merge tags and their associated keys.
Default Values
User Meta (logged-in users only): {user:}
ID
– IDuser_email
– Emaildisplay_name
– Display Namenickname
– Nicknamefirst_name
– First Namelast_name
– Last Nameuser_registered
– Registered Date
Post Meta (current post only): {post:}
ID
– IDpost_title
– Titlepost_excerpt
– Excerptpost_status
– Statuspost_date
– Published Datepost_type
– Typepost_author
– Author
Dates: {date:}
current
– Current Datecurrent:{format}
– Current Date Formattedstart
– Start Datestart:{format}
– Start Date Formattedend
– End Dateend:{format}
– End Date Formatted
Times: {time:}
current
– Current Timecurrent:{format}
– Current Time Formattedstart
– Start Timestart:{format}
– Start Time Formattedend
– End Timeend:{format}
– End Time Formatted
Merge tags are set up in three groupings (e.g. {first:second:third}
). Each section above is dedicated for the first grouping. The keys within each section are used in the second grouping. The third grouping is dedicated for date and time formats. Formats use PHP date format. For example: n/j/Y
would show a date like 1/1/1970
. The default format for dates is F j, Y
, and for times is g:i A
.
User Meta and Post Meta tags accept any user meta and post meta available, whether default or custom. So if you have a custom meta key called reading_time
on your post, you could use {post:reading_time}
to dynamically populate the value for that key into the notice content.
Example for a user's last name would be {user:last_name}
.
Example for the current date with a specific format: {date:current:m/d/Y}
.
Example Usage
/**
* Filters the available merge tags for notices.
*
* @param array $merge_tags → An associative array of merge tag categories, each containing a label and an array of keys with their corresponding labels.
*
* @return array → The modified list of merge tags.
*/
add_filter( 'usernotices_merge_tags', function( $merge_tags ) {
// Rename the "Display Name" user meta field
$merge_tags[ 'user' ][ 'keys' ][ 'display_name' ] = __( 'Public Name', 'user-notices' );
// Remove the "Nickname" user meta field
unset( $merge_tags[ 'user' ][ 'keys' ][ 'nickname' ] );
// Remove the entire "Times" category
unset( $merge_tags[ 'time' ] );
// Add a custom post meta key for a "Reading Time" field
$merge_tags[ 'post' ][ 'keys' ][ 'reading_time' ] = __( 'Estimated Reading Time', 'user-notices' );
return $merge_tags;
} );
usernotices_roles
TYPE ⇢ Filter
This filter allows developers to modify the list of user roles available for targeting notices. Developers can rename, remove, or add roles to the list.
Example Usage
/**
* Filter the list of user roles available for notice targeting.
*
* @param array $roles → An associative array of role slugs as keys and their translated labels as values.
*
* @return array → The modified list of user roles.
*/
add_filter( 'usernotices_roles', function( $roles ) {
// Rename the "Subscriber" role
if ( isset( $roles[ 'subscriber' ] ) ) {
$roles[ 'subscriber' ] = __( 'Basic Member', 'user-notices' );
}
// Remove the "Editor" role
unset( $roles[ 'editor' ] );
// Add a custom role "VIP Member"
$roles[ 'vip_member' ] = __( 'VIP Member', 'user-notices' );
return $roles;
} );
usernotices_post_types
TYPE ⇢ Filter
This filter allows developers to modify the list of post types available for targeting notices. Developers can rename, remove, or add post types to the list.
Example Usage
/**
* Filter the list of post types available for notice targeting.
*
* @param array $post_types → An associative array of post type slugs as keys and their labels as values.
*
* @return array → The modified list of post types.
*/
add_filter( 'usernotices_post_types', function( $post_types ) {
// Rename the "Page" post type
if ( isset( $post_types[ 'page' ] ) ) {
$post_types[ 'page' ] = __( 'Web Page', 'user-notices' );
}
// Remove the "Post" post type
unset( $post_types[ 'post' ] );
// Add a custom post type "event"
$post_types[ 'event' ] = __( 'Events', 'user-notices' );
return $post_types;
} );
usernotices_ip_lookup_path
TYPE ⇢ Filter
This filter allows developers to modify the URL used for IP address lookups on the Interactions page. Developers can change the base URL or the URL structure to suit their needs. Default is https://www.criminalip.io/asset/report/{ip}
.
Example Usage
/**
* Filter the IP address lookup URL.
*
* @param string $ip_lookup_path → The default IP lookup URL with a placeholder for the IP address.
*
* @return string → The modified IP lookup URL.
*/
add_filter( 'usernotices_ip_lookup_path', function( $ip_lookup_path ) {
// Change the base URL for IP address lookups
$ip_lookup_path = 'https://new-lookup-service.com/report/{ip}';
return $ip_lookup_path;
} );