Come aggiungere un invito alla recensione nel footer del plugin WordPress

Hai sviluppato il plugin WordPress e vuoi aumentare le recensioni sul repository WP.org?

Un modo semplice e non invasivo per stimolare gli utenti a lasciare la propria recensione è aggiungere un piccolo messaggio nel footer delle pagine di amministrazione del tuo plugin.

In questa guida vedremo come inserire nel footer del plugin il messaggio:

Enjoying plugin name? Please rate ★★★★★ on WordPress.org to help us spread the word. Thank you!

Ecco il mio risultato:

footer in admin page Was This Helpful Settings WordPress

Come intercettare il footer in WordPress Admin

WordPress offre un hook chiamato admin_footer_text che consente di modificare o aggiungere contenuti al footer della dashboard di amministrazione.

Noi vogliamo aggiungere il messaggio solamente nelle pagine di amministrazione del plugin, per cui dovremo usare anche get_current_screen() per capire quando siamo nelle pagine che ci interessano.

Codice da inserire nel plugin

Ho usato questo codice nel mio plugin Was This Helpful? scritto in OOP e non in maniera procedurale, quindi devi racchiuderlo nella tua classe o adattarlo al tuo codice.

class Nome_Classe {

		public function __construct() {
			// Admin footer text.
			add_filter( 'admin_footer_text', array( $this, 'admin_footer' ), 1, 2 );
		}

		/**
		 * When user is on admin page, display footer text
		 * that graciously asks them to rate us.
		 *
		 * @since 2.1.1
		 *
		 * @param string $text Current footer text.
		 *
		 * @return string
		 */
		public function admin_footer( $text ) {
			global $current_screen;

			if ( ! empty( $current_screen->id ) && $this->is_plugin_link_page() ) {
				$url  = 'https://wordpress.org/support/plugin/your-plugin-slug/reviews/?filter=5#new-post';
				$text = sprintf(
					wp_kses(
					/* translators: $1$s - WP.org review link; $2$s - WP.org review link. */
						__( 'Enjoying <strong>Your Plugin Name</strong>? Please rate <a href="%1$s" target="_blank" rel="noopener noreferrer">&#9733;&#9733;&#9733;&#9733;&#9733;</a> on <a href="%2$s" target="_blank" rel="noopener">WordPress.org</a> to help us spread the word. Thank you!', 'your-plugin-text-domain' ),
						array(
							'a'      => array(
								'href'   => array(),
								'target' => array(),
								'rel'    => array(),
							),
							'strong' => array(),
						)
					),
					$url,
					$url
				);
			}

			return $text;
		}

		/**
		 * Get the current screen ID.
		 *
		 * @since 1.0.0
		 *
		 * @param string|null $hook The screen hook. Default null.
		 * @return string The screen ID.
		 */
		private function get_screen_id( $hook = null ) {
			if ( is_null( $hook ) ) {
				$screen = get_current_screen();
				$hook   = $screen->id;
			}

			return $hook;
		}

		/**
		 * Check if we are on the plugin settings page.
		 *
		 * @since 1.0.0
		 *
		 * @return bool
		 */
		public function is_plugin_link_page() {
			$hook = $this->get_screen_id();
			// List of pages to check
			$plugin_pages = array(
				'your-plugin-admin-page-slug-1',
				'your-plugin-admin-page-slug-2',
			);
			foreach ( $plugin_pages as $page ) {
				if ( strpos( $hook, $page ) !== false ) {
					return true;
				}
			}

			return false;
		}
	}

Spieghiamo brevemente il codice:

admin_footer_text è il filtro che permette di modificare il testo del footer.

get_current_screen() è usato per controllare su quale schermata ci troviamo, così il messaggio appare solo nelle pagine del tuo plugin (e non ovunque nella dashboard).

In $plugin_pages ho aggiunto le pagine come array, quindi puoi inserirne una sola o più di una.

$plugin_pages = array(
	'your-plugin-admin-page-slug-1',
	'your-plugin-admin-page-slug-2',
);

$url è l’indirizzo a cui punteranno i link per lasciare la recensione. Cambia il nome dello slug del tuo plugin.

$url  = 'https://wordpress.org/support/plugin/your-plugin-slug/reviews/?filter=5#new-post';

Personalizza il messaggio nella sprintf:

$text = sprintf(
					wp_kses(
					/* translators: $1$s - WP.org review link; $2$s - WP.org review link. */
						__( 'Enjoying <strong>Was This Helpful</strong>? Please rate <a href="%1$s" target="_blank" rel="noopener noreferrer">&#9733;&#9733;&#9733;&#9733;&#9733;</a> on <a href="%2$s" target="_blank" rel="noopener">WordPress.org</a> to help us spread the word. Thank you! – Roberto Iacono', 'riaco-was-this-helpful' ),
						array(
							'a'      => array(
								'href'   => array(),
								'target' => array(),
								'rel'    => array(),
							),
							'strong' => array(),
						)
					),
					$url,
					$url
				);

Mantieni il tono amichevole e positivo.

Fai passare il messaggio che la recensione è un modo per supportare il plugin in maniera gratuita, aumenterai la probabilità che l’utente lasci davvero una recensione.

E se hai qualche minuto, supporta i miei plugin.

Sponsor

Vhosting
themeforest