Skip to content

Instantly share code, notes, and snippets.

@claudiosanches
Created December 1, 2012 21:16
Show Gist options
  • Save claudiosanches/4185152 to your computer and use it in GitHub Desktop.
Save claudiosanches/4185152 to your computer and use it in GitHub Desktop.
WordPress - Theme Settings Framework
<?php
// Esse é o arquivo com a base das funções.
// Tente não alterar ele.
/**
* Theme Options Functions
*/
/**
* Load options scripts
*/
function _base_theme_options_scripts() {
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'farbtastic' );
wp_enqueue_style( 'farbtastic' );
wp_enqueue_script( 'media-upload' );
wp_enqueue_script( 'thickbox' );
wp_enqueue_style( 'thickbox' );
}
if ( isset( $_GET['page'] ) && $_GET['page'] == $_base_options_url ) {
add_action( 'admin_init', '_base_theme_options_scripts' );
}
/**
* Section fallback
*/
function _base_section_options_callback() {
// your code here
}
/**
* Text
*/
function _base_text_element_callback( $args ) {
$menu = $args['menu'];
$id = $args['id'];
$options = get_option( $menu );
if ( isset( $options[$id] ) ) {
$current = $options[$id];
} else {
$current = isset( $args['default'] ) ? $args['default'] : '';
}
$html = '<input type="text" id="' . $id . '" name="' . $menu . '[' . $id . ']" value="' . esc_attr( $current ) . '" class="regular-text" />';
if (isset($args['description'])) {
$html .= '<p class="description">' . $args['description'] . '</p>';
}
echo $html;
}
/**
* Textarea
*/
function _base_textarea_element_callback( $args ) {
$menu = $args['menu'];
$id = $args['id'];
$options = get_option($menu);
if ( isset( $options[$id] ) ) {
$current = $options[$id];
} else {
$current = isset( $args['default'] ) ? $args['default'] : '';
}
$html = '<textarea id="' . $id . '" name="' . $menu . '[' . $id . ']" rows="5" cols="50">' . esc_attr( $current ) . '</textarea>';
if ( isset( $args['description'] ) ) {
$html .= '<p class="description">' . $args['description'] . '</p>';
}
echo $html;
}
/**
* Checkbox
*/
function _base_checkbox_element_callback( $args ) {
$menu = $args['menu'];
$id = $args['id'];
$label = $args['label'];
$options = get_option( $menu );
if ( isset( $options[$id] ) ) {
$current = $options[$id];
} else {
$current = isset( $args['default'] ) ? $args['default'] : '0';
}
$html = '<input type="checkbox" id="' . $id . '" name="' . $menu . '[' . $id . ']" value="1"' . checked( 1, $current, false ) . '/>';
$html .= ' <label for="' . $id . '">' . $label . '</label>';
if ( isset( $args['description'] ) ) {
$html .= '<p class="description">' . $args['description'] . '</p>';
}
echo $html;
}
/**
* Radio
*/
function _base_radio_element_callback( $args ) {
$menu = $args['menu'];
$id = $args['id'];
$items = $args['items'];
$options = get_option( $menu );
if ( isset( $options[$id] ) ) {
$current = $options[$id];
} else {
$current = isset( $args['default'] ) ? $args['default'] : '';
}
$html = '';
foreach ( $items as $key => $value ) {
$html .= '<input type="radio" id="' . $id . '-' . $key . '" name="' . $menu . '[' . $id . ']" value="' . $key . '"' . checked( $key, $current, false ) . '/>';
$html .= ' <label for="' . $id . '-' . $key . '">' . $value . '</label><br />';
}
if ( isset( $args['description'] ) ) {
$html .= '<p class="description">' . $args['description'] . '</p>';
}
echo $html;
}
/**
* Select
*/
function _base_select_element_callback( $args ) {
$menu = $args['menu'];
$id = $args['id'];
$items = $args['items'];
$options = get_option( $menu );
if ( isset( $options[$id] ) ) {
$current = $options[$id];
} else {
$current = isset( $args['default'] ) ? $args['default'] : '';
}
$html = '<select id="' . $id . '" name="' . $menu . '[' . $id . ']">';
foreach ( $items as $key => $value ) {
$html .= '<option value="' . $key . '"' . selected( $current, $key, false ) . '>' . $value . '</option>';
}
$html .= '</select>';
if (isset($args['description'])) {
$html .= '<p class="description">' . $args['description'] . '</p>';
}
echo $html;
}
/**
* Select category
*/
function _base_category_element_callback( $args ) {
$menu = $args['menu'];
$id = $args['id'];
$items = get_categories( 'hide_empty=0&orderby=name' );
$options = get_option( $menu );
if ( isset( $options[$id] ) ) {
$current = $options[$id];
} else {
$current = isset( $args['default'] ) ? $args['default'] : '';
}
$html = '<select id="' . $id . '" name="' . $menu . '[' . $id . ']">';
foreach ( $items as $cat ) {
$key = $cat->cat_ID;
$value = $cat->category_nicename;
$html .= '<option value="' . $key . '"' . selected( $current, $key, false ) . '>' . $value . '</option>';
}
$html .= '</select>';
if ( isset( $args['description'] ) ) {
$html .= '<p class="description">' . $args['description'] . '</p>';
}
echo $html;
}
/**
* Color
*/
function _base_color_element_callback( $args ) {
$menu = $args['menu'];
$id = $args['id'];
$options = get_option( $menu );
if ( isset( $options[$id] ) ) {
$current = $options[$id];
} else {
$current = isset( $args['default'] ) ? $args['default'] : '#ffffff';
}
$html = '<input style="width: 70px" name="' . $menu . '[' . $id . ']" type="text" id="color-' . $id . '" value="' . esc_attr( $current ) . '" class="regular-text" />';
if ( isset( $args['description'] ) ) {
$html .= '<p class="description">' . $args['description'] . '</p>';
}
$html .= '<div id="farbtasticbox-' . $id . '"></div>';
$html .= '<script type="text/javascript">';
$html .= 'jQuery(document).ready(function($) {';
$html .= '$("#farbtasticbox-' . $id . '").hide();';
$html .= '$("#farbtasticbox-' . $id . '").farbtastic("#color-' . $id . '");';
$html .= '$("#color-' . $id . '").click(function(){';
$html .= '$("#farbtasticbox-' . $id . '").slideToggle()';
$html .= '});';
$html .= '});';
$html .= '</script>';
echo $html;
}
/**
* Upload
*/
function _base_upload_element_callback( $args ) {
$menu = $args['menu'];
$id = $args['id'];
$options = get_option( $menu );
if ( isset( $options[$id] ) ) {
$current = $options[$id];
} else {
$current = isset( $args['default'] ) ? $args['default'] : '';
}
$html = '<input name="' . $menu . '[' . $id . ']" type="text" id="' . $id . '" value="' . esc_attr( $current ) . '" class="regular-text" />';
$html .= '<input class="button" id="' . $id . '_button" type="button" value="' . __( 'Selecionar arquivo', '_base' ) . '" />';
if ( isset( $args['description'] ) ) {
$html .= '<p class="description">' . $args['description'] . '</p>';
}
$html .= '<script type="text/javascript">';
$html .= 'jQuery(document).ready(function($) {';
$html .= '$("#' . $id . '_button").click(function() {';
$html .= 'uploadID = $(this).prev("input");';
$html .= 'formfield = $("' . $id . '").attr("name");';
$html .= 'tb_show("", "media-upload.php?post_id=&amp;type=image&amp;TB_iframe=true");';
$html .= 'return false;';
$html .= '});';
$html .= 'window.send_to_editor = function(html) {';
$html .= 'imgurl = $("img",html).attr("src");';
$html .= 'uploadID.val(imgurl);';
$html .= 'tb_remove();';
$html .= '}';
$html .= '});';
$html .= '</script>';
echo $html;
}
/**
* Sanitization callback
*
* @params type string $input
* The unsanitized collection of options.
*
* @returns string
* The collection of sanitized values.
*/
function _base_validate_input( $input ) {
// Create our array for storing the validated options
$output = array();
// Loop through each of the incoming options
foreach ( $input as $key => $value ) {
// Check to see if the current option has a value. If so, process it.
if ( isset( $input[$key] ) ) {
// Strip all HTML and PHP tags and properly handle quoted strings
$output[$key] = strip_tags( stripslashes( $input[$key] ) );
}
}
// Return the array processing any additional functions filtered by this action
return apply_filters( '_base_validate_input', $output, $input );
}
<?php
// Este é o arquivo que gera as opções.
// É ele que você irá editar.
/**
* Theme options.
*/
$_base_options_name = __( 'Op&ccedil;&otilde;es do Tema', '_base' );
$_base_options_url = 'cs-theme-options';
$_base_options_capability = 'manage_options';
/**
* Calls theme options functons.
*/
// Arrume o caminho que leva para o theme-options-functions.php
require_once get_template_directory() . '/inc/theme-options-functions.php';
/**
* Add theme options menu page.
*
* References:
* add_theme_page: http://codex.wordpress.org/Function_Reference/add_theme_page
* add_submenu_page: http://codex.wordpress.org/Function_Reference/add_submenu_page
* Capability: http://codex.wordpress.org/Roles_and_Capabilities
*/
function _base_theme_menu() {
global $_base_options_name, $_base_options_url, $_base_options_capability;
add_theme_page(
$_base_options_name, // Page title
$_base_options_name, // Menu title
$_base_options_capability, // Capability
$_base_options_url, // Menu slug
'_base_theme_display' // Function
);
}
add_action( 'admin_menu', '_base_theme_menu' );
/**
* Display Options
*/
function _base_theme_display() {
global $_base_options_name, $_base_options_url;
$current_tab = '';
if ( isset($_GET['tab'] ) ) {
$current_tab = $_GET['tab'];
} else {
$current_tab = 'general';
}
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2 class="nav-tab-wrapper">
<a href="?page=<?php echo $_base_options_url; ?>&amp;tab=general" class="nav-tab <?php echo $current_tab == 'general' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Op&ccedil;&otilde;es Gerais', '_base' ); ?></a><a href="?page=<?php echo $_base_options_url; ?>&amp;tab=adsense" class="nav-tab <?php echo $current_tab == 'adsense' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Publicidade', '_base' ); ?></a><a href="?page=<?php echo $_base_options_url; ?>&amp;tab=seo" class="nav-tab <?php echo $current_tab == 'seo' ? 'nav-tab-active' : ''; ?>"><?php _e( 'SEO', '_base' ); ?></a>
</h2>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
if ( $current_tab == 'seo' ) {
settings_fields( '_base_op_seo' );
do_settings_sections( '_base_op_seo' );
} elseif ( $current_tab == 'adsense' ) {
settings_fields( '_base_op_adsense' );
do_settings_sections( '_base_op_adsense' );
} else {
settings_fields( '_base_op_general' );
do_settings_sections( '_base_op_general' );
}
submit_button();
?>
</form>
</div><!-- .wrap -->
<?php
}
/**
* References:
* add_settings_section: http://codex.wordpress.org/Function_Reference/add_settings_section
* add_settings_field: http://codex.wordpress.org/Function_Reference/add_settings_field
* register_setting: http://codex.wordpress.org/Function_Reference/register_setting
*/
function _base_initialize_theme_options() {
$option = '_base_op_general';
// Create option in wp_options
if ( false == get_option( $option ) ) {
add_option( $option );
}
// Favicon Section
add_settings_section(
'favicon_settings_section',
__( 'Favicon', '_base' ),
'__return_false',
$option
);
add_settings_field(
'favicon_ico',
__( 'Favicon .ico', '_base' ),
'_base_upload_element_callback',
$option,
'favicon_settings_section',
array(
'menu' => $option,
'id' => 'fav_ico',
'default' => ''
)
);
add_settings_field(
'favicon_png',
__( 'Favicon .png', '_base' ),
'_base_upload_element_callback',
$option,
'favicon_settings_section',
array(
'menu' => $option,
'id' => 'fav_png',
'default' => ''
)
);
// Social section
add_settings_section(
'social_settings_section',
__( 'Social', '_base' ),
'__return_false',
$option
);
add_settings_field(
'feedburner',
__( 'FeedBurner', '_base' ),
'_base_text_element_callback',
$option,
'social_settings_section',
array(
'menu' => $option,
'id' => 'feedburner',
'default' => '',
'description' => 'ID do FeedBurner'
)
);
add_settings_field(
'twitter',
__( 'Twitter', '_base' ),
'_base_text_element_callback',
$option,
'social_settings_section',
array(
'menu' => $option,
'id' => 'twitter',
'default' => '',
'description' => 'Nome de usu&aacute;rio do Twitter'
)
);
add_settings_field(
'facebook',
__( 'Facebook', '_base' ),
'_base_text_element_callback',
$option,
'social_settings_section',
array(
'menu' => $option,
'id' => 'facebook',
'default' => '',
'description' => 'ID da p&aacute;gina ou perfil no Facebook'
)
);
add_settings_field(
'gplus',
__( 'Google +', '_base' ),
'_base_text_element_callback',
$option,
'social_settings_section',
array(
'menu' => $option,
'id' => 'gplus',
'default' => '',
'description' => 'ID da p&aacute;gina ou perfil no Google +'
)
);
add_settings_field(
'pinterest',
__( 'Pinterest', '_base' ),
'_base_text_element_callback',
$option,
'social_settings_section',
array(
'menu' => $option,
'id' => 'pinterest',
'default' => '',
'description' => 'Nome de usu&aacute;rio no Pinterest'
)
);
// Google Analytics Section
add_settings_section(
'googleanalytics_settings_section',
__( 'Google Analytics', '_base' ),
'__return_false',
$option
);
add_settings_field(
'ga_id',
__( 'Google Analytics ID', '_base' ),
'_base_text_element_callback',
$option,
'googleanalytics_settings_section',
array(
'menu' => $option,
'id' => 'ga_id',
'default' => ''
)
);
// Register settings
register_setting( $option, $option, '_base_validate_input' );
}
add_action( 'admin_init', '_base_initialize_theme_options' );
/**
* Adsense
*/
function _base_initialize_theme_adsense() {
$option = '_base_op_adsense';
$section = 'adsense_settings_section';
// Create option in wp_options
if ( false == get_option( $option ) ) {
add_option( $option );
}
// Add section
add_settings_section(
$section,
__( '', '_base' ),
'__return_false',
$option
);
// Add fields
add_settings_field(
'header',
__( 'Cabe&ccedil;alho', '_base' ),
'_base_textarea_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'header',
'default' => '',
'description' => __( 'Insir&aacute; o HTML do banner', '_base' ),
)
);
add_settings_field(
'post_top',
__( 'In&iacute;cio do post', '_base' ),
'_base_textarea_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'post_top',
'default' => '',
'description' => __( 'Insir&aacute; o HTML do banner', '_base' ),
)
);
add_settings_field(
'post_bottom',
__( 'Final do post', '_base' ),
'_base_textarea_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'post_bottom',
'default' => '',
'description' => __( 'Insir&aacute; o HTML do banner', '_base' ),
)
);
// Register settings
register_setting( $option, $option, '_base_validate_input' );
}
add_action( 'admin_init', '_base_initialize_theme_adsense' );
/**
* Seo options
*/
function _base_initialize_theme_seo() {
$option = '_base_op_seo';
$section = 'seo_settings_section';
// Create option in wp_options
if ( false == get_option( $option ) ) {
add_option( $option );
}
// Add section
add_settings_section(
$section,
__( 'Meta Tags de rastreamento', '_base' ),
'__return_false',
$option
);
// Add fields
add_settings_field(
'google',
__( 'Google Webmaster Tools', '_base' ),
'_base_text_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'google',
'default' => '',
'description' => __( 'ID do Ferramentas para Webmaster', '_base' )
)
);
add_settings_field(
'bing',
__( 'Bing', '_base' ),
'_base_text_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'bing',
'default' => '',
'description' => __( 'ID do Bing', '_base' )
)
);
add_settings_field(
'yahoo',
__( 'Yahoo', '_base' ),
'_base_text_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'yahoo',
'default' => '',
'description' => __( 'ID do Yahoo', '_base' ),
)
);
add_settings_field(
'alexa',
__( 'Alexa', '_base' ),
'_base_text_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'alexa',
'default' => '',
'description' => __( 'ID do Alexa', '_base' ),
)
);
// Register settings
register_setting( $option, $option, '_base_validate_input' );
}
add_action( 'admin_init', '_base_initialize_theme_seo' );
/*
function _base_initialize_theme_seo() {
// Fields example:
$option = '_base_theme_options_seo';
$section = 'seo_settings_section';
// Create option in wp_options
if (false == get_option($option)) {
add_option($option);
}
// Add section
add_settings_section(
$section,
__('', '_base'),
'__return_false',
$option
);
// Add fields
add_settings_field(
'test_text',
'Campo text',
'_base_text_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'text_id',
'default' => ''
)
);
add_settings_field(
'test_textarea',
'Campo textarea',
'_base_textarea_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'textarea_id',
'default' => ''
)
);
add_settings_field(
'test_checkbox',
'Campo checkbox',
'_base_checkbox_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'checkbox_id',
'label' => 'Teste legenda do checkbox'
)
);
add_settings_field(
'test_radio',
'Campo radio',
'_base_radio_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'radio_1',
'items' => array(
'1' => 'Valor 1',
'2' => 'Valor 2',
'3' => 'Valor 3'
)
)
);
add_settings_field(
'test_select',
'Campo select',
'_base_select_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'select_id',
'items' => array(
'1' => 'Valor 1',
'2' => 'Valor 2',
'3' => 'Valor 3'
)
)
);
add_settings_field(
'test_category',
'Campo select com categoria',
'_base_category_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'category_id'
)
);
add_settings_field(
'test_color',
'Color Picker',
'_base_color_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'color_id',
'default' => '#CC0000'
)
);
add_settings_field(
'test_upload',
'Upload',
'_base_upload_element_callback',
$option,
$section,
array(
'menu' => $option,
'id' => 'upload_id'
)
);
// Register settings
register_setting( $option, $option, '_base_validate_input' );
}
add_action( 'admin_init', '_base_initialize_theme_seo' );
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment