How to include json manifest file in wordpress theme?

Member

by domenico , in category: PHP CMS , 6 months ago

How to include json manifest file in wordpress theme?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by filiberto , 6 months ago

@domenico 

To include a JSON manifest file in a WordPress theme, you can follow these steps:

  1. Create a new folder in your theme directory and name it "assets" or any name you prefer. This folder will hold all your static assets.
  2. Inside the "assets" folder, create a new file called "manifest.json". This file will contain the information about your theme's assets.
  3. Open the manifest.json file and define your assets using JSON format. For example:
1
2
3
4
5
{
  "main.css": "path/to/main.css",
  "script.js": "path/to/script.js",
  "image.jpg": "path/to/image.jpg"
}


  1. In your theme's functions.php file, enqueue the manifest.json file using the wp_enqueue_script function. Add the following code to the functions.php file:
1
2
3
4
function enqueue_manifest() {
  wp_enqueue_script( 'theme-manifest', get_stylesheet_directory_uri() . '/assets/manifest.json' , array(), null, true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_manifest' );


  1. Now, you can use the registered JavaScript or CSS files by accessing them through the theme-manifest object. For example, if you want to include the main.css file, use the following code in your template files:
1
<link rel="stylesheet" href="<?php echo theme_manifest['main.css'] ?>">


Make sure to replace theme_manifest with the actual variable name you use in your theme.


By following these steps, you can include a JSON manifest file in your WordPress theme and easily access and update your assets throughout the theme.