Skip to content

Instantly share code, notes, and snippets.

@certainlyakey
Created April 26, 2014 12:45
Show Gist options
  • Save certainlyakey/11319231 to your computer and use it in GitHub Desktop.
Save certainlyakey/11319231 to your computer and use it in GitHub Desktop.
Register additional Wordpress options in admin
<?php $myopts = get_option( 'my_option_name', '' ); ?>
<div class="slogan" title="<?php echo $myopts['slogan_translate']; ?>">
<?php
//from http://codex.wordpress.org/Creating_Options_Pages#Example_.232
//Don't forget to include this file in functions.php: require_once( 'external/register_options_page.php' );
//Search for EDIT to add new options
class MySettingsPage
{
/**
* Holds the values to be used in the fields callbacks
*/
public $sectionname = 'Настройки сайта';
public $sectionname_menu = 'Дополнительные настройки сайта';
public $sectionsubhead = 'Дополнительные настройки сайта';
// public $sectionintrotext = $sectionname.' &laquo;'.get_bloginfo( 'name' ).'&raquo;'; //Referring to another variable doesn't work
public $db_options_id = 'my_option_name';
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
$this->sectionname, //The text for <title> tags
$this->sectionname_menu, //The text to be used for the menu
'manage_options', //capability
'my-setting-admin', //menu slug
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( $this->db_options_id );
?>
<div class="wrap">
<h2><?php echo $this->sectionname; ?></h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'my_option_group' );
do_settings_sections( 'my-setting-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'my_option_group', // Option group
$this->db_options_id, // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'setting_section_id', // ID
$this->sectionsubhead, // Title
array( $this, 'print_section_info' ), // Callback
'my-setting-admin' // Page
);
// EDIT 1
add_settings_field(
'slogan',
'Слоган',
array( $this, 'slogan_callback' ),
'my-setting-admin',
'setting_section_id'
);
add_settings_field(
'slogan_translate',
'Перевод слогана',
array( $this, 'slogan_translate_callback' ),
'my-setting-admin',
'setting_section_id'
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
$new_input = array();
//EDIT 2
if( isset( $input['slogan'] ) )
$new_input['slogan'] = sanitize_text_field( $input['slogan'] );
if( isset( $input['slogan_translate'] ) )
$new_input['slogan_translate'] = sanitize_text_field( $input['slogan_translate'] );
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
// echo $this->sectionintrotext;
echo $this->sectionname.' &laquo;'.get_bloginfo( 'name' ).'&raquo;';
}
//EDIT 3
/**
* Get the settings option array and print one of its values
*/
public function slogan_callback()
{
printf(
'<input type="text" id="slogan" name="my_option_name[slogan]" value="%s" style="width:300px" />',
isset( $this->options['slogan'] ) ? esc_attr( $this->options['slogan']) : ''
);
}
public function slogan_translate_callback()
{
printf(
'<input type="text" id="slogan_translate" name="my_option_name[slogan_translate]" value="%s" style="width:300px" />',
isset( $this->options['slogan_translate'] ) ? esc_attr( $this->options['slogan_translate']) : ''
);
}
}
if( is_admin() )
$my_settings_page = new MySettingsPage();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment