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 settings. 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.
Related
I'm a novice php learner, I was experimenting how to link different php files dynamically. While experimenting, I realize I can create variables in my php files and make my template files echoes out the html I need without editing my template files......
for example:
Within about-me.php page, I have included my header.php and footer.php using
<?php include ('includes/header.html'); ?>
<?php include ('includes/footer.html'); ?>
then I create a variable
$page_title = 'CompanyABC';
and echo out in the header.php
$page_title = 'South Asia Exact';
Now my question is can I do this to my inline css also?
for example, I have create a variable, that store all my inline css:
$page_inlinecss = "#SAEcontentR div#certification_certificate {
margin:0 auto 0 auto;
width:580px;
height:464px;
}\n";
then I echo out in my header.php like so:
<style type="text/css">
<?php echo $page_inlinecss; ?>
</style>
I have tried it and it works, but I want to know is it the right way to do it?
There isn't a right way to do inline CSS
Your code will work, it will produce a valid page, and it will look absolutely fine to the user. BUT you shouldn't do it that way.
So, why shouldn't you do it that way?
Maintainability is the main reason that you shouldn't handle CSS this way. It is far easier to manage a separate CSS file than to pick through PHP code looking for CSS rules to change.
It looks like the data you're storing is static, the point of a variable is to store data that can change. Things like the name of the website (Company ABC) are unlikely to change during the execution of the script, so you should include them in the static HTML template.
On top of this are issues like caching (most browsers cache .css files, saving you bandwidth) and accessibility (screen readers may not know how to deal with inline styles & js).
How should you handle dynamic styles?
One way to handle dynamic styles (that is -- styles based on information which will be different on different page loads) with a combination of PHP and CSS is to define class styles in your external document and then use PHP to apply them.
For example, put this in styles.css:
span.greentext { color: #0f0; }
And this in your PHP file:
<span class='<?php echo ($someCondition) ? "greentext" : null; ?>'>Some text</span>
Or, if you have more styles to handle:
Alternatively, you could load a specific stylesheet upon a condition:
<?php if($someCondition): ?>
<link rel="stylesheet" href="styles/conditional.css" type="text/css" media="screen">
<?php endif; ?>
Hope this helps, and please don't use inline CSS, or variables, unless necessary. You'll thank yourself for it when you have to change the site 5 months down the line.
Can you do this? Yes.
Should you do this? Ehh. (No. was a bit harsh...)
Better to store the CSS filename in a php variable, then in the header add:
<link rel="stylesheet" href="<?php echo $this_page_style_sheet; ?>" />
There is no right or wrong in this case.
You may store the CSS in a string and echo it as you see fit. Or you may even embed it in your includes/header.html file. It's up to you.
Personally, if it is a collection of CSS rules, I would keep it in its own CSS file, and just echo the filename when needed.
$css_filename = "/path/to/rules.css";
// ... etc etc
<link rel="stylesheet" href="<?php echo $css_filename; ?>">
This is a beauty and a pitfall of the way the system works. You can do that, it works and it doesn't seem to present any immediate and glaring security issues. I don't know if that was an intended use of PHP, but it works so if it fits your situation you can use it. The pitfall comes when enough of these little workarounds are used that eventually a security issue could arise somewhere, but I don't recall CSS ever being used as a vector for an attack.
You can do this to generate dynamic css
file css.php
<?php
header("Content-Type: text/css");
echo 'p {color:red}';
?>
html (not complete but it should work cross browser)
<link rel="stylesheet" href="css.php" type="text/css" />
<p>This should be red</p>
Some more strict/uptight folks might say that proper CSS doesn't need variables, yadda yadda.
Personally I think if this works, then it's a clever way to add some ease-of-use to CSS. I'm all for it.
I've installed PixelPost (http://www.pixelpost.org/) photo blog script , and I'm using a template which has two CSS files , [Dark + Light] ,
The problem is here ; How should I make the dark CSS style default , in the file "browse_template" it has the following codes :
<link rel="stylesheet" type="text/css" href="templates/simple/styles/dark.css" title="dark" />
<link rel="alternate stylesheet" type="text/css" href="templates/simple/styles/light.css" title="light" />
which was vice versa , I mean the Alternate stylesheet was dark , I did change this to the above code but still the light style is defualt
here's the full template : Template Link
I'm not sure to understand correctly your question but i'll try to explain.
Everything happens on styleswitcher.js
At first load (never seen the page):
The js file will determine your "favourite" stylesheet following these conditions :
It's a <link> tag containing rel=*style*
It does NOT contain "alt" string in rel attribute.
It has a title attribute.
So ! With your code, the "dark" theme will be selected at first start.
When you leave the page, the js file is saving the selected favourite style into a cookie.
On next loads
The JS file is reading the previously saved cookie and show the last saved style by priority.
If you want to check the correct behaviour of your script, you must delete cookies on this domain, then refresh the page. It should see the "Dark" style at first.
Another option to check is to run your Chrome/Firefox in private browsing mode
Hope this solves your issue
I'm rather new to PHP programming but I thought I'd do it right from the beginning, so I came across this fine pdf Web Performance Boot Camp where he suggests:
All sites should always prepare for CDNized static content
and this is how:
<img src=”<?php echo CDN(‘/i/left-menu-background.gif’) ?>”
etc., he also gave an example of how the CDN function? should look like:
sub CDN { return #_[1]; }
or (when you finally have your static content on a CDN)
sub CDN { return ‘http://s.company.net’ . #_[1]; }
(but that's not valid php, right? it looks more like perl...)
Anyway, this goes on with how to rewrite the header like:
<link type="text/css" rel="stylesheet" href="<?php echo $this->CDN("c/".$this->css_file) ?>" />
But honestly, I have no idea how to do it right. So, my question is, how to I prepare my (php) site for a CDN? Where do I put the sub CDN function? How should it look in valid php? How/Where do I include it? Do I have to put a
<?php require('../cdn.php'); ?>
at the beginning of every html/php file I create (that uses scripts/css/static images/etc.)? Thanks for reading this.
If you're expecting to use a CDN in the future, this is not a stupid idea.
A simple function would look like this:
function getURL($url) // Name it whatever you want
{
// Choose one of the following:
return $url; // If you're local
return "http://s.company.net/".$url; // If you're on a CDN or static server
}
and the markup:
<link type="text/css" rel="stylesheet" href="<?php echo getURL("c/".$this->css_file) ?>" />
Do I have to put... at the
beginning of every html/php file I
create (that uses scripts/css/static
images/etc.)?
Yes. It might be wise to include some sort of central bootstrap file (some frameworks call it bootstrap.php) for future shared PHP settings that you may need to introduce. That bootstrap file would then, in turn, include the cdn.php.
I'm building a small site, that needs to support 2 languages for the same page.
Each language has different buttons on the page, the buttons are basically some images with text inside.
The positioning of the buttons is done using a CSS file, one for every language.
Question is how do I implement the changing of button for every image. I could put a php if statement, and use some other src if lang == English or something else if lang == Russian, Or could implement this with the css file (I could do this with a div and set it's background in the CSS)? What would you recommend?
Cheers.
You could create three CSS files:
One for english buttons
One for russian buttons
One for the rest
This would basically work this way: You create your buttons with a fallback name, for example
<button id="button_ok">OK</button>
and define different background images in your english/russian CSS files:
(english.css)
#button_ok {
background-image: url(images/buttons/eng/ok.gif);
}
and (russian.css)
#button_ok {
background-image: url(images/buttons/rus/ok.gif);
}
All other elements that do not change when the language is chosen come in the third file:
p {
font-size: 1em; /* whatever*/
}
The last step is to choose at the top of every page which file you want to load:
if ($_GET['lang'] == 'eng')
$cssFile = 'english.css';
elseif ($_GET['lang'] == 'rus')
$cssFile = 'russian.css';
and include the special and the general css file in your head:
<link rel="stylesheet" type="text/css" href="general.css" />
<link rel="stylesheet" type="text/css" href="<?php echo $cssFile; ?>" />
I would recommend using CSS by setting the background and size of the button.
use < button> tag, its better because you can use css like "button{}" without having to put a class on it. Just a remind that < button> has a type like the input and it is "submit" as default if I remember correctly.
Btw, always try to depend on CSS instead of PHP, because on a next manutenance you will be glad that you did it.
or, if you going to use PHP, try using a function instead of hardcoding, even if it is just for one button, we never know if we will use it later.
hope it helped,
Joe
I need to use drupal_add_css to call stylesheets onto single Drupal 6 pages. I don't want to edit the main theme stylesheet as there will be a set of individual pages which all need completely new styles - the main sheet would be massive if i put it all in there.
My solution was to edit the page in PHP editor mode and do this:
<?php
drupal_add_css("/styles/file1.css", "theme");
?>
<div id="newPageContent">stuff here in html</div>
But when I view source, there is nothing there! Not even a broken CSS link or anything, it's just refusing to add the CSS sheet to the CSS package put into the page head.
Variations don't seem to work either:
drupal_add_css($path = '/styles/file1.css', $type = 'module', $media = 'all', $preprocess = TRUE)
My template header looks like this, I've not changed anything from the default other than adding a custom JavaScript.
<head>
<?php print $head ?>
<title><?php print $head_title ?></title>
<?php print $styles ?>
<?php print $scripts ?>
<script type="text/javascript" src="<?php print base_path() ?>misc/askme.js"></script>
<!--[if lt IE 7]>
<?php print phptemplate_get_ie_styles(); ?>
<![endif]-->
</head>
Why is this function not working?
It is not quite clear where you are selecting the template that you have in your example. If you are selecting it from a module then you can just use drupal_add_css in the module rather than the template.
If you have your own theme you can use template_preprocess_page and put logic in there to add the relevant CSS (you can also use it to select the template to use).
I have noticed something weird and it might fix your problem:
drupal_add_css( drupal_get_path('theme','themname') . '/working.css','module' ,'all' , false );
drupal_add_css( drupal_get_path('theme','themname') . '/path/to/folder/notworking.css','module' ,'all' , false );
The first one will work ebcause the style it in the main them folder
The second line will not work because the style is in a sub folder !
Edit:
i think it did not work because i did not write the path the the style file properly :S so please disregard my answer
drupal_add_css( drupal_get_path('theme','test') . '/pages/subpage/style.css','theme');
is working
This function wont work in templates. The reason is that the variable $styles which will hold all the stylesheet html will already have been generated at this point, so drupal_add_css wont work as it adds to that. if you want to do this in your theme, you would probably have to add the css file manually
<link rel="stylesheet" ... />
The other way would be to use drupal_add_css in a module, but you might have a hard time adding the correct css files on the pages you want.
It's possible to use drupal_add_css() inside your template.php file; this page has a good example of how to do just that.
Thanks for the link, wyrmmage. That's very useful. I think the rest of the code in the page is unneccessary. You probably just need these since drupal 6 already automatically check for file existence:
drupal_add_css(path_to_theme() . '/css/yourcss.css', 'theme');
// Add the following to regenerate $styles.
// This is needed for template_preprocess_page() since css is already generated at this point.
$variables['styles'] = drupal_get_css();
Answer was very much to use the CSS Injector module - great little addon!
Here is an excerpt from its project page:
Allows administrators to inject CSS into the page output based on configurable rules. It's useful for adding simple CSS tweaks without modifying a site's official theme. The CSS is added using Drupal's standard drupal_add_css() function and respects page caching, etc. The 2.x brach leverages CTools so CSS can be included with Feature changes (ie. CSS that improves the look of a View can be packaged with the View).
This code inside template.php works for me:
function alagna_preprocess_page(&$vars) {
drupal_add_css(path_to_theme() . '/header_1.css', 'theme');
$vars['css'] = drupal_add_css();
$vars['styles'] = drupal_get_css();
}
explained:
alagna is the theme name
header_1.css is the css file required.
drupal_add_css is expecting a path relative to base path whereas drupal_get_path does not return the path relative to base path.
global $base_path;
drupal_add_css($base_path . drupal_get_path('module / theme','name') . "/styles/file1.css", "module / theme");
You can choose between module and theme accordingly.
Try this
common.inc drupal_get_css($css = NULL)
Parameters
$css: (optional) An array of CSS files. If no array is provided, the default stylesheets array is used instead.
$css = $vars['css'];
// unset the system css files
$unset_css = array
'modules/system/system.css',
'modules/system/system-menus.css',
);
foreach($unset_css as $css_f) {
if(isset($css['all']['module'][$css_f])) {
unset($css['all']['module'][$css_f]);
}
}
// add css
$css['all']['theme'][drupal_get_path('theme', 'openpublish_theme') . '/css/style.css'] = true;
$vars['styles'] = drupal_get_css($css);