Add Admin notice for review of your plugin from users in WordPress | wp admin notice
It’s not good to show the review notice right after user activated the plugin , as to give a review user need to use the plugin for a while ! So we will show the notice let’s say after 1 week, so first what we need to know when user started using the plugin so we will keep data when user first activated the plugin ! Let’s Do it.
// add plugin activation time function void_activation_time(){ $get_activation_time = strtotime("now"); add_option('your_plugin_activation_time', $get_activation_time ); // replace your_plugin with Your plugin name } register_activation_hook( __FILE__, 'void_activation_time' );
What the above code does is whenever use activates the plugin it creates an option name your_plugin_activation_time and set the value to current Unix timestamp.
to know more about strtotime() you can check this link and to know more about the register_activation_hook you can check this link.
To be assured that our code worked you can go to your wordpress dahsboard url/options.php (e.g: http://voidthemes.com/wp-admin/options.php ) and look for your_plugin_activation_time
Now that we are done setting activation time so lets show notice when it’s past 7 days after the plugin was activated.
//check if review notice should be shown or not function void_check_installation_time() { $install_date = get_option( 'your_plugin_activation_time' ); $past_date = strtotime( '-7 days' ); if ( $past_date >= $install_date ) { add_action( 'admin_notices', 'void_display_admin_notice' ); } } add_action( 'admin_init', 'void_check_installation_time' );
the above code will run whenever user accesses dashboard but will only run review notice function which is void_display_admin_notice we will define below only if 7 days passed after installation.
User won’t like if we should the review notice on every admin page ! so will show it only on index.php
/** * Display Admin Notice, asking for a review **/ function void_grid_display_admin_notice() { // WordPress global variable global $pagenow; if( $pagenow == 'index.php' ){ $plugin_info = get_plugin_data( __FILE__ , true, true ); $reviewurl = esc_url( 'https://wordpress.org/support/plugin/'. sanitize_title( $plugin_info['Name'] ) . '/reviews/' ); printf(__('<div class="void-review wrap">You have been using <b> %s </b> for a while. We hope you liked it ! Please give us a quick rating, it works as a boost for us to keep working on the plugin !<div class="void-review-btn"><a href="%s" class="button button-primary" target= "_blank">Rate Now!</a></div></div>', $plugin_info['TextDomain']), $plugin_info['Name'], $reviewurl); } }
You can know more about WordPress global variables here . To know more about get_plugin_data() click here
Though we managed to show review notice but we lack one thing which is we have to give user an option to remove the review notice so that it does not bug the user ever again. Let’s do that.
How we will achieve that is we will also add another link on review notice saying already done and whenever user clicks it we will not bug the user again but of course to do that we will need to pass value via URL to do that.
Below is the final void_grid_display_admin_notice function
/** * Display Admin Notice, asking for a review **/ function void_display_admin_notice() { // wordpress global variable global $pagenow; if( $pagenow == 'index.php' ){ $dont_disturb = esc_url( get_admin_url() . '?spare_me=1' ); $plugin_info = get_plugin_data( __FILE__ , true, true ); $reviewurl = esc_url( 'https://wordpress.org/support/plugin/'. sanitize_title( $plugin_info['Name'] ) . '/reviews/' ); printf(__('<div class="void-review wrap">You have been using <b> %s </b> for a while. We hope you liked it ! Please give us a quick rating, it works as a boost for us to keep working on the plugin !<div class="void-review-btn"><a href="%s" class="button button-primary" target= "_blank">Rate Now!</a><a href="%s" class="void-grid-review-done"> Already Done !</a></div></div>', $plugin_info['TextDomain']), $plugin_info['Name'], $reviewurl, $dont_disturb ); } }
Notice is looking fine now but we have to get the value and set it as an option so that we don’t bug the user again , we can do that with the below code.
// remove the notice for the user if review already done or if the user does not want to function void_spare_me(){ if( isset( $_GET['spare_me'] ) && !empty( $_GET['spare_me'] ) ){ $spare_me = $_GET['spare_me']; if( $spare_me == 1 ){ add_option( 'void_spare_me' , TRUE ); } } } add_action( 'admin_init', 'void_spare_me', 5 );
To make the review notice look clean we might like to use CSS for that , so lets enqueue a stylesheet so that we can do that
//add admin css function void_admin_css(){ global $pagenow; if( $pagenow == 'index.php' ){ wp_enqueue_style( 'void-admin', plugins_url( 'assets/css/void-admin.css', __FILE__ ) ); } } add_action( 'admin_enqueue_scripts', 'void_admin_css' );
I was inspired to write this tutorial by seeing winwar’s article which you can check out here .
One comment
Very interesting subject , appreciate it for putting up.