开发者

Add some parameter to all urls in WordPress

开发者 https://www.devze.com 2023-01-26 02:19 出处:网络
How can I add some parameters to all urls on all pages in WordPress? I am working on a theme development and I need to show to a potential customer different variants of using it. So, I have different

How can I add some parameters to all urls on all pages in WordPress? I am working on a theme development and I need to show to a potential customer different variants of using it. So, I have different color schemes. I found a tutorial about how to get parameters from url and use them in my code. Now I easily use an url like http://proceed.skible.com/?color=magic_night to attach a CSS file with color scheme settings. It works fine but when I click any link on a demo page, it obviously doesn't apply my custom color scheme but applies the one that is saved in the set开发者_运维问答tings. I think I can go this way - add ?color=magic_night or whatever color scheme I need to all links. Of course, I need to parse the links and add it right way, not just insert it at the end of every url. More than that, may be there are better ways for implementing the possibility for previewing misc features of the theme? I saw the way I described here: http://themes.mysitemyway.com/infocus/?themedemo=infocus&extracss=deep_blue. All links end with extracss=deep_blue or another theme I choose from the menu. Thanks.


You should use PHP cookies to store the users colour preference between HTTP requests, but allow them to override it using the GET variables you are talking about:

# Get the value of color if specified on the URL (GET) or in a cookie
# If non of those place have the color, then color will be NULL
$color = isset($_GET['color']) ? $_GET['color'] : (
    isset($_COOKIE['color']) ? $_COOKIE['color'] : NULL
);

# If we know what color we have and didn't just get it from a cookie
# then set a cookie with that color as its value
if ($color != NULL && isset(! $_GET['cookie'])) {
    setcookie('color', $color);
}

Now that you have the $color value you can pick your stylesheet in whatever way you want to, for example:

<?php
    if ($color != NULL) {
        ?> <link rel="stylesheet" href="<?php bloginfo('stylesheet_direcctory'); ?>/<?php print($color); ?>.css" type="text/css" /> <?php
    }
?>

P.S. My PHP syntax is a bit rusty, but the concept should be all there


If I'm understanding you right, you want to use a different stylesheet based on what url parameters are passed? You can do that in the header file of your theme:

header.php:

//includes
<html>
...
<head>
...
<? 
 if($_GET['color'] == 'magic_night'){
?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_direcctory'); ?>/magic_night.css" type="text/css" />
<?
}
else
...
?>
...rest of the page...

bloginfo('stylesheet_direcctory') should get you to your theme directory, but I would echo it first in a test file or use a better parameter to bloginfo.

0

精彩评论

暂无评论...
验证码 换一张
取 消