helsingborg-stad / Modularity

Widget plugin for WordPress based on post-type's
MIT License
8 stars 9 forks source link

Can't enqueue scripts when using a module as a plugin #34

Open davidohlin opened 6 years ago

davidohlin commented 6 years ago

Hi!

I'm creating a plugin for Modularity and want to enqueue a script. I'm using the script() method like this:

public function script()
{
    wp_register_script('mod-accordion-js', plugins_url( '/assets/js/mod-accordion.js', __FILE__ ), array(), '', true);
    wp_enqueue_script('mod-accordion-js');
}

I can only get this working by re-invoking wp_enqueue_scripts() on line 140 of modularity/source/php/Module.php, which I assume is a horrible idea:

...
        if (!is_admin() && $this->hasModule()) {
            add_action('wp_enqueue_scripts', array($this, 'style'));
            add_action('wp_enqueue_scripts', array($this, 'script'));
            do_action('wp_enqueue_scripts');
        }
...

Am I missing something?

Thanks!

sebastianthulin commented 6 years ago

I'm shure thats a horrible idea. ;). I can recall that we have had some issues before with this in some enviroments. The styles are registrered on the module __construct. This may be too early or too late, depending on when you invoke "modularity_register_module" in your plugin. You should hook your registration of the module on the hook "Modularity" (not plugins_loaded).

Do you have some code that produces this error?

davidohlin commented 6 years ago

😄 Yes, here's the plugin repo: https://github.com/RegionHalland/mod-accordion

Thanks for the quick reply!

sebastianthulin commented 6 years ago

Seems to be a core issue. The module construct is called to late. The module call happends in content-context where styles already has been printed to the page. I will have a go on restructuring the calls to provide the functionality.

sebastianthulin commented 6 years ago

In contradiction to above; I can actually get this working. The styles & css is enqueued correctly on the page that the module appears on (in other words not all pages). What version are you running of Modularity?

davidohlin commented 6 years ago

Strange! I'm using version 1.6.5. I've applied the changes from this pull request, but even with a fresh install of Modularity I can't get it to work. What theme were you using when you got this working..? We're using a modified version of Sage.

sebastianthulin commented 6 years ago

Latest release is 2.3.51, do you have the ability to update? We are using latest version of Municipio.

davidohlin commented 6 years ago

Updated to 2.3.51, having the same issue. I will look further into this and try to get back with more ideas on what might cause it. Meanwhile, the theme we're using can be found here.

Thanks!