Don’t beat your head against a wall like I did when trying to load a basic script using admin_enqueue_scripts
in my latest plugin that integrates some custom fields with a Subscriber in FluentCRM. Instead, read this article and learn something new.
Sometimes you just have to dig into the codebase of the thing you’re extending because the developers are working on more important things like new features, not documentation. I’m fine with that, actually. I like poking around in other people’s code. I typically learn things. I maybe feel confident about the things I write. Sometimes I gain more confidence with a plugin maker.
Well, for good reason, I presume, FluentCRM decided to add a step in the script loading on an admin page. It runs it’s own method, unloadOtherScripts()
. This method appears to take the headaches out of most users’ use of FluentCRM by limiting other plugin conflicts on the page.
Since FluentCRM uses a single-page app (I think with Vue), it needs to make sure nothing else is getting in the way. So, it unloads scripts that it doesn’t need.
But, it also provides a filter to let other developers add things to the page without too much hassle. It does this via the fluent_crm_asset_listed_slugs
hook.
Here’s the code right out of app/Hooks/Handlers/AdminMenu.php
:
$approvedSlugs = apply_filters('fluent_crm_asset_listed_slugs', [
'\/gutenberg\/'
]);
$approvedSlugs[] = 'fluent-crm';
As you can see, it allows anything in the “gutenberg” plugin, as well as all things in the “fluent-crm” plugin.
If you need to allow scripts to load from your own plugin, simply add something like:
add_filter( 'fluent_crm_asset_listed_slugs', function( $slugs ) {
$slugs[] = 'my-plugin-slug';
return $slugs;
} );
It’s that simple.
I hope this helps somebody else out there!
And, if you need help developing a plugin, or want some cool integration with FluentCRM or GravityForms, or Breakdance, feel free to reach out!
Frequently Asked Questions
Why does FluentCRM unload other scripts?
FluentCRM is a single-page application (SPA) built with Vue, which means it handles everything on one page. To ensure that its functionality isn’t disrupted by other scripts, it selectively unloads those that are not necessary, minimizing conflicts and optimizing performance.
How can I allow my plugin’s scripts to load in FluentCRM?
FluentCRM offers a filter fluent_crm_asset_listed_slugs
that allows developers to add their plugin’s scripts to an approved list. To include your plugin, simply add the following code snippet to your plugin:
php
Copy
Editadd_filter( 'fluent_crm_asset_listed_slugs', function( $slugs ) { $slugs[] = 'my-plugin-slug'; return $slugs; } );
What does the fluent_crm_asset_listed_slugs
hook do?
The fluent_crm_asset_listed_slugs
The hook is used to filter which scripts are allowed to be loaded on FluentCRM’s admin pages. By default, it includes scripts from the gutenberg
plugin and fluent-crm
. You can add your plugin’s slug to this list to ensure your scripts are loaded alongside FluentCRM’s assets.
What should I do if my plugin’s script is still not loading?
Double-check that you’re using the correct plugin slug and that it matches the one you’ve registered in the filter. You can also use wp_debug()
and inspect the page’s source to ensure the script is being included.
Why is it important to modify the script loading behavior in FluentCRM?
FluentCRM prioritizes its own functionality and attempts to prevent script conflicts, which is great for general users. However, when you’re integrating other plugins or adding custom fields, it’s essential to ensure your scripts are allowed to run to maintain compatibility and functionality.
Is there a performance impact when allowing other plugins’ scripts to load?
Generally, allowing additional scripts can slightly impact performance. However, FluentCRM’s method of script unloading ensures that only essential scripts are running. Just ensure your plugin’s scripts are optimized to avoid unnecessary overhead.