PHP: Sending CSS into head instead of body (Joomla extension) - php

I'm using AutsonSlideShow extension for Joomla 1.7 which works just fine. The plugin has its downsides tho, as it writes CSS right into the body of the index.php file. I would like to change this for validation reasons. (There's more validation errors in it btw, if anybody maybe wants to double check that)
Is there any commands or ways to send the whole part of that css-code (includes php variables) to the head instead or make it available externally?
Here is an excerpt from the default.php which is the file that writes into my index.
<style type="text/css">
.box_skitter_large<?php echo $module->id;?> {width:<?php echo $slidewidth;?>px;height:<?php echo $slideheight; ?>px;}
<?php echo $margin;?>
.box_skitter_small {width:200px;height:200px;}
.box_skitter {border:<?php echo $border;?>px solid <?php echo $bordercolor;?>; background:<?php echo $backgroundcolor;?>}
</style>
It's just a cropdown of the whole part, but it's representative. If you check the sourcecode of this demo site of the plugin you can see the problem.
Greetings, Marian

ob_start();
?>
YOUR CSS HERE
<?php
$style = ob_get_contents();
ob_end_clean();
JFactory::getDocument()->addStyleDeclaration($style);

Related

How to adjust HTML when website is in PHP

I have a Wordpress theme for a new website. I want to make some HTML adjustments but I realized that the website has only PHP scripts and of course the CSS file. For instance, I need to adjust the website name but cannot find H1 tags. I do not have any PHP knowledge. Please advice. Many thanks!
Screenshot
<?php echo "<div>html here</div>"; ?>
You can as many php chunks as you want
example :
<?php echo "<h1>html here</h2>"; ?>
<?php echo "<span>html here</span>"; ?>
Wordpress is a bit different from vanilla PHP instead of echoing out HTML with the title:
<?php echo "<h1>The Title</h1>"; ?>
It uses a function/hook called wp_title(); that you can find in header.php.
But to modify it you would have to in to functions.php and edit the function and for that you need some basic PHP knowledge.
Here is some documentation on the wp_title() function/hook.
https://developer.wordpress.org/reference/hooks/wp_title/

PHP echo include variable in <head><title>

I am building a small personal website. To keep things small and to avoid writing things over and over again, I have a single header.php file that every sub-directory index.php file include's. So, every index.php file has these lines:
$title = someTitleVariableOrMethod;
include('/var/testsite/docroot/header.php');
And in my header.php file, I have these lines (I know I could probably improve the formatting, but first I want to get the title working).
<html>
<head>
<?php
if (isset($title)) {
echo '<title>' . $title . '</title>';
} else {
echo '<title>Sampletext</title>';
}
?>
<style>
//a bunch of irrelevant css
</style>
</head>
<body>
//this is the end of the header file, the rest is dealt with in the index.php file
But for some reason, the contents of the title and all my CSS show up at the start of <body> (I see this when I press F12 in browser) and NOTHING at all shows up in <head>. I just want the title contents to be put in the title tag. How could I fix this issue?
Thanks in advance.
Make sure that you are accessing the pages from a web server (e.g., XAMPP - then access via http://localhost/site/index.php). If you try to open them directly from your file system the PHP will simply be shown as you described. Also make sure the index.php has the closing body and html tags.
Sorry for the trouble, I found the issue! I was using passthru to get to content for the variable, which I didn't realize prints to the screen. My mistake!
EDIT: The site says I can't accept this answer for two days, but this is accepted

PHP Variables in CSS not working

I have the following code for my html file:
<html>
<head>
<link rel='stylesheet' href='css/style.php' media = "screen"/>
</head>
<body>
<h1 id="foo">My h1 foo element</h1>
</body>
<html>
And for my php file:
<?php
header("Content-type: text/css; charset: UTF-8");
$asd = '#0000ff';
?>
h1#foo {
color: <?php echo $asd;?>;
}
I've followed some tutorials and this is the simplest one i could make but somehow the output is not working the way it should be. Did i miss anything?
P.S. if i was gonna use php variables in css, can it be sort of dynamic? i mean inside the php body, can i overwrite the value of the php variable used in css and the output would change?
Help would be much appreciated ty!
Works fine for me ^^
Use this syntax:
cssman.php
<?php
ob_clean();
header('Content-Type: text/css');
header('Cache-Control: no-cache, must-revalidate');
error_reporting( 0 );
// These are just to show you can use dynamic values and such:
$type = isset($_GET['type']) ? $_GET['type'] : '';
$theme = isset($_GET['theme']) ? $_GET['theme'] : '';
/** Simply print or echo your css here **/
ob_end_flush();
?>
Try your output first, by navigating to the .php file manually. If there is no content at all, there is most likely a mistake in the PHP code, for debugging you could add error repporting (don't forget to also ini_set('display_errors',1), else errors will only be logged).
Then add it to your view:
your view
<style type="text/css">
#import "/Library/Stylesheets/cssman.php?type=cms" screen;
/* any aditional files here aswell */
</style>
you can do it like
echo"
<style>
h1#foo {
color: ".$asd.";
}
</style>
";
Firstly, to include a php file this syntax is absolutely wrong:
<link rel='stylesheet' href='css/style.php' media = "screen"/>
To include a Css file we use following syntax:
<link rel='stylesheet' href='css/style.css' media = "screen"/>
and include a php file we use:
<?php
include_once "a.php"; // this will include a.php
?>
Instead of using PHP to manage the CSS, you may want to consider one of the CSS preprocessors which are specifically intended for that purpose. It also dissociates your client side code from the server side technology.
http://lesscss.org/
https://learnboost.github.io/stylus/
http://sass-lang.com/
Another approach worth considering is to break up the CSS into several files. You can have a common file that applied to all pages on all devices, one that contains the colors, another that manages the layout, perhaps some device specific ones.
The code you post works as expected. The title with id=foo goes blu. The only problem is that it doesn't use the .css extension for a css file. To solve this you could put in css folder a .htaccess with instructions to Apache Web Server to use Php interpreter also for the css files (look this link).
However probably for dynamic-ly change it from php, you mean change the value (e.g after a user input or some other events).
BUT If I understand well your question the answer is no.
Php can only preprocess your page, it can't modify dynamicly your page after it is loaded by the browser from the user. In addiction, using the code above your variable $asd can only be changed in the style.php AND before that it is used in the code.
I suggest you to use javascript instead, it's a lot easier. Use some javascript library like jQuery, to that kind of job.

External stylesheet (css) in php

I have looked through 4-5 post on stackoverflow regarding this without solving my problem.
I have a php file and want to use the css stylesheet I have for the other HTML files.
Right now it looks like this:
<div style="height: 600px;">
<?php
echo "<link rel='stylesheet' type='text/css' href='stylesheet.css' media='all'/>";
session_start();
$admin = $_SESSION['admin'];
$author = $_SESSION['author'];
?>
<?php if ($admin == true AND $author == true) { ?>
<p id="confirmationText"><?echo "Thank you for your message! <br>You should have received a confirmation email. We will contact you soon."; ?></p>
<?php } ?>
</div>
It is not working; the p id="confirmationText" text does not get formatted. interestingly, when I just open the file locally without apache server, it will get HTML-formatted.
What to do?
CSS style links sometimes don't work properly if they aren't in the <head> ... </head> area. The best practice would be to just declare that echo statement up
there.
Try opening your page in a browser, and clicking 'inspect
element' on that place where your stylesheet is linked, and see if you can see any problems there. Often browsers just render things strangely. It can give your clues as to what's wrong.
Why are you linking the stylesheet before your session_start command? Perhaps this might just be causing an output buffer error. Try putting your link at the end of the PHP code.
Honestly, sometimes even the "best practice" and "cleanup" methods of fixing code, won't work. You can just use a little css hack. It's bad practice and a bit depreciated but I've been writing code for years and this just makes life so much easier: Instead of linking a stylesheet externally .... imbed the style directly.
Add this piece of code to your file:
<style>
<!-- change the path to the correct one, though. I have no clue where your file is, in the filesystem, I'm just guessing, here -->
<?php include($_SERVER['DOCUMENT_ROOT'] . "/stylesheet.css"); ?>
</style>
Can you open the stylesheet in your browser?
Also, why are you echoing a stylesheet in a div, and not in the head?

I need insert some PHP code in style sheet

In my css
div.image {
width:<?php echo get_image_size( $size[1] ); ?>px;
}
The size is stored in an array so i called $size[1] to here...
I am beginner in php...
any one pls help?
Better solution is to set a header for the css / php file in my example cssfile.php:
<?php header("Content-type: text/css"); ?>
Then you can use the php file as style.
<link rel="stylesheet" type="text/css" href="cssfile.php?param=1" />
Then you should get the output on your site. But with that solution you should be very careful if you have a lot of traffic. On every site call your PHP interpreter is used to deliver that file.
Yes for this particular instance if you just need php on this one line it is best to insert in the head of your html instead of having php make a whole css file for you.
<style type="text/css">
div.image {
width:<?php echo get_image_size( $size[1] ); ?>px;
}
</style>
PHP should NOT be creating your static assets unless absolutely necessary. You will notice performance hit if traffic gets high.
Change the extension to .php and then in your stylesheet put:
<?php header("Content-type: text/css"); ?>
Then you can use PHP inside your stylesheet.
Maybe you should use this:
<div class="image">
<img src="image.gif" />
</div>
CSS:
div.image img{
width: 100%
}
The problem with putting style inside html is that everything get messed. You should try to separate html from css and js as much as possible.
Also, from my expirience, this is not a good practice to use php in css or js files.
Consider reviewing your html layout to optimize this.
One way is to move that particular piece out of the .css file, and insert it in a PHP document that has access to the get_image_size() function. For example, you may have a template that is parsed as the header. You could then surround this piece in <style> tags.
For that you need to use like this
change your css file as css.php, and use stylesheet include like this
<link rel="stylesheet" type="text/css" href="css.php?opt1=<?php echo get_image_size($size[1] ); ?>" />
In css.php file
div.image {
width:<?php echo $_GET['opt1']; ?>px;
}
try this

Categories