Parte 1

Titoli in questa pagina:
intestazione - attivazione/disattivazione del plugin

Struttura directory

/plugin-name
     plugin-name.php
     uninstall.php
     /languages
     /includes
     /admin
          /js
          /css
          /images
     /public
          /js
          /css
          /images

plugin-name.php

intestazione

/**
 * Plugin Name
 *
 * @package           PluginPackage
 * @author            Your Name
 * @copyright         2019 Your Name or Company Name
 * @license           GPL-2.0-or-later
 *
 * @wordpress-plugin
 * Plugin Name:       Plugin Name
 * Plugin URI:        https://example.com/plugin-name
 * Description:       Description of the plugin.
 * Version:           1.0.0
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            Your Name
 * Author URI:        https://example.com
 * Text Domain:       plugin-slug
 * License:           GPL v2 or later
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Update URI:        https://example.com/my-plugin/
 */

per la sicurezza del plugin controllare le variabili WPINC e ABSPATH

if ( ! defined( 'WPINC' ) ) die; 
if ( ! defined( 'ABSPATH' ) ) exit;

definire la versione

define( 'SETTINGS_PAGE_VERSION', '1.0.0' );

attivazione/disattivazione del plugin

function activate_plugin-name() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-plugin-name-activator.php';
	Plugin_Name_Activator::activate();
}
function deactivate_plugin-name() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-plugin-name-deactivator.php';
	Plugin_Name_Deactivator::deactivate(); 
}
register_activation_hook( __FILE__, 'activate_plugin-name' );
register_deactivation_hook( __FILE__, 'deactivate_plugin-name' );

nei file class-plugin-name-activator.php e class-plugin-name-deactivator.php creare la classe con la relativa funzione:

class Plugin_Name_Activator {
	/**
	 * Short Description. (use period)
	 * Long Description.
	 * @since    1.0.0
	 */
	public static function activate() {
	}
}
class Plugin_Name_Deactivator {
	/**
	 * Short Description. (use period)
	 * Long Description.
	 * @since    1.0.0
	 */
	public static function deactivate() {
	}
}

Includere il motore del plugin class-plugin-name.php nella cartella includes e richiamare la classe principale

function run_plugin_name() {

	$plugin = new plugin_name();
	$plugin->run();

}
add_action( 'init' , 'run_plugin_name' );

Funzioni utili: https://developer.wordpress.org/plugins/plugin-basics/determining-plugin-and-content-directories/

scarica il file di prova

SCRIVI UN COMMENTO