Dev
Header Requirements
/*
* 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
*/
Si può aggiungere network
License
Consigliano la GPL, inoltre si usa aggiungere un commento ulteriore all’header
/*
{Plugin Name} is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.
{Plugin Name} is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with {Plugin Name}. If not, see {URI to Plugin License}.
*/Attiva/Disattiva hook
register_activation_hook( __FILE__, 'pluginprefix_function_to_run' );
register_deactivation_hook(
__FILE__,
'pluginprefix_function_to_run'
);il primo arg è il fil principale del plugin (quello con header) Si usa comunemente per ==registrare e eliminare nuovi post type==
Disinstalla
Creare un file uninstall.php nella root, controllare per la const WP_UNINSTALL_PLUGIN.
Usato per cancellare le opzioni e le tabelle
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { die; }
$option_name = 'wporg_option';
delete_option( $option_name );
// for site options in Multisite
delete_site_option( $option_name );
// drop a custom database table
global $wpdb;
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}mytable" );Best Pratices & Boilerplates
- usare sempre prefissi
- controllare l’esistenza di cose con
function_exists,class_existsecc - organizzare a cartelle
- Ci sono diversi bolerplate o singoli o a classe, pure wpcli genera una base
wp scaffold plugin lpuk-plugin-name --skip-tests
Dir referency
Per avere il riferimento delle directory WP mette a disposizione diverse funzioni come
plugins_url($path, $plugin)get_stylesheet_directoryValida per temahome_url- ecc
Validation & in/out
Wp mette a disposizione diverse funzioni di
- validation
- sanitation
- escaping
- nonces system (tipo csrf)