Created
February 7, 2016 01:05
-
-
Save 21stcenturyblogger/94678f4af9444be7883f to your computer and use it in GitHub Desktop.
adding options to wp settings api
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 | |
/* Nate field is not saving a value. Or possible updating the value? Any ideas? | |
/** | |
* The admin-specific functionality of the plugin. | |
* | |
* @link http://waynebrett.com | |
* @since 1.0.0 | |
* | |
* @package Developer_Branding | |
* @subpackage Developer_Branding/admin | |
*/ | |
/** | |
* The admin-specific functionality of the plugin. | |
* | |
* Defines the plugin name, version, and two examples hooks for how to | |
* enqueue the admin-specific stylesheet and JavaScript. | |
* | |
* @package Developer_Branding | |
* @subpackage Developer_Branding/admin | |
* @author Wayne Brett <waynebrett@waynebrett.com> | |
*/ | |
class Developer_Branding_Admin { | |
/** | |
* The ID of this plugin. | |
* | |
* @since 1.0.0 | |
* @access private | |
* @var string $plugin_name The ID of this plugin. | |
*/ | |
private $plugin_name; | |
/** | |
* The version of this plugin. | |
* | |
* @since 1.0.0 | |
* @access private | |
* @var string $version The current version of this plugin. | |
*/ | |
private $version; | |
/** | |
* Initialize the class and set its properties. | |
* | |
* @since 1.0.0 | |
* @param string $plugin_name The name of this plugin. | |
* @param string $version The version of this plugin. | |
*/ | |
/** | |
* The options name to be used in this plugin | |
* | |
* @since 1.0.0 | |
* @access private | |
* @var string $option_name Option name of this plugin | |
*/ | |
private $option_name = 'developer_branding'; | |
public function __construct( $plugin_name, $version ) { | |
$this->plugin_name = $plugin_name; | |
$this->version = $version; | |
} | |
/** | |
* Register the stylesheets for the admin area. | |
* | |
* @since 1.0.0 | |
*/ | |
public function enqueue_styles() { | |
/** | |
* This function is provided for demonstration purposes only. | |
* | |
* An instance of this class should be passed to the run() function | |
* defined in Developer_Branding_Loader as all of the hooks are defined | |
* in that particular class. | |
* | |
* The Developer_Branding_Loader will then create the relationship | |
* between the defined hooks and the functions defined in this | |
* class. | |
*/ | |
wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/developer-branding-admin.css', array(), $this->version, 'all' ); | |
} | |
/** | |
* Register the JavaScript for the admin area. | |
* | |
* @since 1.0.0 | |
*/ | |
public function enqueue_scripts() { | |
/** | |
* This function is provided for demonstration purposes only. | |
* | |
* An instance of this class should be passed to the run() function | |
* defined in Developer_Branding_Loader as all of the hooks are defined | |
* in that particular class. | |
* | |
* The Developer_Branding_Loader will then create the relationship | |
* between the defined hooks and the functions defined in this | |
* class. | |
*/ | |
wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/developer-branding-admin.js', array( 'jquery' ), $this->version, false ); | |
} | |
/** | |
* Add an options page under the Settings submenu | |
* | |
* @since 1.0.0 | |
*/ | |
public function add_options_page() { | |
$this->plugin_screen_hook_suffix = add_options_page( | |
__( 'Developer Branding', 'developer_branding' ), | |
__( 'Developer Branding', 'developer_branding' ), | |
'manage_options', | |
$this->plugin_name, | |
array( $this, 'display_options_page' ) | |
); | |
} | |
/** | |
* Render the options page for plugin | |
* | |
* @since 1.0.0 | |
*/ | |
public function display_options_page() { | |
include_once 'partials/developer-branding-admin-display.php'; | |
} | |
/** | |
* Register all related settings of this plugin | |
* | |
* @since 1.0.0 | |
*/ | |
public function register_setting() { | |
// Add a General section | |
add_settings_section( | |
$this->option_name . '_general', | |
__( 'General Options', 'developer-branding' ), | |
array( $this, $this->option_name . '_general_cb' ), | |
$this->plugin_name | |
); | |
add_settings_field( | |
$this->option_name . '_remove_edit', | |
__( 'Remove the Theme editor option', 'developer-branding' ), | |
array( $this, $this->option_name . '_remove_edit_cb' ), | |
$this->plugin_name, | |
$this->option_name . '_general', | |
array( 'label_for' => $this->option_name . '_remove_edit' ) | |
); | |
register_setting( $this->plugin_name, $this->option_name . '_remove_edit', array( $this, $this->option_name . '_validate_options' ) ); | |
} | |
/** | |
* Render the text for the general section | |
* | |
* @since 1.0.0 | |
*/ | |
public function developer_branding_general_cb() { | |
echo '<p>' . __( 'Please change the settings accordingly.', 'developer-branding' ) . '</p>'; | |
} | |
//Check here for solution | |
public function developer_branding_remove_edit_cb() { | |
$remove_edit = get_option( $this->option_name . '_remove_edit' ); | |
?> | |
<input type="checkbox" name="<?php echo $this->option_name . '_remove_edit' ?>" id="<?php echo $this->option_name . '_remove_edit' ?>" value="before" <?php checked( $remove_edit, 1 ); ?> > | |
<?php | |
} | |
//Need a valid sanitization for checkbox | |
public function developer_branding_validate_options( $remove_edit ) { | |
if ( in_array( $remove_edit, array( 'before', 'after' ), true ) ) { | |
return $remove_edit; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment