Wordpress - Less And PHP - php

I'm trying to figure out how to make a mixins.less file using PHP variables.
I made an Admin page and inside it I have colour selector. I want to use that colour inside my less files.
#colour: $mycolour;
I'm thinking to write a din_mixinds.less file using PHP and inside it to add the text;
And the to include the files in my style.less.
Or how can I do something better like this:
<?php
header("Content-type: text/css; charset: UTF-8");
$brandColor = "#990000";
$linkColor = "#555555";
$CDNURL = "http://cdn.blahblah.net";
?>
But with less not css.
Thank you
EDIT:
Based on Bass Jobsen answer.
Gives me an error:
<?php
header("Content-type: text/css; charset: UTF-8");
?>
body{
background-color: <?php echo 'red'; ?>;
}
Doesn't return any errors:
<?php
header("Content-type: text/css; charset: UTF-8");
?>
body{
background-color: red;
}

Well technical you can create a less.php file:
<?php
header("Content-type: text/css; charset: UTF-8");
$brandcolor = "#ff0099";
echo "#brandcolor: $brandcolor;";
And than serve that file on a URL (web server) and write in your Less code:
#import (less) url('http:/localhost/less.php');
p{
color: #brandcolor;
}
update
Yes ... I use less.php
I think that creates a different situation, with less.php you can use the following code to solve your issue:
$parser = new Less_Parser();
$parser->parseFile( 'style.less', 'http://example.com/mysite/' );
$parser->ModifyVars( array('brandcolor'=>'#ff00ff') );
$css = $parser->getCss();

Related

Dynamic Styles Wordpress

I want to add dynamic styles to wordpress using a value from a field in a custom option page
I have created a file called it dynamic_style.php and added following:
<?php
header("Content-type: text/css; charset: UTF-8");
$brandColor = "<?php echo get_option('blank-body-color'); ?>";
?>
body {
background-color: <?php echo $brandColor; ?>;
}
it will work only if the value is css like following:
$brandColor = "green";
but it wont work if its the value from the field which is php like following:
$brandColor = "<?php echo get_option('blank-body-color'); ?>";
What is the best practice to add dynamic styles to wordpress from a field value with php?

using php in css ,substr_count() seach file location

i have some question about php in css
my projet folde
What i'm trying to do : count <li> with php in css
Probleme : cannot find the right location
more information : <link rel="stylesheet" href="./css/style.php"> in my "head.php"
in my style.php
<?php
header("Content-type: text/css; charset: UTF-8");
$menu="../index.php";
$li=substr_count($menu,"<li>");
echo $menu;
?>
i tried "../index.php"; "../inc/menu.php"; "./inc/menu.php";
no one work
you can try something like this
$menu = file_get_contents('../index.php');
$liCount = substr_count($menu,'<li>');
echo $liCount;

WKHTMLTOPDF (knplabs - snappy) not showing header nor footer page

I'm using wkhtmltopdf to generate my pdf files. I've left the generation alone for some time and for whatever reason it's not generating the header and footer anymore.
Things I've tried so far (will update this when more answers come in):
Having the doctype, html, head and body tags ion the header and footer
Chaning the paths to the header and footer to absolute paths (Even using complete absolute paths from the drive forward C:/xampp... does not work.) It might be worth pointing out that changing the filename to something that doesn't exist does not throw an error. So I don't know if it finds the files. Maybe someone can tell me a good way to test this?
This is my header file:
<!DOCTYPE html>
<html>
<head>
<title>PDF header</title>
<style>
html {
display: block;
}
body {
font-family: Calibri, "Segoe Ui Regular", sans-serif;
letter-spacing: 0px;
}
</style>
</head>
<body style="padding-top: 30px">
<img src="../../images/logo_extra.jpg" style="width: 100%;"/>
</body>
</html>
This is my main file:
<?php
session_start();
require __DIR__ . '/../vendor/autoload.php';
use Knp\Snappy\Pdf;
$pdf = new Pdf('pdf\wkhtmltopdf\bin\wkhtmltopdf');
header('Content-Type: application/pdf');
// header('Content-Disposition: attachment; filename="offerte.pdf"');
$pdf->setOption('header-html', 'pdf/header.html');
$pdf->setOption('footer-html', 'pdf/footer.html');
$pdf->setOption('load-error-handling','ignore');
// I know there is a 'cover' function in WKHTMLTOPDF
$file = file_get_contents('pdf/cover.php');
echo $pdf->getOutputFromHtml($file);
?>
And as always, please:
Give me an explanation and maybe an example but not just a bunch of working code!
PS: If you see any other mistakes, please let me know.
wkhtmltopdf has an issue with header/cover/footer. I didn't dig into it very deep as adding margins did solve it for me:
<?php
session_start();
require __DIR__ . '/../vendor/autoload.php';
use Knp\Snappy\Pdf;
$pdf = new Pdf('pdf\wkhtmltopdf\bin\wkhtmltopdf');
header('Content-Type: application/pdf');
//just set margins
$pdf->setOption('margin-top', 20);
$pdf->setOption('margin-bottom', 15);
$pdf->setOption('margin-left', '0');
$pdf->setOption('margin-right', '0');
$pdf->setOption('header-html', 'pdf/header.html');
$pdf->setOption('footer-html', 'pdf/footer.html');
$pdf->setOption('load-error-handling','ignore');
$file = file_get_contents('pdf/cover.php');
echo $pdf->getOutputFromHtml($file);
?>
Description
Second problem is weird - nonexisting filename should throw an error. Comment out header and try then with false filename, snappys AbstractGenerator should say something...

get back PHP variable from php CSS file

My problem is that I cannot access a PHP variable from a CSS file loaded like this from index.php:
<link href="css/style.php" rel="stylesheet">
In the style.php file, I have this:
<?php header("Content-type: text/css; charset: UTF-8");
$myClassName = 'myClass'; ?>
.<?= $myClassName?> {
font-weight: bold;
}
in my index.php I have this:
<span class='<?= $myClassName?>'>this is a text</span>
But $myClassName return an empty string like it doesn exists...it does mean that I cannot access the PHP variable like this....is there someone have maybe a trick..?
I really need to set the css classnames with PHP variables from the css file and be able to get them back to my index.php
Under the header, do $css = $_GET['css']; or replace it with wherever you are initializing the variable from.For example:
<?php
header('Content-Type: text/css');
$css = $_GET['css'];
?>
body {
<?= $css ?>border-radius: 3px
}

Is there any other method to output variable content in php?

I did a setup of scssphp in my framework and made it working with multiple files. Below is the code of how it output itself as text/css.
<?php header('Content-type: text/css');
ob_clean();
function scss_css() {
require "../SCSS-PHP/scss.inc.php";
$import_way = "../styles/scss/";
$compression = "scss_formatter_compressed";
$compiler_script = '#import "demo.scss"';
$scss = new scssc();
$scss->setImportPaths($import_way);
$scss->setFormatter($compression);
echo $scss->compile($compiler_script);
}
scss_css();
die();
?>
The call i used in html is:
<link rel="stylesheet" href="binder/loader.scss.php">
I would like to know if echo is safe for my future working scss setup. Can there be any attack on such a file output and any other suggestions to make it better?

Categories