Dynamic Styles Wordpress - php

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?

Related

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;

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
}

Moodle form not showing when I make moodle header footer hidden

I have moodle page like the following with a form inside it:
<?php
require_once('../../config.php');
global $DB;
global $COURSE;
$courseid = $COURSE->id;
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('invalidcourse', 'block_eparticipation', $courseid);
}
require_login($course);
$PAGE->set_url('/blocks/eparticipation/view.php', array('id' => $courseid));
$PAGE->set_pagelayout('standard');
echo $OUTPUT->header();
require_once(dirname(__FILE__).'/epoll_form.php');
$pollform = new mform("poll_action.php?val={$valid}");
$pollform->display();
echo $OUTPUT->footer();
?>
I need to hide the header and footer so I did as follows:
echo "<div style='display:none;'>";
echo $OUTPUT->header();
echo "</div>";
.............................
echo "<div style='display:none;'>";
echo $OUTPUT->footer();
echo "</div>";
Or we can use : hide_header();
But When I did this my form also get hidden. I am getting a blank page.
I want to hide the header and footer only with my form not hidden.
Is there any alternatives to hide moodle header and footer?Please guide me.
Moodle Version : 2.9
Call:
$PAGE->set_pagelayout('popup');
Before you call echo $OUTPUT->header(). This will hide the header and the page blocks.
You must call echo $OUTPUT->header() on every Moodle page, as that includes the HTML head tag, with all the required CSS, javascript, etc needed by Moodle (as well as the visible 'header' elements).
See: https://docs.moodle.org/dev/Page_API for full details of the $PAGE variable.

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?

Wordpress - Less And 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();

Categories