Skip to content

Instantly share code, notes, and snippets.

@tradesouthwest
Created March 24, 2017 03:28
Show Gist options
  • Save tradesouthwest/525a602012eb44479e719d3131cd3cd5 to your computer and use it in GitHub Desktop.
Save tradesouthwest/525a602012eb44479e719d3131cd3cd5 to your computer and use it in GitHub Desktop.
wordpress settings option defaults keep reappearing
<?php
/**
* Define WTO settings file
*/
function valuadd_settings_file() {
if ( file_exists( VALA_THEME_SETTINGS ) ) {
$theme_settings = VALA_THEME_SETTINGS;
} else {
$theme_settings = VALA_THEME_SETTINGS_VALU;
}
return $theme_settings;
}
/**
* Add a "Theme Options" menu to admin bar
*/
function valuadd_admin_bar_menu() {
global $wp_admin_bar, $wpdb;
if ( !is_super_admin() || !is_admin_bar_showing() )
return;
$wp_admin_bar->add_menu( array(
'id' => 'vala_theme_options',
'title' => VALA_PAGE_NAME,
'href' => get_option( 'siteurl' ).'/wp-admin/themes.php?page=' . VALA_PAGE_SLUG
) );
}
add_action( 'admin_bar_menu', 'valuadd_admin_bar_menu', 1000 );
/**
* Generate a message with a link to the options page on theme activation
*/
function valuadd_activation_message() { ?>
<script type="text/javascript">
jQuery(function(){
var message = '<p><strong>New theme activated!</strong> This theme supports custom settings, please visit the <a href="<?php echo admin_url( 'themes.php?page=' . VALA_PAGE_SLUG ); ?>">theme options</a> page to configure them.</p>';
jQuery('.themes-php #message2').html( message );
});
</script>
<?php
}
add_action( 'admin_head', 'valuadd_activation_message' );
/**
* Load necessary JavaScript and CSS files
*/
function valuadd_options_scripts() {
if ( isset($_GET['page']) && $_GET['page'] == VALA_PAGE_SLUG ) {
wp_enqueue_media();
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'valuadd-admin',
trailingslashit( VALA_JS_URL ) . 'admin.js', array( 'jquery' ) );
wp_enqueue_script( 'valu-colorpicker',
trailingslashit( VALA_JS_URL ) . 'colorpicker.js', array(
'wp-color-picker' ), false, true );
wp_enqueue_script( 'valuadd-upload',
trailingslashit( VALA_JS_URL ) . 'upload.js', array( 'jquery' ) );
wp_enqueue_style( 'valuadd-admin',
trailingslashit( VALA_CSS_URL ) . 'admin.css' );
}
}
add_action('admin_print_scripts', 'valuadd_options_scripts');
/**
* Register Options' Settings.
*/
function valuadd_register_settings() {
include valuadd_settings_file();
foreach ( $options as $option ) {
if ( $option['type'] != 'heading' && $option['type']
!= 'info' && $option['type'] != 'help' )
{
if ( ! get_option( $option['id'] ) )
{
update_option( $option['id'], $option['default'] );
}
}
}
}
add_action( 'after_switch_theme', 'valuadd_register_settings' );
//------------------------------------ yada yada
/**
* Init WTO Page.
*/
function valuadd_page_init() {
// Include the theme settings
include valuadd_settings_file();
// Define the current tab
$current_tab = ( isset( $_GET[ 'tab' ] ) ) ? $_GET[ 'tab' ] : $options[0]['tab'];
// If the current user can manage options and the page is the options page
// Save, Reset, Backup and Restore is possible
if ( current_user_can( 'manage_options' ) && isset( $_GET['page'] )
&& $_GET['page'] == VALA_PAGE_SLUG ) {
// Save Options
if ( isset( $_REQUEST['action'] ) && 'save' == $_REQUEST['action'] ) {
foreach ( $options as $option ) {
if ( $option['type'] != 'heading' && $option['type'] != 'info' && $option['type'] != 'help' ) {
if ( isset( $_POST[$option['id']] ) ) {
if ( ! is_array( $_POST[$option['id']] ) ) {
$the_value = stripslashes( $_POST[$option['id']] );
} else {
$the_value = serialize( $_POST[$option['id']] );
}
update_option( $option['id'], $the_value );
} else {
delete_option( $option['id'] );
}
}
}
// Redirect to the theme options page
wp_redirect( admin_url( 'themes.php?page='. VALA_PAGE_SLUG .'&tab='. $current_tab .'&saved=true' ) );
die;
}
// ------------------------------------yada yada
// Generate the theme options menu
$options_page = add_theme_page(
VALA_PAGE_NAME,
VALA_PAGE_NAME,
'edit_themes',
VALA_PAGE_SLUG,
'valuadd_html_output',
false,
30
);
add_action( 'load-' . $options_page, 'valuadd_help_tab' );
}
add_action( 'admin_menu', 'valuadd_page_init' );
function valuadd_html_output() {
$tabs = '';
// Include the theme settings file
include valuadd_settings_file();
// Include the options machine to populate the setting fields
include VALA_FORMS;
?>
<div id="valuAdd" class="wrap">
<?php
}
/**
* Run the valuadd_init hook.
*/
do_action( 'valuadd_init' );
@tradesouthwest
Copy link
Author

public review gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment