Per creare un Plug-in bisogna creare una cartella con all’interno un file php (es. my-basics-plugin.php, solitamente il nome del file richiama il nome del plugin) e deve contenere all’inizio un’intestazione specifica come spiegato sul sito https://developer.wordpress.org/plugins/plugin-basics/header-requirements/
esempio:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/** * Plugin Name: My Basics Plugin * Plugin URI: https://example.com/plugins/the-basics/ * Description: Handle the basics with this plugin. * Version: 1.10.3 * Requires at least: 5.2 * Requires PHP: 7.2 * Author: John Smith * Author URI: https://author.example.com/ * License: GPL v2 or later * License URI: https://www.gnu.org/licenses/gpl-2.0.html * Update URI: https://example.com/my-plugin/ * Text Domain: my-basics-plugin * Domain Path: /languages */ // If this file is called directly, abort. if ( ! defined( 'WPINC' ) ) { die; } |
Per aggiungere un menù di settagio creare una funzione e richiamarla con add_action();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
function wpplugin_settings_page() { add_menu_page( 'Plugin Name', //nome plugin e titolo della pagina 'Plugin Menu', // Il testo da utilizzare per il menu 'manage_options', //permessi per visualizzare il menu 'wpplugin', //slug della pagina 'wpplugin_settings_page_markup', //la funzione da richimare 'dashicons-wordpress-alt', //<a href="https://developer.wordpress.org/resource/dashicons/#media-archive" data-type="URL" data-id="https://developer.wordpress.org/resource/dashicons/#media-archive">l'icona da visualizzare</a> 100 //la posizione del menu ); } add_action( 'admin_menu', 'wpplugin_settings_page' ); function wpplugin_settings_page_markup() { // ulteriore controllo di sicurezza if ( !current_user_can('manage_options') ) { return; } ?> <div class="wrap"> <h1><?php esc_html_e( get_admin_page_title() ); ?></h1> <p><?php esc_html_e( 'Some content.', 'wpplugin' ); ?></p> </div> <?php } ?> |
I sottomenù
per aggiungere i sottomenù bisogna usare la funzione add_submenu_page() inserendolo dopo add_menu_page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
add_submenu_page( 'wpplugin', __( 'Plugin Feature 1', 'wpplugin' ), //nome submenu __( 'Feature 1', 'wpplugin' ), //testo da utilizzare per il sottomenu 'manage_options', //permessi per visualizzare il menu 'wpplugin-feature-1', //slug 'wpplugin_settings_subpage_markup' //callback della funzione ); add_action( 'admin_menu', 'wpplugin_default_sub_pages' ); function wpplugin_settings_subpage_markup() { // Double check user capabilities if ( !current_user_can('manage_options') ) { return; } ?> <div class="wrap"> <h1><?php esc_html_e( get_admin_page_title() ); ?></h1> <p><?php esc_html_e( 'Some content.', 'wpplugin' ); ?></p> </div> <?php } |
Oppure si possono aggiungere dei sottomenù ai menù esistenti usando delle specifiche funzioni
1 2 3 4 5 6 7 8 9 10 |
<a href="https://developer.wordpress.org/reference/functions/add_dashboard_page/" data-type="URL" data-id="https://developer.wordpress.org/reference/functions/add_dashboard_page/">add_dashboard_page()</a> <a href="https://developer.wordpress.org/reference/functions/add_posts_page/" data-type="URL" data-id="https://developer.wordpress.org/reference/functions/add_posts_page/">add_posts_page()</a> <a href="https://developer.wordpress.org/reference/functions/add_media_page/" data-type="URL" data-id="https://developer.wordpress.org/reference/functions/add_media_page/">add_media_page()</a> <a href="https://developer.wordpress.org/reference/functions/add_pages_page/" data-type="URL" data-id="https://developer.wordpress.org/reference/functions/add_pages_page/">add_pages_page()</a> <a href="https://developer.wordpress.org/reference/functions/add_comments_page/" data-type="URL" data-id="https://developer.wordpress.org/reference/functions/add_comments_page/">add_comments_page()</a> <a href="https://developer.wordpress.org/reference/functions/add_theme_page/" data-type="URL" data-id="https://developer.wordpress.org/reference/functions/add_theme_page/">add_theme_page()</a> <a href="https://developer.wordpress.org/reference/functions/add_plugins_page/" data-type="URL" data-id="https://developer.wordpress.org/reference/functions/add_plugins_page/">add_plugins_page()</a> <a href="https://developer.wordpress.org/reference/functions/add_users_page/" data-type="URL" data-id="https://developer.wordpress.org/reference/functions/add_users_page/">add_users_page()</a> <a href="https://developer.wordpress.org/reference/functions/add_management_page/" data-type="URL" data-id="https://developer.wordpress.org/reference/functions/add_management_page/">add_management_page()</a> <a href="https://developer.wordpress.org/reference/functions/add_options_page/" data-type="URL" data-id="https://developer.wordpress.org/reference/functions/add_options_page/">add_options_page()</a> |
esempio:
1 2 3 4 5 6 7 8 9 10 11 |
function wpplugin_default_sub_pages() { add_dashboard_page( __( 'Cool Default Sub Page', 'wpplugin' ), //nome submenu __( 'Custom Sub Page', 'wpplugin' ), //testo da utilizzare per il sottomenu 'manage_options', //permessi per visualizzare il menu 'wpplugin-subpage', //slug 'wpplugin_settings_page_markup' //callback della funzione ); } add_action( 'admin_menu', 'wpplugin_default_sub_pages' ); |
Aggiungere un link diretto alla pagina di settaggio
Per aggiungere un link diretto, dopo aver attivato il plugin, nella pagina dei plugin installati, aggiungendo un “Settaggio” o altre impostazioni dirette:
1 2 3 4 5 6 7 |
function wpplugin_add_settings_link( $links ) { $settings_link = '<a href="admin.php?page=wpplugin">' . __( 'Settaggio', 'wpplugin' ) . '</a>'; array_push( $links, $settings_link ); return $links; } $filter_name = "plugin_action_links_" . plugin_basename( __FILE__ ); add_filter( $filter_name, 'wpplugin_add_settings_link' ); |