programmazione Plugin programmare wordpress
Creare una cartella, ad esempio 6.20-more-settings-fields, che contiene i file per il plugin ed inserirla nel precorso wp-content/plugins della cartella principale di wordpress.
Percorso completo:
wp-content/plugins/6.20-more-settings-fields
File principale del plugin
creaiamo un file principale del plugin con lo stesso nome della cartella, ad esempio 6.20-more-settings-fields.php
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 |
<?php /* Plugin Name: 6.20 - More Setting Fields Plugin URI: https://mstc.sytes.net/category/wordpress/programmazione-wordpress/programmazione-plugin-wordpress/ Description: Demo plugin for learning how to add multiple settings fields. Version: 1.0.0 Contributors: musty Author: Luca Mustacchio Author URI: https://zacgordon.com License: GPLv2 or later License URI: https://mstc.sytes.net/curriculum-vitae/ Text Domain: mio-plug-in Domain Path: /languages */ // If this file is called directly, abort. 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'); ?> |
Creazione del menù
Creiamo nella cartella principale del plugin una cartella includes e creiamo il file mioplugin-menus.php
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 |
<?php function mioplugin_settings_page_markup() { // Double check user capabilities if ( !current_user_can('manage_options') ) { return; } include( MIOPLUGIN_DIR . 'templates/admin/settings-page.php'); } function mioplugin_settings_pages() { add_menu_page( __( 'Practice Plugin', 'mio-plug-in' ), __( 'Practice', 'mio-plug-in' ), 'manage_options', 'mioplugin', 'mioplugin_settings_page_markup', 'dashicons-screenoptions', 100 ); } add_action( 'admin_menu', 'mioplugin_settings_pages' ); // Add a link to your settings page in your plugin function mioplugin_add_settings_link( $links ) { $settings_link = '<a href="admin.php?page=mioplugin">' . __( 'Settings' ) . '</a>'; array_push( $links, $settings_link ); return $links; } $filter_name = "plugin_action_links_" . MIOPLUGIN_BASENAME; add_filter( $filter_name, 'mioplugin_add_settings_link' ); |
Creaiamo nella directory principale del plugin le cartelle templates/admin/ e il file settings-page.php.
Percorso completo:
templates/admin/settings-page.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<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> |
Pagina di setup
Creaiamo nella cartella includes il file mioplugin-settings-fields.php.
Percorso completo:
includes/mioplugin-settings-fields.php
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
<?php function mioplugin_settings() { // If plugin settings don't exist, then create them if( false == 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 __( 'Plugin Settings Section', 'mio-plug-in' ), // Callback for an optional description 'mioplugin_settings_section_callback', // Admin page to add section to 'mioplugin' ); // Input Text Field add_settings_field( // Unique identifier for field 'mioplugin_settings_input_text', // Field Title __( 'Text Input', 'mioplugin'), // Callback for field markup 'mioplugin_settings_text_input_callback', // Page to go on 'mioplugin', // Section to go in 'mioplugin_settings_section' ); // Textarea Field add_settings_field( 'mioplugin_settings_textarea', __( 'Text Area', 'mioplugin'), 'mioplugin_settings_textarea_callback', 'mioplugin', 'mioplugin_settings_section' ); // Checkbox Field add_settings_field( 'mioplugin_settings_checkbox', __( 'Checkbox', 'mioplugin'), 'mioplugin_settings_checkbox_callback', 'mioplugin', 'mioplugin_settings_section', [ 'label' => 'Checkbox Label' ] ); // Radio Field add_settings_field( 'mioplugin_settings_radio', __( 'Radio', 'mioplugin'), 'mioplugin_settings_radio_callback', 'mioplugin', 'mioplugin_settings_section', [ 'option_one' => 'Radio 1', 'option_two' => 'Radio 2' ] ); // Dropdown add_settings_field( 'mioplugin_settings_select', __( 'Select', 'mioplugin'), 'mioplugin_settings_select_callback', 'mioplugin', 'mioplugin_settings_section', [ 'option_one' => 'Select Option 1', 'option_two' => 'Select Option 2', 'option_three' => 'Select Option 3' ] ); register_setting( 'mioplugin_settings', 'mioplugin_settings' ); } add_action( 'admin_init', 'mioplugin_settings' ); function mioplugin_settings_section_callback() { esc_html_e( 'Plugin settings section description', 'mio-plug-in' ); } function mioplugin_settings_text_input_callback() { $options = get_option( 'mioplugin_settings' ); $text_input = ''; if( isset( $options[ 'text_input' ] ) ) { $text_input = esc_html( $options['text_input'] ); } echo '<input type="text" id="mioplugin_customtext" name="mioplugin_settings[text_input]" value="' . $text_input . '" />'; } function mioplugin_settings_textarea_callback() { $options = get_option( 'mioplugin_settings' ); $textarea = ''; if( isset( $options[ 'textarea' ] ) ) { $textarea = esc_html( $options['textarea'] ); } echo '<textarea id="mioplugin_settings_textarea" name="mioplugin_settings[textarea]" rows="5" cols="50">' . $textarea . '</textarea>'; } function mioplugin_settings_checkbox_callback( $args ) { $options = get_option( 'mioplugin_settings' ); $checkbox = ''; if( isset( $options[ 'checkbox' ] ) ) { $checkbox = esc_html( $options['checkbox'] ); } $html = '<input type="checkbox" id="mioplugin_settings_checkbox" name="mioplugin_settings[checkbox]" value="1"' . checked( 1, $checkbox, false ) . '/>'; $html .= ' '; $html .= '<label for="mioplugin_settings_checkbox">' . $args['label'] . '</label>'; echo $html; } function mioplugin_settings_radio_callback( $args ) { $options = get_option( 'mioplugin_settings' ); $radio = ''; if( isset( $options[ 'radio' ] ) ) { $radio = esc_html( $options['radio'] ); } $html = '<input type="radio" id="mioplugin_settings_radio_one" name="mioplugin_settings[radio]" value="1"' . checked( 1, $radio, false ) . '/>'; $html .= ' <label for="mioplugin_settings_radio_one">'. $args['option_one'] .'</label> '; $html .= '<input type="radio" id="mioplugin_settings_radio_two" name="mioplugin_settings[radio]" value="2"' . checked( 2, $radio, false ) . '/>'; $html .= ' <label for="mioplugin_settings_radio_two">'. $args['option_two'] .'</label>'; echo $html; } function mioplugin_settings_select_callback( $args ) { $options = get_option( 'mioplugin_settings' ); $select = ''; if( isset( $options[ 'select' ] ) ) { $select = esc_html( $options['select'] ); } $html = '<select id="mioplugin_settings_options" name="mioplugin_settings[select]">'; $html .= '<option value="option_one"' . selected( $select, 'option_one', false) . '>' . $args['option_one'] . '</option>'; $html .= '<option value="option_two"' . selected( $select, 'option_two', false) . '>' . $args['option_two'] . '</option>'; $html .= '<option value="option_three"' . selected( $select, 'option_three', false) . '>' . $args['option_three'] . '</option>'; $html .= '</select>'; echo $html; } |