Last active
August 29, 2015 14:02
-
-
Save cgrymala/5222d7a7d543866a1b0e to your computer and use it in GitHub Desktop.
Registering a New Setting in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( ! class_exists( 'Sample_Plugin' ) ) { | |
class Sample_Plugin { | |
var $text_domain = 'sample_plugin'; | |
var $options_page_slug = 'sample-options-page'; /* Because you will use this in a lot of places, we'll define it here */ | |
function __construct() { | |
add_action( 'admin_menu', array( $this, 'admin_menu' ) ); | |
add_action( 'admin_init', array( $this, 'admin_init' ) ); | |
} | |
function admin_menu() { | |
add_options_page( | |
/* $page_title = */__( 'Sample Options Page', $this->text_domain ), /* Will be used as <title> element on page, only */ | |
/* $menu_title = */__( 'Sample Options', $this->text_domain ), /* Will appear as the name within the admin menu */ | |
/* $capability = */'manage_options', /* Be sure to choose a capability that makes sense for your plugin */ | |
/* $menu_slug = */$this->options_page_slug, /* Make this unique, so that no other plugin/theme will clash with it */ | |
/* $callback = */array( $this, 'do_options_page' ) /* This will actually output the HTML for the options page */ | |
); | |
} | |
function admin_init() { | |
/* Whitelist the setting itself */ | |
register_setting( | |
/* $option_group = */$this->options_page_slug, /* For ease of use, use the page slug for this */ | |
/* $option_name = */'my-sample-setting', /* Make this unique to your plugin; it will be used as the key in the database */ | |
/* $callback = */array( $this, 'sanitize_settings' ) /* This is the function that will be used to escape/sanitize the user input */ | |
); | |
/* Add the settings section */ | |
add_settings_section( | |
/* $id = */'sample-settings-section', /* Make this unique to your plugin */ | |
/* $title = */__( 'Sample Settings Section', $this->text_domain ), /* The full text heading for this section */ | |
/* $callback = */array( $this, 'settings_section' ) | |
); | |
/* Add the settings field */ | |
add_settings_field( | |
/* $id = */'sample-settings-field', | |
/* $title = */__( 'Sample Settings Field', $this->text_domain ), | |
/* $callback = */array( $this, 'settings_field' ), | |
/* $page = */$this->options_page_slug, /* Make sure this matches the slug you used when regsitering the page */ | |
/* $section = */'sample-settings-section', /* Make sure this matches the ID you assigned to the section you regsitered */ | |
/* $args = */array( 'label_for' => 'sample-settings-field', 'foo' => 'bar' ) | |
); | |
} | |
/** | |
* Output anything that needs to be at the beginning of our settings section | |
* If you want some introductory text or anything like that, use this callback | |
* Otherwise, this can just return null/void. | |
*/ | |
function settings_section() { | |
return; | |
} | |
/** | |
* Output the settings form field itself | |
* WordPress handles outputting the label and wrappers, etc.; you just | |
* need to output the field (and any notes, extra info you need) | |
*/ | |
function settings_field( $args=array() ) { | |
/* Retrieve the existing values from the database */ | |
$vals = get_option( 'sample-setting', array() ); | |
/* You can output any type of field you want here */ | |
echo '<input type="text" name="settings-field[baz]" id="' . $args['label_for'] . '" value="' . $vals['baz'] . '"/>'; | |
/* If you need to output any additional info, you can do that, too. You can also access any values sent through the args array */ | |
echo '<p style="font-style:italic">The value of the foo argument is: ' . $args['foo'] . '</p>'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment