0

I'm trying to conditionally load my front-end and admin area code, so the file and class that creates admin area will on load on admin side and file and class that is needed to be run on front-end will run only on front-end and won't touch anything on admin area.

I tried to use is_admin() conditional:

if (!is_admin()) {
    require_once(plugin_dir_path(dirname(__FILE__)) . 'public/class-public.php');
    $this->Public = new Public();
} else {
    require_once(plugin_dir_path(dirname(__FILE__)) . 'admin/class-admin.php');
    $this->Admin = new Admin();
}

code loading was fine, but AJAX was not working on public side, as AJAX requests bound to either wp_ajax_ or wp_ajax_nopriv_ actions are executed in the WP Admin context. So I decided to create my own isAdmin() function:

public static function isAdmin() {
    $currentUrl = set_url_scheme(
        sprintf(
            'http://%s%s',
            $_SERVER['HTTP_HOST'],
            $_SERVER['REQUEST_URI']
        )
    );
    $adminUrl = strtolower(admin_url());
    $referrer  = strtolower(wp_get_referer());

    if (strpos($currentUrl, $adminUrl) === 0) {
        if (strpos($referrer, $adminUrl) === 0) {
            return true;
        } else {
            if (function_exists('wp_doing_ajax')) {
                return !wp_doing_ajax();
            } else {
                return !(defined('DOING_AJAX') && DOING_AJAX);
            }
        }
    } else {
        if (!defined('REST_REQUEST') || !REST_REQUEST) {
            return false;
        }
        return (isset($_REQUEST['context']) && $_REQUEST['context'] === 'edit');
    }
}

code loading was also fine, but now AJAX was working on public side and not working on admin side.

So, how can I prevent loading public code on admin area code and vice versa with AJAX working on both sides?

8
  • Can't you just check if ( ! is_admin() || wp_doing_ajax() )? Commented Mar 3, 2020 at 7:11
  • @JacobPeattie No, I can't because in that case AJAX won't work on admin side. I need it to work on both sides.
    – DaftPlug
    Commented Mar 3, 2020 at 7:16
  • How about if ( is_admin() || wp_doing_ajax() ) { echo 'admin'; } else { echo 'blog'; } - the echo is just for testing..
    – Sally CJ
    Commented Mar 3, 2020 at 9:29
  • @SallyCJ In this case AJAX is not working on public side.
    – DaftPlug
    Commented Mar 3, 2020 at 9:56
  • You can just register both the AJAX actions for admin and public/front sides. That way, regardless the current request is made on the admin or public side, your AJAX would continue to run as expected. But for the rest of your code, you can use the conditional to run/load the code only on the admin or public side.
    – Sally CJ
    Commented Mar 3, 2020 at 20:50

1 Answer 1

0

I managed to resolve this issue by also checking for public interface. I created new function isPublic() to check if it is public. So here is my final code:

if ($this->isPublic()) {
    require_once(plugin_dir_path(dirname(__FILE__)) . 'public/class-public.php');
    $this->Public = new Public();
} elseif ($this->isAdmin()) {
    require_once(plugin_dir_path(dirname(__FILE__)) . 'admin/class-admin.php');
    $this->Admin = new Admin();
}

and here are helper isPublic() and isAdmin() functions:

public static function isAdmin() {
    if (function_exists('is_admin') && is_admin()) {
        return true;
    } else {
        if (strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false) {
            return true;
        } else {
            return false;
        }
    }
}

public static function isPublic() {
    if (function_exists('is_admin') && is_admin()) {
        if (function_exists('wp_doing_ajax') && wp_doing_ajax()) {
            return true;
        } else {
            return false;
        }
    } else {
        if (strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false) {
            if (strpos($_SERVER['REQUEST_URI'], 'admin-ajax.php') !== false) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
}

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.