Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Custom Theme Styles Issue - using style.php
#1
To the smart one that figures this out, you have my thanks upfront:

I have a custom style page for my theme. Using a PHP page instead of a traditional CSS page, just like it is done in "/admin/template/style.php"

MY REASONS: I want to be able to dynamically load the variables from the theme's XML data file into this page. Just like it is done by GETSIMPLE already on "/admin/template/style.php"

I am adding including this in my theme header like so:

---------------------------------------------------------------------------------
<link rel="stylesheet" type="text/css" media="screen" href="<?php get_theme_url(); ?>/style.php">
---------------------------------------------------------------------------------

In the content of the PHP file I define the CSS header so it is treated like a CSS page when echoed like so:

---------------------------------------------------------------------------------
<?php
header("Content-type: text/css");

function getXML($file) {
$xml = file_get_contents($file);
$data = simplexml_load_string($xml, 'SimpleXMLExtended', LIBXML_NOCDATA);
return $data;
}
$v = getXML(GSDATAOTHERPATH.'ThemeSettings.xml');
$customvariable1 = $v->themevariable1;
$customvariable2 = $v->themevariable2;
$customvariable3 = $v->themevariable3;
$customvariable4 = $v->themevariable4;
?>

#customdiv {
background-color: #<?php echo $customvariable1; ?>;
}
---------------------------------------------------------------------------------

MY ISSUE:
I cannot seem to display the variables defined. I have narrowed my errors or issues to 2 main things if I am correct.

1. The style.php file does not have permissions to read the theme's xml data file.
2. The style.php file is not properly registered within GETSIMPLE so it will not work properly or it is not recognized as a file to access data safely.
3. There are some GETSIMPLE tags that needs to be added to the header or somewhere to validate this page as part of the theme files.

The funny thing I cannot wrap my brain around is that, multiple files use theme XML data variables: functions.php, header.php, template.php e.t.c

I need help I have been at this for a hours, reading documentations and so on. I need you smart guys out there to come to my aid. Thanks-a-bunch.
Reply
#2
Include the file in your template somewhere in the header

Code:
<? include get_theme_url(FALSE).'/style.php'; ?>

Then use heredoc syntax

http://www.php.net/manual/en/language.ty...ax.heredoc

to output your block of CSS code.

Your variables should then be available to your template.


Code:
<?php

$v = getXML(GSDATAOTHERPATH.'ThemeSettings.xml');
$customvariable1 =  $v->themevariable1;
$customvariable2 =  $v->themevariable2;
$customvariable3 =  $v->themevariable3;
$customvariable4 =  $v->themevariable4;

echo <<< EOT
<style type="text/css" >
#customdiv {
      background-color: #$customvariable1;
}
</style>
EOT;

Should work, Hope it helps,
My Github Repos: Github
Website: DigiMute
Reply
#3
Nope did not work. I copied word for word what you had listed also put the include in header and it did not work. Also I noticed it will not even recognize plain text in the style.php without this are the header:

------------------------------------------------------------------------------

function getXML($file) {
$xml = file_get_contents($file);
$data = simplexml_load_string($xml, 'SimpleXMLExtended', LIBXML_NOCDATA);
return $data;
}

------------------------------------------------------------------------------

But does not translate to what I need. My variables are not echoing to the page.

Also since this will be an external style sheet it will not be needing the <style> declaration at the top since the header is declared in the php as such

------------------------------------------------------------------------------

header("Content-type: text/css");

------------------------------------------------------------------------------

Do you have a working version you can share? Also if you have GETSIMPLE which I assume we all do on this forum, just take a look at your "/admin/template/style.php" page to see what I am trying to recreate but on the website theme area not the admin.

Seems GETSIMPLE has this working already without EOT<<< so I am not sure I need that to work, please any help will be appreciated. Thanks


n00dles101 Wrote:Include the file in your template somewhere in the header

Code:
<? include get_theme_url(FALSE).'/style.php'; ?>

Then use heredoc syntax

http://www.php.net/manual/en/language.ty...ax.heredoc

to output your block of CSS code.

Your variables should then be available to your template.


Code:
<?php

$v = getXML(GSDATAOTHERPATH.'ThemeSettings.xml');
$customvariable1 =  $v->themevariable1;
$customvariable2 =  $v->themevariable2;
$customvariable3 =  $v->themevariable3;
$customvariable4 =  $v->themevariable4;

echo <<< EOT
<style type="text/css" >
#customdiv {
      background-color: #$customvariable1;
}
</style>
EOT;

Should work, Hope it helps,
Reply
#4
This should work exactly as I said, just a small typo though extra space between the <<< and the EOT
so the new style.php is

Code:
<?php

$v = getXML(GSDATAOTHERPATH.'ThemeSettings.xml');
$customvariable1 =  $v->themevariable1;
$customvariable2 =  $v->themevariable2;
$customvariable3 =  $v->themevariable3;
$customvariable4 =  $v->themevariable4;

echo <<<EOT
<style type="text/css" >
#customdiv {
      background-color: #$customvariable1;
}
</style>
EOT;

Include the file in your template like so:

Code:
<?php include 'style.php'; ?>
My Github Repos: Github
Website: DigiMute
Reply
#5
Doing it like this, as in your original post and how GS backend does it

Code:
<link rel="stylesheet" type="text/css" media="screen" href="<?php get_theme_url(); ?>/style.php">

renders the stylesheet and uses the variables internally and will not be available to other scripts.

So you have a choice of doing it the way GS does it and render the stylesheet with your variables.
Then you could read in the file again in your template to retrieve the variables for your theme.

or

Do it this way and have your stylesheet inline but your variables available to the template/theme.

Hope it helps...
Mike.
My Github Repos: Github
Website: DigiMute
Reply
#6
Thanks for the attempt n00dles101 your way worked, but it loads all the css content right in the head aka <style> </style> by a simple include, I can do the same thing just having the styles in header.php. But my agenda was to keep all styles external and not inline on the page. I wish I could make this work the way GETSIMPLE has it in admin "admin/template/style.php".

I have tried all humanly possible tricks to define and re-read the contents again into the page from style.php rendered as a css page with the header and just referenced in my original post, but that is not working.

The variables are globally defined as you can see in my original post so I don't know why they do not work in style.php rendered as a css page. I know this is my last attempt at this and many thanks for trying, but do you or anyone know how to recreate the exact same thing done by GETSIMPLE in the "admin/template/style.php" but for "theme/mytheme/style.php" and when link via CSS external on "theme/mytheme/header.php" it reads variables and posts their values from the xml.

A sample working code will be awesome to post here so I can read it.

Just like my original post but here is a copy of the codes again:

-----------------------------theme/mytheme/header.php-----------------------------------

<link rel="stylesheet" type="text/css" media="screen" href="<?php get_theme_url(); ?>/style.php">

----------------------------------------------------------------------------------------------------

------------------------------theme/mytheme/style.php-------------------------------------
<?php
header("Content-type: text/css");

function getXML($file) {
$xml = file_get_contents($file);
$data = simplexml_load_string($xml, 'SimpleXMLExtended', LIBXML_NOCDATA);
return $data;
}
$v = getXML(GSDATAOTHERPATH.'ThemeSettings.xml');
$customvariable1 = $v->themevariable1;
$customvariable2 = $v->themevariable2;
$customvariable3 = $v->themevariable3;
$customvariable4 = $v->themevariable4;
?>

#customdiv {
background-color: #<?php echo $customvariable1; ?>;
}

----------------------------------------------------------------------------------------------------

All the same thanks for showing me another PHP technique I did not know till now. I am always looking to expand my knowledge on the language.


n00dles101 Wrote:Doing it like this, as in your original post and how GS backend does it

Code:
<link rel="stylesheet" type="text/css" media="screen" href="<?php get_theme_url(); ?>/style.php">

renders the stylesheet and uses the variables internally and will not be available to other scripts.

So you have a choice of doing it the way GS does it and render the stylesheet with your variables.
Then you could read in the file again in your template to retrieve the variables for your theme.

or

Do it this way and have your stylesheet inline but your variables available to the template/theme.

Hope it helps...
Mike.
Reply




Users browsing this thread: 1 Guest(s)