Support » Plugins » Using current_user_can breaks plugin

  • Resolved mrmist



    I’m trying to use
    if (current_user_can('manage_options'))
    in my plugin

    But when I do I get this error ..

    Fatal error: Call to undefined function wp_get_current_user() in …./blog/wp-includes/capabilities.php on line 447

    Any ideas what I have to do to make current_user_can work in my plugin?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    You cannot use wp_get_current_user until pluggable.php has loaded, which means you need to defer that usage until the plugins_loaded action, at least.

    Ok. I see what you mean but am not sure how to do it?

    Basically I have a line in the plugin that adds a link on the dashboard, but I only want the link to appear if the user is an admin.

    add_action(‘activity_box_end’, ‘dm_bblr_activitybox’);

    is what I’m doing.

    Does what you say mean that I can’t do any checks around the user permissions in my plugin?

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    No, you can do user checks, but you can’t do them too early.

    The question is when exactly are you doing this check? When the plugin just runs? Or in the dm_bblr_activitybox function?

    Ahh the light begins to dawn. I was attempting to do it when the plugin runs. I’ll see if I can move the check. 🙂

    Thanks Otto 🙂

    For anyone confused by his instructions (I was for a start). You need to move the check to the point in your plugin/theme which does the check. Ie: If you checking before loading something into wp_foot, you need to add the check around the chunk of code which actually modfies wp_foot. Otherwise pluggable.php has not loaded yet.

    Actually, I don’t think my instructions are much better. But hopefully someone will be able to decipher it :p

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using current_user_can breaks plugin’ is closed to new replies.