How to insert php variables into less?

For days I've been looking all over the internet for a way to dynamically create a less file, into which I would pass color and font variables set inside the wordpress theme customizer api. The closest thing I came up with is a way to insert these variables into style.css.php file dynamically, which is then embedded as css, thanks to this css-tricks article. I created a php file into which I wrote this code:

<?php $absolute_path = explode('wp-content', $_SERVER['SCRIPT_FILENAME']); $wp_load = $absolute_path[0] . 'wp-load.php'; require_once($wp_load); $dark_red = get_option('color-1'); $light_red = get_option('color-2'); $white = get_option('color-3'); $dark_grey = get_option('color-4'); $light_grey = get_option('color-5'); header('Content-type: text/css'); header('Cache-control: must-revalidate'); ?> body{ font-family: <?php echo get_theme_mod( 'theme_font' ); ?>; } header{ background-color: <?php echo $dark_red ?>; } etc.

Which works when it is embeded as css, however when I try to @import "style.css.php"; into bootstrap.less, right below the @import "variables.less"; to override the bootstrap variables, prepros can't recompile the css file because of unrecognized input, and when I activate lessphp in my functions.php it gives me this error:

Fatal error: Uncaught exception 'Less_Exception_Chunk' with message 'ParseError: Unexpected input in custom-styles.css.php on line 1, column 0 1| @font-family-sans-serif: <?php echo get_theme_mod( 'theme_font' ); ?>;' in C:\xampp\htdocs\inicijativa\wp-content\themes\liberter-less\less\lib\Less\Parser.php:535 Stack trace: #0 C:\xampp\htdocs\inicijativa\wp-content\themes\liberter-less\less\lib\Less\Parser.php(343): Less_Parser->GetRules('C:/xampp/htdocs...') #1 C:\xampp\htdocs\inicijativa\wp-content\themes\liberter-less\less\lib\Less\Tree\Import.php(277): Less_Parser->parseFile('C:/xampp/htdocs...', '', true) #2 C:\xampp\htdocs\inicijativa\wp-content\themes\liberter-less\less\lib\Less\Tree\Import.php(193): Less_Tree_Import->ParseImport('C:/xampp/htdocs...', '', Object(Less_Environment)) #3 C:\xampp\htdocs\inicijativa\wp-content\themes\liberter-less\less\lib\Less\Tree\Ruleset.php(248): Less_Tree_Import->compile(Object(Less_Environment)) #4 C:\xampp\htdocs\inicijativa\wp-content\themes\liberter-less\less\lib\Less\Tree\Rulese in C:\xampp\htdocs\inicijativa\wp-content\themes\liberter-less\less\lib\Less\Parser.php on line 535

Is there a way to insert php variables generated by the theme customizer into less?

Edit: I seem to be way in over my head here. I looked at the lessphp documentation and I just don't seem to get it.

this is how I set the lessphp function in my functions.php

//compile less function autoCompileLess() { // include lessc.inc require_once( get_template_directory().'/less/lessc.inc.php' ); // input and output location $inputFile = get_template_directory().'/less/bootstrap.less'; $outputFile = get_template_directory().'/style.css'; // load the cache $cacheFile = $inputFile.".cache"; if (file_exists($cacheFile)) { $cache = unserialize(file_get_contents($cacheFile)); } else { $cache = $inputFile; } $less = new lessc; $less->setVariables(array( "font" => get_theme_mod( 'theme_font' ) )); // create a new cache object, and compile $newCache = $less->cachedCompile($cache); // output a LESS file, and cache file only if it has been modified since last compile if (!is_array($cache) || $newCache["updated"] > $cache["updated"]) { file_put_contents($cacheFile, serialize($newCache)); file_put_contents($outputFile, $newCache['compiled']); } } if(is_admin()) { add_action('init', 'autoCompileLess'); }

