问题描述
我在网上搜索过,但我看到的结果不够清楚。
通过将此代码添加到 functions.PHP 文件中,我能够将 manifest.json 文件添加到我的 wordpress 主题中。
//manifest file
add_action( 'wp_head','inc_manifest_link' );
// Creates the link tag
function inc_manifest_link() {
echo '<link rel="manifest" href="'.get_template_directory_uri().'/manifest.json">';
}
manifest.json 应该是这样。
但是我希望在清单文件中使用一些 theme_mod 函数。没有 PHP,我就做不到。
无论如何都可以在 manifest.json 文件中编写 PHP 代码。
提前致谢。
解决方法
所以你可以这样做:
<?php
$filename = "manifest.json";
// Grab contents and decode them into an array
$data = json_decode(file_get_contents($filename),true);
//this is where you're adding your content so below you can write to the file
//Creating new var
//(make sure no var has the name "['newnewname']" unless you want to edit it)
$data['newname'] = 'Adding NEW contents';
//Creating new 2 dimensional array
$data['newname'] = array("another1" => "val1","another2" => "val2");
//adding content to an existing 2 dimensional array
$data['exists'] += array("another1" => "val1","another2" => "val2");
//adding content to an existing 3 dimensional array
$data['exists']['name'] += array("another1" => "val1","another2" => "val2");
//adding content to an existing 4 dimensional array
$data['exists']['name']['name'] += array("another1" => "val1","another2" => "val2");
// encode back into json
$jencode = json_encode($data);
// write over file with new contents
$fopen = fopen($filename,"w");
if(fwrite($fopen,$jencode)){
echo('Success!<hr>');
echo(file_get_contents($filename));
}
fclose($fopen);
这是一个活生生的例子(删除了 fopen/write/read 的东西):
高级:http://sandbox.onlinephpfunctions.com/code/eacdd6b8254c2127521769461d5f6d9daeda224d
简单:http://sandbox.onlinephpfunctions.com/code/3589151881aac2e442738b381c05a37240081901