WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using current_user_can breaks plugin (6 posts)

  1. mrmist
    Forum Janitor
    Posted 5 years ago #

    Hello

    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?

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    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.

  3. mrmist
    Forum Janitor
    Posted 5 years ago #

    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?

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    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?

  5. mrmist
    Forum Janitor
    Posted 5 years ago #

    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. :)

  6. Ryan Hellyer
    Member
    Posted 4 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic