1

I define the wp_enqueue_script in the pugin definition file and I can see my script being fired when I log in as admin, but not as a normal user. Why is this happening? And how can I enable scripts for all logged-in users, and not just admin? I did this on plugin file:

function enqueue_team_management_scripts() {
    wp_enqueue_script("team-scripts", plugin_dir_url(__FILE__) . "js/team-scripts.js");

}

add_action("wp_enqueue_scripts", "enqueue_team_management_scripts");

and this on my template page:

<button id="create_team" onclick="createTeam();" >CREATE TEAM</button>

When I click the button, nothing happens (error displays in firebug)>

1
  • Where are you enqueueing the script - in the admin area or the front end? It could be that your cache is disabled when you are logged in but un-logged-in users see a cached version that isn't reflecting your changes. It may help if you can let us know where you are enqueueing (theme, plugin?) and what code you're using to enqueue.
    – WebElaine
    Commented May 19, 2017 at 16:11

2 Answers 2

4

Additionally, apart from the "where" of your wp_enqueue_script() function (i.e. theme, plugin, etc.), the "when" is also important, in terms of WP's Action Reference hooks, and the order in which they're fired.

There are 2 distinct script-related hooks, wp_enqueue_scripts and admin_enqueue_scripts, which allow for separation of scripts and styles depending on which view the user currently has.

If you're not hooking your wp_enqueue_script() function into one of these properly, this would be one potential cause of the problem.

Providing the code in question will help us find the answer.

EDIT: if you need/want the script to only be enqueued for logged-in, non-admin users, you can wrap it with a two-part conditional evaluator, similar to this:

if( is_user_logged_in() && !current_user_can( 'administrator' ) ) {
    // enqueue stuff goes here
}

That's the most basic method of testing the conditions you've provided, but it's important to note, there's better, more regimented, methods of determining a user's role type and capabilities, than current_user_can() as this simple function only returns the role type, and not specific capabilities that may have been added/removed to/from a role type via a plugin or customization elsewhere in the theme, and those changes may or may not impact your end goal.

1
  • I did this on plugin file: add_action("wp_enqueue_scripts", "enqueue_team_management_scripts"); function enqueue_team_management_scripts() { wp_enqueue_script("team-scripts", plugin_dir_url(FILE) . "/js/team-scripts.js"); } and this on my template page: <button id="create_team" onclick="createTeam();" >CREATE TEAM</button> When I click the button, nothing happens (error displays in firebug)> Commented May 23, 2017 at 13:27
1

To output your scripts for non-logged-in users, use a conditional in conjunction with wp_enqueue_script():

add_action('wp_enqueue_scripts','enqueue_my_script');
function enqueue_my_script(){
    if (!is_logged_in()) {
        wp_enqueue_script('my-script','YOUR SCRIPT URL');
    }
}

This will only enqueue the script for non-logged in users. If you remove the conditional, it will enqueue the script for everyone.

If you already have an script being enqueued only for admins, there is most likely a conditional is_logged_in() in your code. Try finding and removing that line of code.

4
  • I don't have is_admin. Also, I only need this script for logged in users, and not for admins, and not for non-logged-in users. This is my code to include the script: wp_enqueue_script("team-scripts", plugin_dir_url(FILE) . "/js/team-scripts.js"); wp_localize_script("team-scripts", "ajaxurl", admin_url( "admin-ajax.php")); Commented May 22, 2017 at 8:06
  • Your solution didn't work also. I am putting these things in the plugin definition file, just so you know (I will edit the question). Commented May 22, 2017 at 8:14
  • 1
    side note, is_admin is not the right way to test if a user is logged in. for example it will return true for all ajax requests Commented May 22, 2017 at 9:25
  • @VladimirDespotovic I updated the answer to is_logged_in.
    – Johansson
    Commented May 22, 2017 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.