1

Given a wordpress (3.4.x) install that only has 1 admin, how would I grab, from within a plugin, the IP of the currently logged in admin, if there is an admin logged in?

In other words: I need to obtain the IP of the admin, IF there is admin is logged in, and use this IP for other purposes. The request will not necessarily come in from the person logged in as admin. If there is no admin logged in, the function should return empty, as I only need the IP of the admin WHILE he/she is logged in.

2 Answers 2

2

This functionality is not directly available, you'll have to track the logins inside the database(probably as a transient) & check if that transient is available.

function check_for_admin() {
    $user = wp_get_current_user();
    if(in_array('administrator', $user->roles))
        set_transient('admin_ip', get_ip(), 60*10);
}
add_action('init', 'check_for_admin');

After that you can use get_transient('admin_ip') & check if it's available

For retrieving the IP, you may use toscho's code.

EDIT:

The 60*10 in the code means if no admin has been active for last 10 minutes, consider them all offline & cleanup the database.

It's also possible to delete the transient when user manually logs out, you need to hook into the 'clear_auth_cookie' action to clean up the database.

function clear_the_transient() {
    $user = wp_get_current_user();
    if(in_array('administrator', $user->roles))
        delete_transient('admin_ip')
}
add_action('clear_auth_cookie', 'clear_the_transient');

This code will work in your case when there is only 1 administrator. But this code has a major problem, it deletes the IP from the database even if there are 2 administrators online & only one of them logged out. To overcome that situation, you will have to store the user id as well as their corresponding last activity time in the database.

11
  • Maybe I need to clarify that I need to obtain the IP of the admin, IF there is admin is logged in, and use this IP for other purposes. The request will not necessarily come in from the person logged in as admin. If there is no admin logged in, the function should return empty, as I only need the IP of the admin WHILE he/she is logged in. Thanks
    – Gaia
    Nov 22, 2012 at 13:59
  • that's exactly what the if(in_array('administrator', $user->roles)) will do. If user's role is 'administrator', only then the next statement is executed Nov 22, 2012 at 14:02
  • Where is get_transient() defined?
    – Gaia
    Nov 22, 2012 at 14:04
  • get_transient('admin_ip') will return the admin ip if it's there in the database. Use it anywhere you want to retrieve the ip Nov 22, 2012 at 14:09
  • 1
    @Gaia "If come across a situation where I have two or more admins" ... than you add the user id (or the login name or something else from $user) to the transient: set_transient('admin_ip_' . $user->ID, get_ip(), 60*10);. Same with delete_transient()
    – Ralf912
    Nov 22, 2012 at 20:45
1

Like any other IP, they are not different for admins …

function get_ip() 
{
    $ip      = '';
    $sources = array (
        'REMOTE_ADDR',
        'HTTP_X_FORWARDED_FOR',
        'HTTP_CLIENT_IP',
    );

    foreach ( $sources as $source ) {
        if ( isset ( $_SERVER[ $source ] ) )  {
            $ip = $_SERVER[ $source ];
        } elseif ( getenv( $source ) ) {
            $ip = getenv( $source );
        }
    }

    return $ip;
}

Make sure to validate the value.

1
  • I know how to get an IP, but I want the currently logged in ADMIN's IP.
    – Gaia
    Nov 22, 2012 at 10:34

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.