plugin development – Why won’t this conditional script enqueue?

Question

I’m trying to get my admin scripts to enqueue if a user can moderate comments, but for some reason, this conditional is not working. I can place a var_dump('hello!') inside the “if” statement, and it displays…but wp_enqueue_{fileType} doesn’t work. Any ideas?

Defining admin hooks:

I define the Admin hooks here, and I have a similar function that defines public hooks and uses wp_enqueue_scripts instead of admin.

private function define_admin_hooks() {

        $plugin_admin = new Featured_Reviews_Admin($this->get_plugin_name(), $this->get_version());

        $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles');
        $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts');
    }

Admin registering:

I tried registering the style/script first.

public function enqueue_styles() {
        wp_register_style('featured-reviews-admin-style', plugin_dir_url(__FILE__) . 'css/featured-reviews-admin.css', array(), $this->version, 'all');
        wp_enqueue_style('featured-reviews-admin-style');
    }

Conditional loading:

Here I want to load my public styles / scripts, and if a user can moderate comments, I want to load my admin script, but this conditional doesn’t seem to work.

    public function enqueue_styles() {
        wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/featured-reviews-public.css', array(), $this->version, 'all');

        if (current_user_can('moderate_comments')) {
            var_dump('HI THERE!'); // This runs
            wp_enqueue_style('featured-reviews-admin-style'); // This does not show up.
        }
    }

Any ideas on where this is going wrong?

0
sackadelic 2 months 2022-05-07T11:14:21-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse