Skip to content

Instantly share code, notes, and snippets.

@alessandrotesoro
Last active June 8, 2016 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alessandrotesoro/2e916f5c7d16ff35ceb9ddb841e98b97 to your computer and use it in GitHub Desktop.
Save alessandrotesoro/2e916f5c7d16ff35ceb9ddb841e98b97 to your computer and use it in GitHub Desktop.
WP Tidy Settings test plugin
<?php
namespace TDS;
if ( ! defined( 'ABSPATH' ) ) exit;
class Settings_Test extends Settings_Page {
public function __construct() {
$this->id = 'test';
$this->label = 'Test page';
$this->plugin_prefix = 'test';
$this->init();
}
public function init() {
add_filter( $this->plugin_prefix . '_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
add_action( $this->plugin_prefix . '_settings_sections_' . $this->id, array( $this, 'output_sections' ) );
add_action( $this->plugin_prefix . '_settings_' . $this->id, array( $this, 'output' ) );
add_action( $this->plugin_prefix . '_settings_save_' . $this->id, array( $this, 'save' ) );
}
public function get_sections() {
$sections = array(
'' => __( 'Section 1' ),
'test' => __( 'Section 2' ),
);
return $sections;
}
public function get_settings() {
$section = $this->get_current_section();
if( $section == 'test' ) {
$settings = array(
array(
'label' => 'Text Field',
'name' => 'test_field_new',
'desc' => 'this is the description of this field',
'type' => 'text',
),
);
} else {
$settings = array(
array(
'label' => 'General Options',
'type' => 'title',
),
array(
'label' => 'Text Field',
'name' => 'test_field',
'desc' => 'this is the description of this field',
'type' => 'text',
),
array(
'label' => 'Text Field',
'name' => 'test_field_select2',
'desc' => 'this is the description of this field',
'type' => 'select',
'options' => array( 'test' => 't', 'test2' => 't2' )
),
array(
'label' => 'Text Field',
'name' => 'test_field_radio2',
'desc' => 'this is the description of this field',
'type' => 'radio',
'options' => array( 'test' => 't', 'test2' => 't2' )
),
array(
'label' => 'Textarea Field',
'name' => 'test_field2',
'desc' => 'this is the description of this field',
'type' => 'textarea',
'attributes' => array( 'rows' => 10, 'cols' => 50 )
),
array(
'label' => 'Text Field',
'name' => 'test_field33',
'desc' => 'this is the description of this field',
'type' => 'number',
),
array(
'label' => 'Text Field',
'name' => 'test_field_check',
'desc' => 'this is the description of this field',
'type' => 'checkbox',
),
);
}
return $settings;
}
public function save() {
$settings = $this->get_settings();
call_user_func( array( __NAMESPACE__ .'\\Settings_Manager', 'save_fields' ), $settings );
}
}
return new Settings_Test();
<?php
require_once( plugin_dir_path( __FILE__ ) .'wp-tidy-settings.php' );
class WP_Tidy_Settings_Test {
public $settings_api;
function __construct() {
$this->settings_api = new TDS\Settings_Manager( array(
'option_name' => 'tds_test',
'plugin_prefix' => 'test',
'plugin_version' => '1.0.0',
'first_page_id' => 'test',
'panel_title' => 'Options Panel Test'
) );
$this->settings_api->set_panel_url( 'options-general.php?page=settings_api_test' );
$this->settings_api->add_settings_pages( $this->get_settings_pages() );
add_action( 'admin_menu', array($this, 'admin_menu') );
add_action( 'admin_enqueue_scripts', array( $this, 'styles') );
}
function admin_menu() {
add_options_page( 'Settings API', 'Settings API', 'manage_options', 'settings_api_test', array( $this, 'plugin_page' ) );
}
function get_settings_pages() {
$pages = array();
$pages[] = include( 'class-settings-test.php' );
return $pages;
}
function styles() {
$this->settings_api->enqueue_styles();
}
function plugin_page() {
$this->settings_api->output();
}
}
new WP_Tidy_Settings_Test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment