I'm wondering if that's possible and how : I would like to show a popup only to a certain group of users. I've looked into the plugin directory but didn't find anything that fits my needs. Can someone help ?
Thank you !
The way I'd approach this problem is to give a certain role to users you want to be shown the popup. ( If you're looking to show the popup to different users that are logged out of WordPress, that's out of scope for this StackExchange. )
I'd then use the wp_loaded
hook to check the user role of the logged in user. If that user was a member of the role that I wanted to show the popup to, then I'd add hook to display the popup and enqueue the necessary CSS and javascript.
namespace StackExchange\WordPress;
\add_action( 'wp_loaded', __NAMESPACE__ . '\wp_loaded' );
function wp_loaded() {
$role = 'author';
if( ! is_current_user_in_role( $role ) ) {
return;
}
\add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\wp_enqueue_scripts' );
\add_action( 'wp_footer', __NAMESPACE__ . '\wp_footer' );
}
function wp_footer() { ?>
<div class="hidden or-some-other-class-that-wont-be-shown">
<!-- Some other HTML code -->
</div>
<?php }
function wp_enqueue_scripts() {
//* Enqueue the CSS that will initially hide the hidden div
//* Or it could be in another stylesheet already enqueued
\wp_enqueue_style( 'name-of-style', PATH_TO . 'style-name.css' );
//* In the javascript file, pop up the hidden div
\wp_enqueue_script( 'name-of-script', PATH_TO . 'script-name.js' );
}
function is_current_user_in_role( $role ) {
return in_array( $role, (array) \wp_get_current_user()->roles )
}
is_current_user_in_role()
based on this answer by Rarst