何日もの間、ワードプレスのテーマ カスタマイザー API 内で設定された色とフォントの変数を渡す、より少ないファイルを動的に作成する方法をインターネット全体で探していました。私が思いついた最も近いものは、これらの変数を style.css.php ファイルに動的に挿入する方法です。これは、このcss-tricks 記事のおかげで、css として埋め込まれます。このコードを記述した php ファイルを作成しました。
<?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.
cssとして埋め込まれている場合は機能しますが、 @import "style.css.php"; を試みると機能します。@import "variables.less" のすぐ下の bootstrap.less に。ブートストラップ変数をオーバーライドするために、入力が認識されないため prepros が css ファイルを再コンパイルできず、functions.php で lessphp を有効にすると、次のエラーが表示されます。
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
テーマカスタマイザによって生成されたphp変数をlessに挿入する方法はありますか?
編集:私はここで頭を悩ませているようです。私はlessphpのドキュメントを見ましたが、それを理解していないようです。
これは、functions.phpでlessphp関数を設定する方法です
//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');
}
ここで、デフォルトのブートストラップ変数をオーバーライドするために、bootstrap.less の variables.less の下で、テーマ カスタマイザーによって設定された変数を含むファイルを @import する必要があります。次に、style.css を再コンパイルする必要があります。
@font-family-sans-serif: <?php echo get_theme_mod( 'theme_font' ); ?>;
では、 lessphp で動作する方法でこのようなものを渡すにはどうすれば
よいでしょうか?