最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Conditionally load public and admin code with AJAX working on both sides

programmeradmin0浏览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?

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?

Share Improve this question asked Mar 3, 2020 at 7:06 DaftPlugDaftPlug 31 silver badge4 bronze badges 8
  • Can't you just check if ( ! is_admin() || wp_doing_ajax() )? – Jacob Peattie 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
 |  Show 3 more comments

1 Answer 1

Reset to default 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;
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论