WordPress.org

Ready to get started?Download WordPress

Forums

Custom Plugin - Getting Code to run on WP_ADMIN Login (5 posts)

  1. fatalx
    Member
    Posted 4 years ago #

    Hey,

    I am just starting to write plugins for wordpress and I'm trying to write one that will execute a piece of code whenever someone logins to the admin dashboard.

    Specifically I am trying to run a script that will generate a cookie so I can exclude all site admins from showing up on Google Analytics.

    Google says that I have to add a piece of code inside the <body> tag how can I do that with a plugin? what hooks would I need?

    Thanks

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    See the action reference.
    http://codex.wordpress.org/Plugin_API/Action_Reference

    I'd guess you'd need to hook onto wp_login ...

  3. fatalx
    Member
    Posted 4 years ago #

    ok cool I'll give it a shot

  4. fatalx
    Member
    Posted 4 years ago #

    so im trying to create a cookie and im having some trouble

    here is my code so far

    function set_cookie()
    {
    setcookie("RandomName", "RandomValue", time()+2592000,"/","localhost");
    }

    add_action('admin_menu','set_cookie');

    all I want is for the cookie to be created or update when an admin logs in (or the dashboard load)via wp_admin

    what am I doing wrong here?

  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    Try adding an action onto wp_validate_auth_cookie, which should be when a user has logged in under valid credentials. Grab the current user ID at this point to determine role, then go ahead and do whatever you want to from there.

    See wp-includes/pluggable.php, from lines 506 - 548, and you'll see there's quite a few hooks there .. the above suggested hook is on line 545.

Topic Closed

This topic has been closed to new replies.

About this Topic