admin管理员组文章数量:1391995
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 | Show 3 more comments1 Answer
Reset to default 0I 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;
}
}
}
本文标签: pluginsConditionally load public and admin code with AJAX working on both sides
版权声明:本文标题:plugins - Conditionally load public and admin code with AJAX working on both sides 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744698386a2620419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
if ( ! is_admin() || wp_doing_ajax() )
? – Jacob Peattie Commented Mar 3, 2020 at 7:11if ( 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