【问题标题】:How to integrate manifest.json into wordpress theme如何将 manifest.json 集成到 wordpress 主题中
【发布时间】:2021-02-06 04:10:18
【问题描述】:

我在网上搜索过,但我看到的结果不够清楚。

通过将此代码添加到 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 代码。

提前致谢。

【问题讨论】:

  • 怎么样把它放到 manifest.json 中作为 php 执行?或者在 manifest.json 中添加另一个变量
  • 将 wp 变量添加到 manifest.json
  • 查看答案,看看是否能帮助你萌芽@ZeddyEmy

标签: php wordpress wordpress-theming progressive-web-apps manifest.json


【解决方案1】:

所以你可以这样做:

<?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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多