Support » Fixing WordPress » Run a code before wordpress loads

  • Hi,

    I’m using WordPress 3.5 w/ SP Genesis Framework. I want to run a code before WordPress loads, it has to be site-wide. So when a visitor visit any page within the root / directory , the code will execute first and then WordPress.

    I used the following code in the index.php file in the root /. It works great but not no sure if thats the right way to do it. Should i put it in wp-blog-header.php or in the WordPress functions.php or in the theme’s header file.

    Here is the code I used in the “index.php”:

    $gi = geoip_open('/home/user/public_html/GeoIP-Data/GeoIP.dat', GEOIP_STANDARD);
    $country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
    $my_countries = array('us','ca');
    if (!in_array(strtolower($country), $my_countries))
    define('WP_USE_THEMES', true);

    The code gets visitor’s ip address from $_SERVER['REMOTE_ADDR'] get ip location from GeoIP.dat and if the visitor is from either US or Canada it executes wordpress otherwise shows an error msg that the site is not accessible from visitors country.

    I mainly want to protect the wp registration/login page from spammers. But since I don’t do business outside US/CA … I don’t mind blocking the rest.

    Please …. I would really appreciate if someone from the WordPress community can help me out.


Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Run a code before wordpress loads’ is closed to new replies.