Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
davidohlin opened this issue Mar 7, 2018 · 7 comments
Open

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

davidohlin opened this issue Mar 7, 2018 · 7 comments

Comments

@davidohlin
Copy link

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
Copy link
Member

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
Copy link
Author

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

Thanks for the quick reply!

@sebastianthulin
Copy link
Member

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
Copy link
Member

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
Copy link
Author

davidohlin commented Mar 7, 2018

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
Copy link
Member

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

@davidohlin
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants