|
/ Documentation / Regenerating Assets with Code

Regenerating Assets with Code

The Spectra plugin allows users to update assets using the `__uagb_asset_version` option. By leveraging this option, you can ensure that your assets are refreshed each time a specific event occurs, such as saving a post or page.

This can be useful for ensuring that the most recent versions of your assets are loaded, helping to prevent caching issues and keeping your website up to date.

In this document, we will guide you through the process of:

  • Understanding the purpose of the __uagb_asset_version option
  • How to implement it on your website
  • Practical examples of its usage
  • Customizing the asset update process for your needs

What Is the Purpose of the __uagb_asset_version Option?

The __uagb_asset_version option allows your website to refresh asset versions automatically. Each time this option is updated, a new version number is appended to your assets (like stylesheets or JavaScript files), prompting the browser to load the latest version.

This helps avoid caching issues where old versions of files are used even after updates have been made.

You can configure this option to refresh on specific events, such as when a post is saved, or you can schedule updates regularly using a cron job.

How to Implement the __uagb_asset_version Option?

To start using this feature, you need to add a simple piece of code to your theme’s functions.php file or your custom plugin.

Here’s the code snippet:

update_option( '__uagb_asset_version', time() );

This code updates the __uagb_asset_version option to the current timestamp. Each time it runs, it generates a new, unique version number, ensuring that your assets are always up to date.

How Can You Use This in Real Scenarios?

Let’s look at a common use case: updating your assets whenever a post or page is saved.

You can achieve this by hooking into the save_post action. Here’s the code you’ll need:

add_action( 'save_post', 'regenerate_page_assets', 10 );

function regenerate_page_assets(){
    update_option( '__uagb_asset_version', time() );
}

In this example:

  • The regenerate_page_assets function is triggered each time you save a post or page.
  • It updates the __uagb_asset_version with the current timestamp.
  • As a result, your assets will be reloaded the next time the page is accessed, ensuring they reflect any recent changes.

Customizing Asset Updates

You can customize the conditions under which the `__uagb_asset_version` option is updated by using different WordPress hooks or even scheduling regular updates with a cron job.

We hope this guide has helped you understand how to manage your asset versions within the Spectra plugin effectively. By implementing the __uagb_asset_version option, you can keep your website fresh, responsive, and free from caching issues.

If you have any questions or feedback, feel free to leave a comment! We’re always here to help.

Was this doc helpful?
What went wrong?

We don't respond to the article feedback, we use it to improve our support content.

On this page

Get Notified When Available

Note - You can purchase the Essential Toolkit now and easily upgrade to the Business Toolkit once it becomes available.

Download is Just A Click Away!

Enter your email address and be the first to learn about updates and new features.

Get your hands on Spectra Pro

Enter your name and email address to get access to Spectra Pro.