简体   繁体   中英

How to integrate manifest.json into wordpress theme

I have searched the internet, but the results I have seen are not clear enough.

I was able to add a manifest.json file into my wordpress theme by adding this code to the functions.php file.

//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">';
}

The manifest.json is as it should be.

However I wish to use some theme_mod functions within the manifest file. Which I can't do without php.

So is there anyway to write php code within the manifest.json file.

Thanks in advance.

So you can do something like this:

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

Heres a live example (with fopen/write/read stuff removed):

Advanced: http://sandbox.onlinephpfunctions.com/code/eacdd6b8254c2127521769461d5f6d9daeda224d

Simple: http://sandbox.onlinephpfunctions.com/code/3589151881aac2e442738b381c05a37240081901

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM