保存/重置主题选项页面时,如何获取备用样式表加载到 wp_head 中?

发布于 2024-11-10 10:02:48 字数 7067 浏览 0 评论 0原文

我希望能够获得一些帮助,了解如何将两个不同的嵌入样式表加载到 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="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="<?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="Save changes" />
<input type="hidden" name="action" value="save" />
</p>
</form>
<form method="post">
<p class="submit">
<input name="reset" class="button-primary" type="submit" value="Reset" />
<input type="hidden" name="action" value="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');
//}

?>

我也愿意接受其他方法来做到这一点。

I was hoping I could get some help on how to load two different embedded stylesheets into the head tag (wp_head action hook), depending on if a user clicks save or reset in a theme options page.

The way it is set up now, only the options specified in the array at the beginning (the options typed in by the user) are saved, but not the alternative stylesheet. Right now I have placeholder functions called test1() and test2() in the place of the actual stylesheets that would be loaded into wp_head. Below is the code I have. The parts that are commented out below the test functions are what I tried to get this functionality to work.

<?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="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="<?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="Save changes" />
<input type="hidden" name="action" value="save" />
</p>
</form>
<form method="post">
<p class="submit">
<input name="reset" class="button-primary" type="submit" value="Reset" />
<input type="hidden" name="action" value="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');
//}

?>

I'm open to alternative ways of doing this as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑶笙 2024-11-17 10:02:48

看起来经过两天的反复试验,我找到了解决方案,或者更确切地说,找到了解决这个问题的方法。我遵循了有关如何从主题选项页面内切换样式表的教程。该教程名为将样式切换器添加到您的WordPress 主题。在本教程中,作者从选择下拉菜单中切换外部样式表。在我的代码中,我将外部样式表替换为嵌入式样式表。就我上面的问题而言,这将是 test1 和 test2 函数。

代码示例是:

在顶部添加一个新数组

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 function

global $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_function

add_action('wp_head', 'css_switcher');
?>

这对我有用。

It looks like after only two days of trial and error, I found a solution, or rather, a workaround to this problem. I followed a tutorial on how to switch stylesheets from within a theme options page. The tutorial is called Add a Style Switcher to your WordPress Theme. In this tutorial the author switches external stylesheets from a selection drop down menu. In my code, I swapped out the external stylesheets for embedded stylesheets. In the case of my question above, that would be the test1 and test2 functions.

A code example would be:

Add a new array at the top

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" ),

Then add a new function, in this case called css_switcher

function css_switcher() { //opens css_switcher function

global $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_function

add_action('wp_head', 'css_switcher');
?>

This worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文