
02-28-2008, 01:37 AM
|
 |
Super Moderator
|
|
Join Date: Dec 2007
Posts: 685
|
|
PHP in WordPress theme
Okay, I have the following code which I'm trying to use to display a chunk of text in the header of a WordPress theme.
PHP Code:
function hellishsimplicity_head() {echo ' <link rel="stylesheet" type="text/css" href="' , bloginfo('template_directory') , '/style' , get_option('hellishsimplicity_colour') , '.css" />'; $hellishsimplicity_colour = get_option('hellishsimplicity_colour'); }
It currently outputs either red or blue to change the style sheet used in the way shown. However I would prefer to have an IF statement to control the HTML output.
So I tried the following code:
PHP Code:
function hellishsimplicity_head() { if ($hellishsimplicity_colour == "blue") {echo "<link rel="stylesheet" type="text/css" href="' , bloginfo('template_directory') , '/styleblue'.css" />";} else {echo "<link rel="stylesheet" type="text/css" href="' , bloginfo('template_directory') , '/stylered'.css" />";} $hellishsimplicity_colour = get_option('hellishsimplicity_colour'); }
But it's not working  Does anyone know what I'm doing wrong?
|

02-28-2008, 01:42 AM
|
 |
Super Moderator
|
|
Join Date: Dec 2007
Posts: 685
|
|
Lol, well after pounding away at this for the past hour, I figured it out as soon as I posted the question!
The answer was to move the $hellishsimplicity_colour = get_option('hellishsimplicity_colour'); to above my IF statement.
PHP Code:
function hellishsimplicity_head() { $hellishsimplicity_colour = get_option('hellishsimplicity_colour'); if ($hellishsimplicity_colour == "blue") {echo "<link rel="stylesheet" type="text/css" href="' , bloginfo('template_directory') , '/styleblue'.css" />";} else {echo "<link rel="stylesheet" type="text/css" href="' , bloginfo('template_directory') , '/stylered'.css" />";} }
|

03-01-2008, 05:13 AM
|
 |
Super Moderator
|
|
Join Date: Dec 2007
Posts: 685
|
|
Okay, now have another question!
I'm trying to take a string generated in a WordPress plugin file, and output the value from that string to a dynamic PHP driven CSS file.
I've tried specifying the string in the plugin, then displaying it via an echo statement, but that doesn't work at all. And I've also tried turning it into a function and displaying it that way, but still no luck
I'm assuming there is no connection between the two scripts, so a string specified in one is not read into the other. But I have no idea how to link the two together so that the string can be outputted
Any ideas?
|

03-02-2008, 05:27 AM
|
 |
Moderator Extraordinaire!
|
|
Join Date: Dec 2007
Location: The Wirral, England
Posts: 288
|
|
Is the string generated in the plugin file a one time thing, generated only on installation, or could it be a different string on each page load?
|

03-03-2008, 01:20 AM
|
 |
Super Moderator
|
|
Join Date: Dec 2007
Posts: 685
|
|
It is generated on each page load.
|

03-06-2008, 08:32 AM
|
|
Junior Member
|
|
Join Date: Jan 2008
Posts: 10
|
|
Is it possible to pass the string from wordpress to the CSS PHP script via the URL? e.g. within your WP code an echo "<link href=\"style.css?string={$string}\" rel=\"stylesheet\" />";. You could then easily get it via $_GET in your CSS script.
|

03-06-2008, 01:41 PM
|
 |
Super Moderator
|
|
Join Date: Dec 2007
Posts: 685
|
|
Yes, that is an option. I assumed there was a more elegant way to do this, but perhaps not 
|

03-06-2008, 01:43 PM
|
 |
Moderator Extraordinaire!
|
|
Join Date: Dec 2007
Location: The Wirral, England
Posts: 288
|
|
Draicone's solution is what you're looking for - it's the only way to do it really. Elegance is subjective anyway!!
|

03-08-2008, 01:01 AM
|
 |
Super Moderator
|
|
Join Date: Dec 2007
Posts: 685
|
|
Hmm, now that I've come to try and put this into action I've stumbled across the rather obvious problem that it is completely pointless.
By including all the CSS data in the URL, each page load has to download the ENTIRE CSS file as part of the URL, which is obviously not acceptable as I may as well just use <style> tags in my HEAD instead.
So ... how would I go about passing a string which is stored in my WordPress database in my style.php CSS file? Basically I just want to output the entire string of data to the CSS file.
At the moment, my beta version of my plugin inserts the styles via <style> tags but that creates very messy HTML and means the CSS information is downloaded on every page refresh.
|

03-08-2008, 01:28 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 37
|
|
If you are storing your data via the WordPress "option" mechanism, then just add the following code to the top of your php file in your plugin folder to access the string you have saved in the database:
PHP Code:
<?php require('../../../wp-blog-header.php'); ?>
Don't forget to output the file as CSS, not text/HTML or the browser may not render it correctly. The following will output a CSS file which contains your blogs URL. Just replace the bloginfo with whatever code you want to display in the file.
PHP Code:
<?php
require('../../../wp-blog-header.php');
header('Content-type: text/css');
echo bloginfo('url');
?>
__________________
gribbit
|
|