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() { | |
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 */ | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment