programmazione Plugin programmare wordpress
Una pagina dei menu si può dividere in varie sezioni e a sua volta si possono dividere in campi delle impostazioni che può contenere form di settaggio che contiene 3 comandi settings_fields, do_settings_sections e submit_button.
Dopo aver fatto i soliti controlli e le varie definizioni, creeremo un file wpplugin-menus.php e un file wpplugin-settings-fields.php nella cartella includes e andremo ad includerli nel file principale del plugin, vediamo un esempio:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
if ( ! defined( 'WPINC' ) ) { die; } // Define plugin paths and URLs define( 'mioplugin_URL', plugin_dir_url( __FILE__ ) ); define( 'mioplugin_DIR', plugin_dir_path( __FILE__ ) ); define( 'MIOPLUGIN_BASENAME', plugin_basename( __FILE__ )); // Create Settings Fields include( plugin_dir_path( __FILE__ ) . 'includes/mioplugin-settings-fields.php'); // Create Plugin Admin Menus and Setting Pages include( plugin_dir_path( __FILE__ ) . 'includes/mioplugin-menus.php'); ?> |
Nel file wpplugin-settings-fields.php creeremo una funzione, mioplugin_settings, controlleremo se l’opzione è stata aggiunta o no, utilizzeremo add_settings_section, per creare una sezione, per ogni sezione bisogna avere un nome univoco. utilizzare add_settings_field per definire il campo delle impostazioni, invocare tramite callback una funzione, in questo caso mioplugin_settings_custom_text_callback, che serve per fare il markup del campo delle impostazioni.
Registrare tramite register_setting i settaggi.
infine aggiungeremo la nostra azione trmite 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<?php function mioplugin_settings() { // If plugin settings don't exist, then create them if( !get_option( 'mioplugin_settings' ) ) { add_option( 'mioplugin_settings' ); } // Define (at least) one section for our fields add_settings_section( // Unique identifier for the section 'mioplugin_settings_section', // Section Title __( 'A Plugin Settings Section', 'mio-plug-in' ), // Callback for an optional description 'mioplugin_settings_section_callback', // slug name, Admin page to add section to 'mioplugin' ); add_settings_field( // Unique identifier for field 'mioplugin_settings_custom_text', // Field Title __( 'Custom Text', 'mioplugin'), // Callback for field markup 'mioplugin_settings_custom_text_callback', // Page to go on 'mioplugin', // Section to go in 'mioplugin_settings_section' ); register_setting( 'mioplugin_settings', 'mioplugin_settings' ); } add_action( 'admin_init', 'mioplugin_settings' ); function mioplugin_settings_section_callback() { esc_html_e( 'Plugin settings section description', 'mioplugin' ); } function mioplugin_settings_custom_text_callback() { $options = get_option( 'mioplugin_settings' ); $custom_text = ''; if( isset( $options[ 'custom_text' ] ) ) { $custom_text = esc_html( $options['custom_text'] ); } echo '<input type="text" id="mioplugin_customtext" name="mioplugin_settings[custom_text]" value="' . $custom_text . '" />'; |
per utilizzare le pagine integrate modificare lo slugname della pagina in add_settings_field con uno dei seguenti termini: ‘general’, ‘reading’, ‘writing’, ‘discussion’, ‘media’, etc, prendiamo come esempio ‘general’ e la sezione impostarla su ‘default’. Registrare il settaggio tramite register_setting impostando il gruppo su ‘general’, troveremo la nostra sezione di opzioni nella pagina impostazioni di wordpress
Per visualizzare il tutto, bisogna creare, nel file /admin/settings-page.php un form e usare i seguenti comandi settings_fields, do_settings_sections e submit_button.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<div class="wrap"> <h1><?php esc_html_e( get_admin_page_title() ); ?></h1> <form method="post" action="options.php"> <!-- Display necessary hidden fields for settings --> <?php settings_fields( 'mioplugin_settings' ); ?> <!-- Display the settings sections for the page --> <?php do_settings_sections( 'mioplugin' ); ?> <!-- Default Submit Button --> <?php submit_button(); ?> </form> </div> |
per la creazione del menu andare al capitolo 7 alla funzione mioplugin-settings-pages
Una risposta a “Capitolo 8 Api salvataggio opzioni”
[…] a modificare il file, mioplugin-settings-fields.php già trattato nel capitolo 8, andremo ad aggiungere svariati campi di setaggio con la funzione add_setting_field e le relative […]