awesome9 / json
WordPress json manager.
Requires
- php: >=5.6
This package is auto-updated.
Last update: 2024-12-09 14:05:30 UTC
README
📃 About JSON
This package provides ease of managing data localization within WordPress. It enables developers to add, remove, and manipulate JSON objects that can be output in the footer of WordPress pages.
💾 Installation
composer require awesome9/json
🕹 Usage
Initialize the JSON Manager
To start using the JSON manager, initialize the JSON
class with a default object name. This object name will be used as the JavaScript variable where the JSON data will be accessible.
use Awesome9\JSON\JSON; // Initialize JSON manager with a default object name $json_manager = new JSON('awesome9'); $json_manager->hooks();
The hooks()
method binds the necessary WordPress hooks to output the JSON data automatically in the footer.
Adding Data
Use the add()
method to add key-value pairs to the JSON object. You can add data in two ways:
- As individual key-value pairs.
- As an array of key-value pairs.
// Adding individual data $json_manager->add('company', 'awesome'); $json_manager->add('product', 'json_manager'); // Adding multiple key-value pairs using an array $json_manager->add([ 'feature1' => 'easy to use', 'feature2' => 'robust', ]);
Removing Data
To remove a specific key from the JSON object, use the remove()
method:
$json_manager->remove('product'); // Removes the 'product' key
Clearing All Data
If you need to remove all data stored in the JSON object, use clear_all()
:
$json_manager->clear_all();
Accessing Data in JavaScript
After setting up data with the JSON manager, you can access it on the frontend in JavaScript. The default object name (in this case, awesome9
) will hold the data.
console.log(awesome9.company); // Outputs: 'awesome' console.log(awesome9.feature1); // Outputs: 'easy to use'