Skip to Content
Menu

manifest_json()

Dynamically generate a manifest.json file.

PHP June 29, 2017

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Functions.php on line 329.

    3 Hooks

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    "pre_nebula_manifest_json"
    "nebula_manifest_shortcuts"
    Need a new filter hook? Request one here.

    Actions
    "qm/info"
    Need a new action hook? Request one here.

    PHP
            public function manifest_json(){
                $timer_name = $this->timer('Write Manifest JSON', 'start', 'Manifest');
    
                $override = apply_filters('pre_nebula_manifest_json', null);
                if ( isset($override) ){return;}
    
                $manifest_json = '{
                    "name": "' . get_bloginfo('name') . ': ' . get_bloginfo('description') . '",
                    "short_name": "' . get_bloginfo('name') . '",
                    "description": "' . get_bloginfo('description') . '",
                    "theme_color": "' . get_theme_mod('nebula_primary_color', $this->get_color('$primary_color')) . '",
                    "background_color": "' . get_theme_mod('nebula_background_color', $this->get_color('$background_color')) . '",
                    "gcm_sender_id": "' . $this->get_option('gcm_sender_id') . '",
                    "scope": "/",
                    "start_url": "' . home_url('/') . '?utm_source=pwa",
                    "display": "standalone",
                    "orientation": "portrait",';
    
                $shortcuts = apply_filters('nebula_manifest_shortcuts', array()); //Allow the child theme (or plugins) to add shortcuts to the PWA
                if ( !empty($shortcuts) ){
                    $manifest_json .= '"shortcuts": ' . wp_json_encode($shortcuts, JSON_PRETTY_PRINT) . ',';
                }
    
                $manifest_json .= '"icons": [';
                if ( has_site_icon() ){
                    $manifest_json .= '{
                        "src": "' . get_site_icon_url(16, get_theme_file_uri('/assets/img/meta') . '/favicon-16x16.png') . '",
                        "sizes": "16x16",
                        "type": "image/png"
                    }, {
                        "src": "' . get_site_icon_url(32, get_theme_file_uri('/assets/img/meta') . '/favicon-32x32.png') . '",
                        "sizes": "32x32",
                        "type": "image/png"
                    }, {
                        "src": "' . get_site_icon_url(192, get_theme_file_uri('/assets/img/meta') . '/android-chrome-192x192.png') . '",
                        "sizes": "192x192",
                        "type": "image/png",
                        "purpose": "any maskable"
                    }, {
                        "src": "' . get_site_icon_url(512, get_theme_file_uri('/assets/img/meta') . '/android-chrome-512x512.png') . '",
                        "sizes": "512x512",
                        "type": "image/png",
                        "purpose": "any maskable"
                    }';
                } else {
                    //Loop through all meta images
                    $files = glob(get_theme_file_path('/assets/img/meta') . '/*.png');
                    foreach ( $files as $file ){
                        $filename = $this->url_components('filename', $file);
                        $dimensions = getimagesize($file); //Considering adding an @ to ignore notices when getimagesize fails
                        if ( !empty($dimensions) ){
                            $manifest_json .= '{
                                "src": "' . get_theme_file_uri('/assets/img/meta') . '/' . $filename . '",
                                "sizes": "' . $dimensions[0] . 'x' . $dimensions[1] . '",
                                "type": "image/png",
                                "purpose": "any maskable"
                            }, ';
                        }
                    }
                }
    
                $manifest_json = rtrim($manifest_json, ', ') . ']}';
    
                WP_Filesystem();
                global $wp_filesystem;
                $wp_filesystem->put_contents($this->manifest_json_location(false), $manifest_json);
                do_action('qm/info', 'Updated manifest.json File');
                $this->timer($timer_name, 'end');
            }
    

    Override

    To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

    PHP
    add_filter('pre_nebula_manifest_json', 'my_custom_manifest_json'); 
    function my_custom_manifest_json(){ 
        //Write your own code here
    
        return true; //Return true to prevent the original function from running afterwords
    }

    You can completely disable this PHP function with a single line actions:

    PHP
     add_filter('pre_nebula_manifest_json', '__return_false');