如何将 manifest.json 集成到 wordpress 主题中
How to integrate manifest.json into wordpress theme
我在网上搜索过,但我看到的结果不够清楚。
通过将此代码添加到 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
我在网上搜索过,但我看到的结果不够清楚。
通过将此代码添加到 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