WordPress.org

Ready to get started?Download WordPress

Forums

Run a code before wordpress loads (2 posts)

  1. OmanusH
    Member
    Posted 1 year ago #

    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":

    <?php
    require_once('/home/user/public_html/GeoIP-Data/geoip.inc');
    
    $gi = geoip_open('/home/user/public_html/GeoIP-Data/GeoIP.dat', GEOIP_STANDARD);
    $country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
    geoip_close($gi);
    
    $my_countries = array('us','ca');
    if (!in_array(strtolower($country), $my_countries))
    {
    include('/home/user/public_html/errmsg.inc');
    exit;
    }
    else
    {
    define('WP_USE_THEMES', true);
    require('./mvwp/wp-blog-header.php');
    }

    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.

    Thanks.

  2. Andrew Bartel
    Member
    Posted 1 year ago #

    Just do it at the top of your header.php.

Topic Closed

This topic has been closed to new replies.

About this Topic