我希望我可以在如何将两个不同的嵌入式样式表加载到head标签(wp_head操作钩)中获得帮助, 这取决于用户是否在主题选项页面中单击保存或重置。
现在的设置方式, 仅保存在数组开头指定的选项(用户键入的选项), 而不保存其他样式表。现在, 我有一个名为test1()和test2()的占位符函数代替了将要加载到wp_head中的实际样式表。下面是我的代码。测试功能下方注释掉的部分是我试图使此功能正常工作的部分。
<
?php // add the admin options page
$themename = "Cool Orange";
$shortname = "co";
$options = array (
array ( "name" =>
"Main Settings", "type" =>
"title" ), array ( "type" =>
"open" ), array ( "name" =>
"Upload logo to the Media Library:", "id" =>
$shortname."_logo_label", "std" =>
"", "type" =>
"label" ), array ( "name" =>
"Logo location:", "desc" =>
"In this section, you can replace the standard blog title heading with a custom logo. The logo cannot be wider than 960 pixels.", "id" =>
$shortname."_logo_url", "std" =>
"", "type" =>
"text" ), array ( "name" =>
"Logo width:", "desc" =>
"Enter logo width in pixels, for example <
strong>
800px<
/strong>
", "id" =>
$shortname."_logo_width", "std" =>
"", "type" =>
"text" ), array ( "name" =>
"Logo height:", "desc" =>
"Enter logo height in pixels, for example <
strong>
90px<
/strong>
", "id" =>
$shortname."_logo_height", "std" =>
"", "type" =>
"text" ), array( "type" =>
"close" ));
function mytheme_add_admin() {
global $themename, $shortname, $options;
if ( $_GET['page'] == basename(__FILE__) ) {
if ( 'save' == $_REQUEST['action'] ) {
foreach($options as $value) {
update_option( $value['id'], $_REQUEST[ $value['id'] ] );
}
foreach ($options as $value) {
if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]);
} else { delete_option( $value['id'] );
} }
header("Location: themes.php?page=functions.php&
saved=true");
die;
} else if( 'reset' == $_REQUEST['action'] ) {
foreach ($options as $value) {
delete_option( $value['id'] );
}
header("Location: themes.php?page=functions.php&
reset=true");
die;
}
}
add_theme_page($themename." Options", "".$themename." Options", 'edit_themes', basename(__FILE__), 'mytheme_admin');
}function mytheme_admin() {
global $themename, $shortname, $options;
if ( $_REQUEST['saved'] ) echo '<
div id="message" class="updated fade">
<
p>
<
strong>
'.$themename.' settings saved.<
/strong>
<
/p>
<
/div>
';
if ( $_REQUEST['reset'] ) echo '<
div id="message" class="updated fade">
<
p>
<
strong>
'.$themename.' settings reset.<
/strong>
<
/p>
<
/div>
';
?>
<
div class="wrap">
<
h2>
<
?php echo $themename;
?>
Theme Options<
/h2>
<
form method="post">
<
h3>
How to upload a logo to replace the heading<
/h3>
<
div style="background-color: #FFFFFF;
border: 1px solid #BBBBBB;
padding: 30px;
">
<
ul style="list-style: disc;
">
<
li>
Upload the image from your computer to the Media Library using the <
strong>
Upload image<
/strong>
button below<
/li>
<
li>
Go to the <
strong>
Media<
/strong>
button at the left to acces the Media Library. Look for the file you just uploaded. It should be the file at the top of the list.<
/li>
<
li>
Once you mouseover the image, click <
strong>
Edit<
/strong>
<
/li>
<
li>
in the Edit Media dialog, highlight (Ctrl a) the url in the <
strong>
File URL<
/strong>
textbox<
/li>
<
li>
Note the width and height of the image<
/li>
<
li>
<
strong>
Copy<
/strong>
(ctrl c) the url and return to this page by clicking <
strong>
Appearance<
/strong>
, then <
strong>
Cool Orange Theme Options<
/strong>
<
/li>
<
li>
<
strong>
Paste<
/strong>
(ctrl v) the url into the <
strong>
Logo location<
/strong>
box below<
/li>
<
li>
<
strong>
Paste<
/strong>
(ctrl v) the width and height of the image into the <
strong>
Logo width<
/strong>
and <
strong>
Logo height<
/strong>
boxes below<
/li>
<
/ul>
<
/div>
<
?php foreach($options as $value) {
//Next is the code which tells WordPress how to display the ‘type’ of option used (title, open, close, text, textarea, checkbox etc.)switch ( $value['type'] ) {
case "open":
?>
<
div style="width: 100%;
">
<
?php break;
case "title":
?>
<
h3>
<
?php echo $value['name'];
?>
<
/h3>
<
?php break;
case "label":
?>
<
p>
<
?php echo $value['name'];
?>
<
/p>
<
p>
<
label for="upload_image">
<
input id="upload_image_button" type="button" value="http://www.srcmini.com/Upload image" />
<
/p>
<
?php break;
case "text":
?>
<
p>
<
strong>
<
?php echo $value['name'];
?>
<
/strong>
<
/p>
<
input style="width:400px;
" name="<
?php echo $value['id'];
?>
" id="<
?php echo $value['id'];
?>
" type="<
?php echo $value['type'];
?>
" value="http://www.srcmini.com/<
?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] );
} else { echo $value['std'];
} ?>
" />
<
br />
<
div style="font-size: 11px;
">
<
?php echo $value['desc'];
?>
<
/div>
<
?php break;
case "close":
?>
<
/div>
<
br />
<
?php break;
}
} ?>
<
p class="submit">
<
input name="save" class="button-primary" type="submit" value="http://www.srcmini.com/Save changes" />
<
input type="hidden" name="action" value="http://www.srcmini.com/save" />
<
/p>
<
/form>
<
form method="post">
<
p class="submit">
<
input name="reset" class="button-primary" type="submit" value="http://www.srcmini.com/Reset" />
<
input type="hidden" name="action" value="http://www.srcmini.com/reset" />
<
/p>
<
/form>
<
/div>
<
?php
}add_action('admin_menu', 'mytheme_add_admin');
//Scripts to load WP's Media Library panel
//http://www.webmaster-source.com/2010/01/08/using-the-wordpress-uploader-in-your-plugin-or-theme/function my_admin_scripts() {
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_register_script('my-upload', trailingslashit( get_stylesheet_directory_uri()).'scripts/invoke_uploader.js', array('jquery', 'media-upload', 'thickbox'));
wp_enqueue_script('my-upload');
}function my_admin_styles() {
wp_enqueue_style('thickbox');
}if (isset($_GET['page']) &
&
$_GET['page'] == 'functions.php') {
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');
}//test functions
function test1() {
echo '<
!-- logo stylesheet -->
';
}function test2() {
echo '<
!-- reset stylesheet -->
';
}//if ( $_REQUEST['saved'] ) {
//add_action('wp_head', 'test1');
//} elseif ( $_REQUEST['reset'] ) {
//add_action('wp_head', 'test2');
//}?>
我也乐于接受其他替代方法。
#1经过仅两天的反复试验, 看来我找到了解决方案, 或更确切地说, 是解决此问题的方法。我遵循了有关如何从主题选项页面内切换样式表的教程。该教程称为” 将样式切换器添加到WordPress主题” 。在本教程中, 作者从选择下拉菜单切换外部样式表。在我的代码中, 我将外部样式表换成了嵌入式样式表。就我上面的问题而言, 这将是test1和test2函数。
代码示例为:
【保存/重置主题选项页面时,如何获取备用样式表加载到wp_head中()】在顶部添加一个新数组
array ( "name" =>
"Restore CSS", "desc" =>
"Restore the original heading text", "id" =>
$shortname."_restore_css", "type" =>
"select", "options" =>
array ("select an option", "test2"), "std" =>
"Logo CSS" ),
然后添加一个新功能, 在本例中为css_switcher
function css_switcher() { //opens css_switcher functionglobal $options;
foreach ($options as $value) {
if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std'];
} else { $$value['id'] = get_settings( $value['id'] );
}
}?>
<
!-- logo stylesheet goes outside of switch statement -->
<
?php switch ($co_restore_css) {//opens switch statement
case "select an option": //nothing goes between this closing php tag and
// next opening php tag, to make select an option do nothing
?>
<
?php break;
case "Restore CSS": ?>
<
!-- restore stylesheet goes inside of switch statement -->
<
?php break;
}//closes switch statement}//closes css_switcher_functionadd_action('wp_head', 'css_switcher');
?>
这对我有用。
推荐阅读
- 如何通过相对URL引用WordPress主题内的文件夹而没有PHP函数()
- 通过插件劫持get_template_part
- 隐藏”发布的帖子”。在WordPress中查看”发布”管理员通知
- 在WordPress中隐藏/删除自定义帖子类型选项
- 仅使用get_terms获得类别名称
- 获取并显示附带的视频wordpress
- esc_url()和WordPress安全性()
- 根据类别WordPress获取帖子
- 回溯算法小细节