1

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">
HeroWeb512
  • 15
  • 10

2 Answers2

3

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.

Marco Castelluccio
  • 10,152
  • 2
  • 33
  • 48
0

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
}
collimarco
  • 34,231
  • 36
  • 108
  • 142
  • BTW If you don't want to build from scratch and you are ok to use a service like Pushpad, feel free to fork the project on Github (https://github.com/pushpad/pushpad-wordpress) and add your improvements. I'd also be happy to merge any pull request on the main repo – collimarco Nov 01 '16 at 08:54
  • But I just want to send custom notifications from admin side of the plugin send notification page – HeroWeb512 Nov 01 '16 at 10:42
  • A suggestion, with this code you can't support all possible WordPress configurations (many installations can't write files directly). You might want to use the class that I mentioned in my reply or build your own manifest generator on top of the class https://github.com/marco-c/WP_Serve_File, or draw inspiration from the code in WP_Serve_File. – Marco Castelluccio Nov 01 '16 at 13:11