Skip to content

Instantly share code, notes, and snippets.

@AustinGil
Last active October 7, 2024 02:45
Show Gist options
  • Save AustinGil/8504035f7ff9417284a26af7e3c3db74 to your computer and use it in GitHub Desktop.
Save AustinGil/8504035f7ff9417284a26af7e3c3db74 to your computer and use it in GitHub Desktop.
Only load scripts and styles if a specific shortcode is present in the content when the page/post is saved
/*
* This first function maps shortcodes to their respective resources,
* then checks whether those shortcodes exist in the content,
* then stores the required resource handles in a custom field.
* This way, we aren't running resource-intensive functions on every page load.
*/
function check_post_shortcodes($post_id) {
// Map conditional shortcodes to their respective resource handle
// Remember to manually dequeue resource handles below as well.
$shortcodes_mapper = array(
'vc_row' => 'js_composer_front',
);
// If this is a revision, get real post ID
if ( $parent_id = wp_is_post_revision( $post_id ) )
$post_id = $parent_id;
// Get the post content
$post = get_post($post_id);
$content = $post->post_content;
// Create an empty array for the required resource handles to be stored in post meta
$required_resources = array();
// For each shortcode handle in the array, search the content to see if it's present.
// If it exists in the content, add it's resource handle to the list of required resources.
foreach ( $shortcodes_mapper as $shortcode_tag => $resource_handle ) {
if ( strstr($content, '[' . $shortcode_tag ) ) {
array_push($required_resources, $resource_handle);
}
}
// Only continue if we have any resource handles
if ( $required_resources ) {
// Encode the array to save in post meta
$required_resources = json_encode($required_resources);
// Unhook this function so it doesn't loop infinitely
remove_action( 'save_post', 'check_post_shortcodes' );
// If our custom field already exists, just update it
if ( get_post_meta( $post_id, 'conditional_shortcode', true) ) {
update_post_meta( $post_id, 'conditional_shortcode', $required_resources );
}
// Otherwise, create it
else {
add_post_meta( $post_id, 'conditional_shortcode', $required_resources );
}
// NOTE: You may want to change the name of the custom field. Prefixing with an underscore will also hide it from the back end.
// Re-hook this function
add_action( 'save_post', 'check_post_shortcodes' );
}
// Finally, if our shortcode is not present, but our custom field does, delete the custom field
else {
if ( get_post_meta( $post_id, 'conditional_shortcode', true) ) {
delete_post_meta( $post_id, 'conditional_shortcode' );
}
}
}
// This function only fires when the post is saved. Not on page load.
add_action( 'save_post', 'check_post_shortcodes' );
/*
* This second function dequeues your conditional resources,
* then re-enqueues the ones present in the custom field.
*/
function conditionally_load_shortcode_resources() {
// Remember to manually dequeue your conditional resource handles in case they were already enqueued
wp_dequeue_style( 'js_composer_front' );
// Set up our variables
global $post;
$post_id = $post->ID;
$resource_handles = get_post_meta( $post_id, 'conditional_shortcode', true);
// If our array of handles exists, enqueue each one.
if ( $resource_handles ) {
foreach ( json_decode($resource_handles) as $handle ) {
wp_enqueue_style( $handle );
}
}
}
add_action( 'wp_enqueue_scripts', 'conditionally_load_shortcode_resources', 100 );
@aelsharawi
Copy link

when pasted into this plugin:

My Custom Functions
by Arthur Gareginyan

@AustinGil
Copy link
Author

Oh hey @aelsharawi, I don't know why I never got an email from your comments.

Anyway, this Gist was mostly for demonstration purposes. You need to configure it a bit to make it work. There is an array on line 11 that needs takes a shortcode ('vc_row') and associates it to a resource handle ('js_composer_front'). There is also a manual resource dequeue on line 81 that would need to be edited.

Sorry it didn't work for you right out of the box. It is more of a proof of concept.

If you need a built out plugin for that, I would recommend Plugin Organizer or WP Asset CleanUp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment