Helping ordinary people create extraordinary websites!

Go Back   Web Development Forum > Website Programming > Server-Side Scripting
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-28-2008, 12:37 AM
ryanhellyer's Avatar
Super Moderator
 
Join Date: Dec 2007
Posts: 663
Default 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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 12:42 AM
ryanhellyer's Avatar
Super Moderator
 
Join Date: Dec 2007
Posts: 663
Default

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" />";}

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 04:13 AM
ryanhellyer's Avatar
Super Moderator
 
Join Date: Dec 2007
Posts: 663
Default

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-02-2008, 04:27 AM
BigAlReturns's Avatar
Moderator Extraordinaire!
 
Join Date: Dec 2007
Location: The Wirral, England
Posts: 298
Send a message via MSN to BigAlReturns
Default

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-03-2008, 12:20 AM
ryanhellyer's Avatar
Super Moderator
 
Join Date: Dec 2007
Posts: 663
Default

It is generated on each page load.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-06-2008, 07:32 AM
Junior Member
 
Join Date: Jan 2008
Posts: 10
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-06-2008, 12:41 PM
ryanhellyer's Avatar
Super Moderator
 
Join Date: Dec 2007
Posts: 663
Default

Yes, that is an option. I assumed there was a more elegant way to do this, but perhaps not
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-06-2008, 12:43 PM
BigAlReturns's Avatar
Moderator Extraordinaire!
 
Join Date: Dec 2007
Location: The Wirral, England
Posts: 298
Send a message via MSN to BigAlReturns
Default

Draicone's solution is what you're looking for - it's the only way to do it really. Elegance is subjective anyway!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-08-2008, 12:01 AM
ryanhellyer's Avatar
Super Moderator
 
Join Date: Dec 2007
Posts: 663
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 03-08-2008, 12:28 AM
Member
 
Join Date: Jan 2008
Posts: 37
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:34 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.