class Custom_Fields {
// ========== Variables ===========
public $page_title = '';
public $menu_title = '';
public $capability = '';
public $slug = '';
public $callback = array();
public $icon = '';
public $position = 0;
// custom variables
public $add_to = array();
public $options = array();
public $section_title = '';
public $section = '';
public $parent_slugs = array(
'dashboard' => 'index.php',
'posts' => 'edit.php',
'media' => 'upload.php',
'pages' => 'edit.php?post_type=page',
'comments' => 'edit-comments.php',
'custom_page_types' => 'edit.php?post_type=your_post_type',
'appearance' => 'themes.php',
'plugins' => 'plugins.php',
'users' => 'users.php',
'tools' => 'tools.php',
'settings' => 'options-general.php',
'network_settings' => 'settings.php'
);
/**
* Constructor
* @param String $args['page_title']
* @param String $args['menu_title']
* @param String $args['slug']
* @param String $args['icon']
* @param String $args['add_to']
* @param String $args['section_title']
* @param String $args['section']
* @param Array $args['options']
*/
public function __construct($args) {
// initialize variables
$this->init($args);
// register CSS
wp_register_style( 'sy-custom-settings-css', plugins_url( 'sy-custom-settings/sy-custom-settings.css' ));
wp_enqueue_style( 'sy-custom-settings-css' );
// Hook into the admin menu
add_action( 'admin_menu', array( $this, 'create_plugin_settings_page'));
// Add Settings and Fields
add_action( 'admin_init', array( $this, 'setup_sections' ) );
add_action( 'admin_init', array( $this, 'setup_fields' ) );
}
/**
* ========== Initialize variables ==========
* @param String $args['page_title']
* @param String $args['menu_title']
* @param String $args['slug']
* @param String $args['icon']
* @param String $args['add_to']
* @param String $args['section_title']
* @param String $args['section']
* @param Array $args['options']
*/
public function init($args){
$this->page_title = $args['page_title'];
$this->menu_title = $args['menu_title'];
$this->capability = 'manage_options';
$this->slug = $args['slug'];
$this->callback = array( $this, 'plugin_settings_page_content' );
$this->icon = $args['icon'];
$this->position = 100;
// custom
$this->add_to = $args['add_to'];
$this->options = $args['options'];
$this->section_title = $args['section_title'];
$this->section = $args['section'];
}
/**
* ========== Plugin Settings ==========
* Add option under a parent if it exists
* Otherwise, create a new option menu
*/
public function create_plugin_settings_page() {
// add_submenu_page( $slug, $page_title, $menu_title, $capability, $slug . '-sub', $callback );
// check if parent slugs exists in the array already
$parent_slug_exists = in_array($this->add_to, array_keys($this->parent_slugs));
// add it under parent slug
if($parent_slug_exists){
$parent_slug = $this->parent_slugs[$this->add_to];
add_submenu_page($parent_slug, $this->page_title, $this->menu_title, $this->capability, $this->slug, $this->callback );
return;
}
// add itself as a parent slug
add_menu_page( $this->page_title, $this->menu_title, $this->capability, $this->slug, $this->callback, $this->icon, $this->position );
}
/**
* ========== Display ===========
* The HTML side of the option page
*/
public function plugin_settings_page_content() {?>
<div class="wrap">
<h2><?php echo $this->page_title; ?></h2>
<hr>
<!-- Display admin notice ---->
<?php
if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] ){
$this->admin_notice();
} ?>
<!-- form -->
<form method="POST" action="options.php">
<?php
settings_fields( $this->slug );
do_settings_sections( $this->slug );
submit_button();
?>
</form>
</div> <?php
}
// =========== Admin Notice ===========
public function admin_notice() { ?>
<div class="notice notice-success is-dismissible">
<p>Your settings have been updated!</p>
</div><?php
}
// =========== Setup Sections =============
public function setup_sections() {
add_settings_section( $this->section, $this->section_title, array( $this, 'section_callback' ), $this->slug );
// add_settings_section( 'theme_settings', 'Theme Settings', array( $this, 'section_callback' ), $this->slug );
// add_settings_section( 'our_third_section', 'My Third Section Title', array( $this, 'section_callback' ), $this->slug );
}
// ========== Section function callback ===========
public function section_callback( $args ) {
switch( $args['id'] ){
case $this->section:
// echo 'Settings for your Live TV';
break;
// case 'theme_settings':
// echo 'This one is number two';
// break;
// case 'our_third_section':
// echo 'Third time is the charm!';
// break;
}
}
// ========== Setup Fields ===========
public function setup_fields() {
$fields = $this->options;
foreach( $fields as $field ){
add_settings_field( $field['uid'], $field['label'], array( $this, 'field_callback' ), $this->slug, $this->section, $field );
register_setting( $this->slug, $field['uid'] );
}
// foreach( $fields as $field ){
// add_settings_field( $field['uid'], $field['label'], array( $this, 'field_callback' ), $this->slug, $field['section'], $field );
// register_setting( $this->slug, $field['uid'] );
// }
}
// =========== Field functions callback ==========
public function field_callback( $args ) {
$value = get_option( $args['uid'] );
if( ! $value || !isset($value)) {
$value = isset($args['default']) ? $args['default'] : null;
}
switch( $args['type'] ){
case 'text':
case 'password':
case 'number':
printf( '<input class="sy_custom_input" name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" />', $args['uid'], $args['type'], $args['placeholder'], $value );
break;
case 'textarea':
printf( '<textarea name="%1$s" id="%1$s" placeholder="%2$s" rows="5" cols="50">%3$s</textarea>', $args['uid'], $args['placeholder'], $value );
break;
case 'select':
case 'multiselect':
if( ! empty ( $args['options'] ) && is_array( $args['options'] ) ){
$attributes = '';
$options_markup = '';
foreach( $args['options'] as $key => $label ){
$options_markup .= sprintf( '<option value="%s" %s>%s</option>', $key, selected( $value[ array_search( $key, $value, true ) ], $key, false ), $label );
}
if( $args['type'] === 'multiselect' ){
$attributes = ' multiple="multiple" ';
}
printf( '<select name="%1$s[]" id="%1$s" %2$s>%3$s</select>', $args['uid'], $attributes, $options_markup );
}
break;
case 'radio':
case 'checkbox':
if( ! empty ( $args['options'] ) && is_array( $args['options'] ) ){
$options_markup = '';
$i = 0;
foreach( $args['options'] as $key => $label ){
$i++;
$checked = !isset($value) ? false : checked( $value[ array_search( $key, $value, true ) ], $key, false );
$options_markup .= sprintf( '<label for="%1$s_%6$s"><input id="%1$s_%6$s" name="%1$s[]" type="%2$s" value="%3$s" %4$s /> %5$s</label><br/>', $args['uid'], $args['type'], $key, $checked, $label, $i );
}
printf( '<fieldset>%s</fieldset>', $options_markup );
}
break;
}
if(isset($args['helper'])){
printf( '<span class="helper"> %s</span>', $args['helper'] );
}
if(isset($args['supplimental'])){
printf( '<p class="description">%s</p>', $args['supplimental'] );
}
}
}