Now I need to @import file with the variables set by the theme customizer, below the variables.less in bootstrap.less to override the default bootstrap variables, and then I need to recompile the style.css

So, how can I pass something like this @font-family-sans-serif: <?php echo get_theme_mod( 'theme_font' ); ?>; in a way that will work with lessphp?

Category:php Views:49 Time:2019-03-21

Related post

  • How to insert PHP code into WordPress Custom Fields 2010-07-29

    I'm trying to figure out how to put PHP code into a custom field, but so far all that happens is that it displays the code literally, instead of executing it. So, how would I go about getting the PHP I'm inserting into a custom field to actually exec

  • How to pass php variable into a class function? 2011-04-30

    I want to pass php variable $aa into a class function. I have read some articles in php.net, but I still don't understand well. Can anyone help me put the variable into this class? thanks. $aa='some word'; class Action { private $_objXML; private $_a

  • How to insert PHP values into Javascript? 2011-09-18

    Javascript: var counter = 1; var limit = 5; function addInput(divName){ if (counter == limit) { alert("You have reached the limit of adding " + counter + " inputs"); } else { var newdiv = document.createElement('div'); newdiv.innerHTML = " <br>

  • How to insert C++ variable into TCL script program? 2012-01-11

    In NS2, is there any way to embed a C++ variable into the TCL script? For example, in TCL, set routing protocol like this, set opt(rt) XXX XXX could be a variable which is defined in C++ program, such as if(CONDITION==1) XXX = "FLOODING"; if(CONDITIO

  • Less - How to insert an @variable into property (as opposed to the value) 2011-02-25

    In less.js, I'm able to replace values with variables with no problems. @gutter: 20px; margin-left:e(%("-%d"), @gutter); When trying to replace properties with variables, I get errors. How would I perform the following in Less? @gutter: 20px; @direct

  • how to insert multiple variables into a sql query 2012-03-31

    <?php session_start(); include('function.php'); site::db(); $type=$_GET['type']; $date=$_GET['date']; $amount=$_GET['amount']; switch($type) { case "in_park": $table = "lot_income"; $col = "date,amount"; $val = $date $type $amount; break; } $sql =

  • How to insert PHP code into table in database? 2011-08-30

    I'm building an admin panel for some website, and I want to add the option to publish a new page. I want to build some text editor that will allow the publisher add HTML/PHP code. I tried to insert text into dababase table and than print that: EOF; e

  • How to insert PHP script into HTML the best way? 2010-06-06

    I have to insert full path to every single image/css file of my website, because of url_rewriting and I'm looking for the most officiant way to do it. Of course I could do: <img src='<?php echo $full_path; ?>/images/theImg.jpg' alt='alternat

  • how to insert a variable into an xml file before using wget 2010-02-09

    I have the following looop for i in $(cat numbers.txt); do echo $i; wget -a output.txt --no-check-certificate http://localhost:9001 --post-file=netev.xml; done but the netev.xml has a fieled which is defined by the variable $1, this $1 needs to corro

  • Can I insert PHP variables into a JSON string? 2010-02-13

    I am using a REST API to POST attributes to a person using json. My request body looks like this: $requestBody = ' { "attribute": { "@id": "", "@uri": "", "person": { "@id": "222", "@uri": "https://api_name_removed.com/v1/People/222" }, "attributeGro

  • Inserting php variables into sql statements 2010-11-27

    Changing the question as I have better information. The following query function gamesWaiting($join, $type, $status, $u) { global $database; $q = "SELECT g.home_user, g.away_user, g.home_score, g.home_team, g.away_score, g.away_team, g.id, f.name, f.

  • Inserting PHP variable into Html5 "Placeholder" attribute 2012-01-26

    I tried the following, and it shows an empty text area, the value isn't displayed: <input type="textarea" class="class1" name="name1" placeholder="<?= $val1?>" /> Any thoughts on what should be done so it will work? Update: It is now work

  • How to insert a variable into an xslt document function 2012-05-02

    ok, sorry. I have this two xml files. ProvlaLingua1.xml <VENDOR> <IMMAGINI_SITO>True</IMMAGINI_SITO> <VERSIONE_SKYWALKER>1</VERSIONE_SKYWALKER> <CDLINGUA>IT</CDLINGUA> </VENDOR> and this second ProvaLin

  • how to insert a variable into a countif/sumif 2014-06-17

    B1 has a value dependant on what I choose, from 25,000,000 to 200,000,000 (30,000,000 at the moment) A1 - A25 is, but not limited to, as follows: 200,000,000 200,000,000 200,000,000 200,000,000 104,530,445 72,216,324 55,176,707 49,331,951 48,830,955

  • How would I insert a variable into MSSQL Database from php? 2010-12-25

    mssql_query("INSERT into User_Shoutbox(user,message) VALUES('$user','$message')",$dbhandle ); This code inserts the Variables User and Message into the database. But this code keeps giving me an error... message: Incorrect syntax near the keyword 'us

  • How do I insert a $variable into jQuery 2009-12-29

    I'm working in Drupal with jQuery. How do I insert a php $variable into a tag. $(document).ready(function(){ $("#comment-delete-<?php print $variable ?>").click(function(){ $("div.comment-<?php print $variable ?> span.body").replaceWith("

  • Insert PHP variable in INSERT INTO MySQL query 2011-12-15

    I am trying to insert a PHP variable into an "INSERT INTO" MySQL query. I have tried many many combinations with no luck. My code is below: $sql = 'INSERT INTO `mse_names` (`id`, `name`, `day`) VALUES (NULL, ".$testing_for.", \'test_day\');'; mysql_q

  • Rails - How do you insert a variable into a regular expression (Regex) for instance assert_match 2009-12-17

    I want to do something like assert_match /blah blah blah #{@user}/, @some_text but I'm not having any luck with it working. What am I doing wrong here? --------------Solutions------------- That is the correct way to insert a variable into a regex: ir

  • How to insert special characters into a database? 2010-04-06

    Can anyone tell me how to insert special characters into a MySQL database? I've made a PHP script which is meant to insert some words into a database, although if the word contains a ' then it wont be inserted. I can insert the special characters fin

  • How to insert complex strings into Actionscript? 2010-05-21

    How to insert complex strings into Actionscript? So I have a string -vvv -I rc w:// v dv s="60x40" --ut="#scode{vcode=FV1,acode=p3,ab=128,ch=2,rate=4400}:dup{dt=st{ac=http{mime=v/x-flv},mux=mpeg{v},dt=:80/sm.fv}}" How to insert it into code like publ

  • Inserting a variable into jQuery .css 2011-04-25

    I have a JavaScript Variable that stores the width of an element ,called popupWidth, in px, e.g. '200px'. The '' are part of the variable, because jQuery needs them. How can I insert the variable into jQuerys .css? $(this).css({ width: + popupWidth,

  • Read in echoed PHP variable into an iPhone application 2011-06-27

    Possible Duplicate: How to read php variables in iPhone App How can I read in PHP variables into an iPhone application? For example: $query = "SELECT * FROM Messages WHERE Id = 2"; $result = mysql_query( $query ) or die ("didn't query"); $row = mysql

  • How to make PHP variables preserved in CSS stylesheets generated by SASS / SCSS? 2012-02-03

    I'm working on a new big PHP / Symfony 2 project and decided to use Sass / SCSS and Compass which I found both really awesome for managing CSS in a modern way. However, there's still one problem for which I couldn't find any answer: how to place PHP

  • Inserting php code into src of image in jquery 2012-03-09

    I have no idea how to do this. I want to insert php code into an image source through jquery.. but it is not working. Here is my code and help would be greatly appreciated! JQUERY: var mainId = $(this).attr('id'); $("#intabdiv img").attr('src', '<

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.060 (s). 12 q(s)