1

I am creating plugin that redirects users UNLESS they are visiting from a specific IP.

Originally I had this code sat in index.php so it would never load on wp-admin, wp-login pages. However since I have moved this code to a plugin it is.

Does anyone know how I can stop the plugin from running while user is on wp-login / wp-admin? I was going to simply check the url for these strings but wondered if there was a 'better practice' method of achieving this.

Plugin Code:

/**
 * Plugin Name: LSM - Device Detection
 * Plugin URI: http://no_uri
 * Description: Makes use of WURFL API to detect what device a user is on and redirect if needed
 * Version: 1.0
 * Author: James Husband
 * Author URI: http://no_uri
 * License: GPL2
*/

    // I DO NOT WANT THE FOLLOWING CODE TO RUN IF USER IS ON WP-ADMIN / WP-LOGIN

  /* Device Redirects */
    $device = getDevice();
    if ($_SERVER["REMOTE_ADDR"] != "IPHIDDEN" and $_SERVER["REMOTE_ADDR"] != "IPHIDDEN" and $_SERVER["HTTP_X_FORWARDED_FOR"] != "IPHIDDEN") {
          switch ($device) {
          case TABLET:
              header('Location: http://www.urlhidden.com/tablet/');
              exit;
          break;

          case DESKTOP:
              header('Location: http://www.urlhidden.com/');
              exit;
          break;
      }
    }

2 Answers 2

3

You can try with the following example to stop the code running on admin side or on wp-login.php page.

if ( ! is_admin() || 'wp-login.php' != $GLOBALS['pagenow'] ) {
    // Do stuff here. code inside this statement will 
    // not run on wp-admin & login page.
}
1

Hook your code onto the template_redirect hook which is only fired on the front end of a WP site.

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.