我今天刚刚完成了我的主题,并在主题设置页面中添加了css.php文件,我可以从面板中更改网站颜色。
标题中的我添加了
<link rel='stylesheet' type='text/css' href="<?php bloginfo('template_directory'); ?>/css/style.php" />
但我想用if else
来使用它 例如,
如果安装了主题并且主题设置中没有更改
显示
<link rel='stylesheet' type='text/css' href="<?php bloginfo('template_directory'); ?>/css/style.css" />
css文件
但是当主题设置页面中的主题更改时,首先删除第一个css文件并显示
<link rel='stylesheet' type='text/css' href="<?php bloginfo('template_directory'); ?>/css/style.php" />
这可能吗?
答案 0 :(得分:0)
您需要做的就是为主题设置保存操作添加一个标志。 例如:
1:在你的插件或主题functions.php
中add_action( 'optionsframework_after_validate', function(){ // this action is not real, you need to change it to your code
update_option( 'theme-settings-changed', 'changed' );
});
2:在您的主题标题文件
中$change_check = get_option( 'theme-settings-changed' );
if ( $change_check == 'changed' ) {
?>
<link rel='stylesheet' type='text/css' href="<?php bloginfo('template_directory'); ?>/css/style.php" />
<?php
} else {
?>
<link rel='stylesheet' type='text/css' href="<?php bloginfo('template_directory'); ?>/css/style.css" />
<?php
}
此方法无法保证设置是否“实际”更改,因为它只是检查是否触发了保存按钮而不管设置更改。但是可以肯定的是,当用户更改值时会触发保存按钮。 如果这还不够,你可以为此目的添加一个检查逻辑,虽然它会很乏味。
答案 1 :(得分:0)
<?php
add_action( 'init', 'optionsframework_rolescheck' );
function optionsframework_rolescheck () {
if ( current_user_can( 'edit_theme_options' ) ) {
// If the user can edit theme options, let the fun begin!
add_action( 'admin_menu', 'optionsframework_add_page');
add_action( 'admin_init', 'optionsframework_init' );
add_action( 'wp_before_admin_bar_render', 'optionsframework_adminbar' );
}
}
/* Loads the file for option sanitization */
add_action( 'init', 'optionsframework_load_sanitization' );
function optionsframework_load_sanitization() {
require_once dirname( __FILE__ ) . '/options-sanitize.php';
}
/*
* Creates the settings in the database by looping through the array
* we supplied in options.php. This is a neat way to do it since
* we won't have to save settings for headers, descriptions, or arguments.
*
* Read more about the Settings API in the WordPress codex:
* http://codex.wordpress.org/Settings_API
*
*/
function optionsframework_init() {
// Include the required files
require_once dirname( __FILE__ ) . '/options-interface.php';
require_once dirname( __FILE__ ) . '/options-media-uploader.php';
// Optionally Loads the options file from the theme
$location = apply_filters( 'options_framework_location', array( 'options.php' ) );
$optionsfile = locate_template( $location );
// Load settings
$optionsframework_settings = get_option('optionsframework' );
// Updates the unique option id in the database if it has changed
if ( function_exists( 'optionsframework_option_name' ) ) {
optionsframework_option_name();
}
elseif ( has_action( 'optionsframework_option_name' ) ) {
do_action( 'optionsframework_option_name' );
}
// If the developer hasn't explicitly set an option id, we'll use a default
else {
$default_themename = get_option( 'stylesheet' );
$default_themename = preg_replace("/\W/", "_", strtolower($default_themename) );
$default_themename = 'optionsframework_' . $default_themename;
if ( isset( $optionsframework_settings['id'] ) ) {
if ( $optionsframework_settings['id'] == $default_themename ) {
// All good, using default theme id
} else {
$optionsframework_settings['id'] = $default_themename;
update_option( 'optionsframework', $optionsframework_settings );
}
}
else {
$optionsframework_settings['id'] = $default_themename;
update_option( 'optionsframework', $optionsframework_settings );
}
}
// If the option has no saved data, load the defaults
if ( ! get_option( $optionsframework_settings['id'] ) ) {
optionsframework_setdefaults();
}
// Registers the settings fields and callback
register_setting( 'optionsframework', $optionsframework_settings['id'], 'optionsframework_validate' );
// Change the capability required to save the 'optionsframework' options group.
add_filter( 'option_page_capability_optionsframework', 'optionsframework_page_capability' );
}
/**
* Ensures that a user with the 'edit_theme_options' capability can actually set the options
* See: http://core.trac.wordpress.org/ticket/14365
*
* @param string $capability The capability used for the page, which is manage_options by default.
* @return string The capability to actually use.
*/
function optionsframework_page_capability( $capability ) {
return 'edit_theme_options';
}
/*
* Adds default options to the database if they aren't already present.
* May update this later to load only on plugin activation, or theme
* activation since most people won't be editing the options.php
* on a regular basis.
*
* http://codex.wordpress.org/Function_Reference/add_option
*
*/
function optionsframework_setdefaults() {
$optionsframework_settings = get_option( 'optionsframework' );
// Gets the unique option id
$option_name = $optionsframework_settings['id'];
/*
* Each theme will hopefully have a unique id, and all of its options saved
* as a separate option set. We need to track all of these option sets so
* it can be easily deleted if someone wishes to remove the plugin and
* its associated data. No need to clutter the database.
*
*/
if ( isset( $optionsframework_settings['knownoptions'] ) ) {
$knownoptions = $optionsframework_settings['knownoptions'];
if ( !in_array( $option_name, $knownoptions ) ) {
array_push( $knownoptions, $option_name );
$optionsframework_settings['knownoptions'] = $knownoptions;
update_option( 'optionsframework', $optionsframework_settings );
}
} else {
$newoptionname = array( $option_name );
$optionsframework_settings['knownoptions'] = $newoptionname;
update_option( 'optionsframework', $optionsframework_settings );
}
// Gets the default options data from the array in options.php
$options =& _optionsframework_options();
// If the options haven't been added to the database yet, they are added now
$values = of_get_default_values();
if ( isset( $values ) ) {
add_option( $option_name, $values ); // Add option with default settings
}
}
function optionsframework_menu_settings() {
$menu = array(
'page_title' => __( 'test Theme Options', 'optionsframework'),
'menu_title' => __('test Settings', 'optionsframework'),
'capability' => 'edit_theme_options',
'menu_slug' => 'test-options',
'callback' => 'optionsframework_page'
);
return apply_filters( 'optionsframework_menu', $menu );
}
/* Add a subpage called "Theme Options" to the appearance menu. */
function optionsframework_add_page() {
$menu = optionsframework_menu_settings();
$of_page = add_theme_page( $menu['page_title'], $menu['menu_title'], $menu['capability'], $menu['menu_slug'], $menu['callback'] );
// Load the required CSS and javscript
add_action( 'admin_enqueue_scripts', 'optionsframework_load_scripts' );
add_action( 'admin_print_styles-' . $of_page, 'optionsframework_load_styles' );
}
/* Loads the CSS */
function optionsframework_load_styles() {
wp_enqueue_style( 'optionsframework', OPTIONS_FRAMEWORK_DIRECTORY.'css/optionsframework.css' );
if ( !wp_style_is( 'wp-color-picker','registered' ) ) {
wp_register_style( 'wp-color-picker', OPTIONS_FRAMEWORK_DIRECTORY.'css/color-picker.min.css' );
}
wp_enqueue_style( 'wp-color-picker' );
}
/* Loads the javascript */
function optionsframework_load_scripts( $hook ) {
$menu = optionsframework_menu_settings();
if ( 'appearance_page_' . $menu['menu_slug'] != $hook )
return;
// Enqueue colorpicker scripts for versions below 3.5 for compatibility
if ( !wp_script_is( 'wp-color-picker', 'registered' ) ) {
wp_enqueue_script( 'iris', false, array( 'jquery','jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), false, 1 );
wp_register_script( 'wp-color-picker', OPTIONS_FRAMEWORK_DIRECTORY . 'js/color-picker.min.js', array( 'jquery', 'iris' ) );
$colorpicker_l10n = array(
'clear' => __( 'Clear','options_framework_theme' ),
'defaultString' => __( 'Default', 'options_framework_theme' ),
'pick' => __( 'Select Color', 'options_framework_theme' )
);
wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', $colorpicker_l10n );
}
// Enqueue custom option panel JS
wp_enqueue_script( 'options-custom', OPTIONS_FRAMEWORK_DIRECTORY . 'js/options-custom.js', array( 'jquery','wp-color-picker' ) );
// Inline scripts from options-interface.php
add_action( 'admin_head', 'of_admin_head' );
}
function of_admin_head() {
// Hook to add custom scripts
do_action( 'optionsframework_custom_scripts' );
}
/*
* Builds out the options panel.
*
* If we were using the Settings API as it was likely intended we would use
* do_settings_sections here. But as we don't want the settings wrapped in a table,
* we'll call our own custom optionsframework_fields. See options-interface.php
* for specifics on how each individual field is generated.
*
* Nonces are provided using the settings_fields()
*
*/
if ( !function_exists( 'optionsframework_page' ) ) :
function optionsframework_page() {
settings_errors(); ?>
<div id="optionsframework-wrap" class="wrap">
<?php screen_icon( 'themes' ); ?>
<h2 class="nav-tab-wrapper">
<?php echo optionsframework_tabs(); ?>
</h2>
<div id="optionsframework-metabox" class="metabox-holder">
<div id="optionsframework" class="postbox">
<form action="options.php" method="post">
<?php settings_fields( 'optionsframework' ); ?>
<?php optionsframework_fields(); /* Settings */ ?>
<div id="optionsframework-submit">
<input type="submit" class="button-primary" name="update" value="<?php esc_attr_e( 'Save Options', 'options_framework_theme' ); ?>" />
<input type="submit" class="reset-button button-secondary" name="reset" value="<?php esc_attr_e( 'Restore Defaults', 'options_framework_theme' ); ?>" onclick="return confirm( '<?php print esc_js( __( 'Click OK to reset. Any theme settings will be lost!', 'options_framework_theme' ) ); ?>' );" />
<div class="clear"></div>
</div>
</form>
</div> <!-- / #container -->
</div>
<?php do_action( 'optionsframework_after' ); ?>
</div> <!-- / .wrap -->
<?php
}
endif;
/**
* Validate Options.
*
* This runs after the submit/reset button has been clicked and
* validates the inputs.
*
* @uses $_POST['reset'] to restore default options
*/
function optionsframework_validate( $input ) {
/*
* Restore Defaults.
*
* In the event that the user clicked the "Restore Defaults"
* button, the options defined in the theme's options.php
* file will be added to the option for the active theme.
*/
if ( isset( $_POST['reset'] ) ) {
add_settings_error( 'options-framework', 'restore_defaults', __( 'Default options restored.', 'options_framework_theme' ), 'updated fade' );
return of_get_default_values();
}
/*
* Update Settings
*
* This used to check for $_POST['update'], but has been updated
* to be compatible with the theme customizer introduced in WordPress 3.4
*/
$clean = array();
$options =& _optionsframework_options();
foreach ( $options as $option ) {
if ( ! isset( $option['id'] ) ) {
continue;
}
if ( ! isset( $option['type'] ) ) {
continue;
}
$id = preg_replace( '/[^a-zA-Z0-9._\-]/', '', strtolower( $option['id'] ) );
// Set checkbox to false if it wasn't sent in the $_POST
if ( 'checkbox' == $option['type'] && ! isset( $input[$id] ) ) {
$input[$id] = false;
}
// Set each item in the multicheck to false if it wasn't sent in the $_POST
if ( 'multicheck' == $option['type'] && ! isset( $input[$id] ) ) {
foreach ( $option['options'] as $key => $value ) {
$input[$id][$key] = false;
}
}
// For a value to be submitted to database it must pass through a sanitization filter
if ( has_filter( 'of_sanitize_' . $option['type'] ) ) {
$clean[$id] = apply_filters( 'of_sanitize_' . $option['type'], $input[$id], $option );
}
}
// Hook to run after validation
do_action( 'optionsframework_after_validate', $clean );
return $clean;
}
/**
* Display message when options have been saved
*/
function optionsframework_save_options_notice() {
add_settings_error( 'options-framework', 'save_options', __( 'Options saved.', 'options_framework_theme' ), 'updated fade' );
}
add_action( 'optionsframework_after_validate', 'optionsframework_save_options_notice' );
/**
* Format Configuration Array.
*
* Get an array of all default values as set in
* options.php. The 'id','std' and 'type' keys need
* to be defined in the configuration array. In the
* event that these keys are not present the option
* will not be included in this function's output.
*
* @return array Rey-keyed options configuration array.
*
* @access private
*/
function of_get_default_values() {
$output = array();
$config =& _optionsframework_options();
foreach ( (array) $config as $option ) {
if ( ! isset( $option['id'] ) ) {
continue;
}
if ( ! isset( $option['std'] ) ) {
continue;
}
if ( ! isset( $option['type'] ) ) {
continue;
}
if ( has_filter( 'of_sanitize_' . $option['type'] ) ) {
$output[$option['id']] = apply_filters( 'of_sanitize_' . $option['type'], $option['std'], $option );
}
}
return $output;
}
/**
* Add Theme Options menu item to Admin Bar.
*/
function &_optionsframework_options() {
static $options = null;
if ( !$options ) {
// Load options from options.php file (if it exists)
$location = apply_filters( 'options_framework_location', array('options.php') );
if ( $optionsfile = locate_template( $location ) ) {
$maybe_options = require_once $optionsfile;
if ( is_array($maybe_options) ) {
$options = $maybe_options;
} else if ( function_exists( 'optionsframework_options' ) ) {
$options = optionsframework_options();
}
}
// Allow setting/manipulating options via filters
$options = apply_filters('of_options', $options);
}
return $options;
}
/**
* Get Option.
*
* Helper function to return the theme option value.
* If no value has been saved, it returns $default.
* Needed because options are saved as serialized strings.
*/
if ( ! function_exists( 'of_get_option' ) ) {
function of_get_option( $name, $default = false ) {
$config = get_option( 'optionsframework' );
if ( ! isset( $config['id'] ) ) {
return $default;
}
$options = get_option( $config['id'] );
if ( isset( $options[$name] ) ) {
return $options[$name];
}
return $default;
}
}
答案 2 :(得分:0)
function optionsframework_option_name() {
// This gets the theme name from the stylesheet
$themename = wp_get_theme();
$themename = preg_replace("/\W/", "_", strtolower($themename) );
$optionsframework_settings = get_option( 'optionsframework' );
$optionsframework_settings['id'] = $themename;
update_option( 'optionsframework', $optionsframework_settings );
}
/**
* Defines an array of options that will be used to generate the settings page and be saved in the database.
* When creating the 'id' fields, make sure to use all lowercase and no spaces.
*
* If you are making your theme translatable, you should replace 'test'
* with the actual text domain for your theme. Read more:
* http://codex.wordpress.org/Function_Reference/load_theme_textdomain
*/
function optionsframework_options() {
$options = array();
$imagepath = get_template_directory_uri() . '/images/';
$true_false = array(
'true' => __('true', 'test'),
'false' => __('false', 'test')
);
$options[] = array(
'name' => __('General', 'test'),
'type' => 'heading');
$options[] = array(
'name' => __('Site Links', 'test'),
'desc' => __('Site Links color', 'test'),
'id' => 'links',
'std' => '',
'class' => '',
'type' => 'color');
$options[] = array(
'name' => __('Site Links Hover', 'test'),
'desc' => __('Site Links Hover color', 'test'),
'id' => 'hover',
'std' => '',
'class' => '',
'type' => 'color');
$options[] = array(
'name' => __('Background Color', 'test'),
'desc' => __('background color', 'test'),
'id' => 'bg',
'std' => '',
'class' => '',
'type' => 'color');
return $options;
}