Capitolo 10 Plugin Completo

Titoli in questa pagina:
File principale del plugin - Creazione del menù - Pagina di setup

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

<?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

<?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

<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

<?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 .= '&nbsp;';
	$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> &nbsp;';
	$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;

}

file zip contenente il plugin

SCRIVI UN COMMENTO