How to pass project number to manifest.json

372 Views Asked by At

I am building a web push WordPress plugin and I want to pass project number from form input field to manifest.json file which is included in index.php as

<link rel="manifest" href="/manifest.json">
2

There are 2 best solutions below

0
Marco Castelluccio On BEST ANSWER

Disclaimer: I'm the author of this plugin. Instead of building your own from scratch, you could contribute to the already existing https://github.com/mozilla/wp-web-push.

If you want to build your own, you can check the source of that plugin out to see how we have implemented it.

We've built a class to handle it: https://github.com/marco-c/wp-web-app-manifest-generator.

3
collimarco On

You cannot pass any param to the manifest.json. You must genarate it as a static file when the form is submitted.

Here's the code that we have used for the Pushpad plugin:

if (file_exists ( ABSPATH . 'manifest.json' )) {
  $oldManifestJson = file_get_contents ( ABSPATH . 'manifest.json' );
} else {
  $oldManifestJson = '{}';
}
$data = json_decode ( $oldManifestJson, true );

$data ['gcm_sender_id'] = $settings ['gcm_sender_id'];
$data ['gcm_user_visible_only'] = true;
$newManifestJson = json_encode ( $data );
if ( is_writable ( ABSPATH . 'manifest.json' ) || !file_exists ( ABSPATH . 'manifest.json' ) && is_writable ( ABSPATH ) ) {
  file_put_contents ( ABSPATH . 'manifest.json', $newManifestJson );
} else {
  // display an error
}