当您查看页面、帖子、管理页面等时,会自动解释活动插件PHP文件。
如果我知道完全限定的URL,我也可以直接运行一个插件PHP,但是它很可能会抛出错误,因为没有包含Wordpress函数。
如何测试插件PHP页面是否已通过Wordpress执行?是否有可以安全依赖的全局变量?
当您查看页面、帖子、管理页面等时,会自动解释活动插件PHP文件。
如果我知道完全限定的URL,我也可以直接运行一个插件PHP,但是它很可能会抛出错误,因为没有包含Wordpress函数。
如何测试插件PHP页面是否已通过Wordpress执行?是否有可以安全依赖的全局变量?
您不需要将用户、表单或AJAX请求直接发送到插件文件URL。
If you\'re submitting a form, 根本不需要新的URL,只需:
if ( !empty( $_POST[\'formfield\'] ) {
// handle form submission and print thank you
} else {
// display form
}
或者像大多数人一样,使用重力表单或contact7之类的插件If you\'re doing an AJAX call, 使用提供的API进行AJAX调用
PHP:
function example_ajax_request() {
if ( isset($_REQUEST) ) {
$fruit = $_REQUEST[\'fruit\'];
if ( $fruit == \'Banana\' ) {
$fruit = \'Apple\';
}
echo $fruit;
}
// Always die in functions echoing ajax content
die();
}
add_action( \'wp_ajax_example_ajax_request\', \'example_ajax_request\' );
add_action( \'wp_ajax_nopriv_example_ajax_request\', \'example_ajax_request\' );
JavascriptjQuery(document).ready(function($) {
// We\'ll pass this variable to the PHP function example_ajax_request
var fruit = \'Banana\';
// This does the ajax request
$.ajax({
url: ajaxurl,
data: {
\'action\':\'example_ajax_request\',
\'fruit\' : fruit
},
success:function(data) {
// This outputs the result of the ajax request
console.log(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
});
WP AJAX注释Smashing Magazine How to use AJAX in WordPressE、 g。
if ( !defined( \'WPINC\' ) ) {
die();
}
更快的AJAX调用在超高流量站点上执行操作时,标准的AJAX调用程序有时会表现不佳,在这种情况下,我遵从RARST的回答:但即使在这种情况下,我也不会让一个文件同时处理AJAX和非AJAX,这很难区分关注点。如果您的AJAX处理程序没有使用WP AJAX API,那么它应该是一个单独的专用文件。有很多方法,您可以查看WordPress常量或WordPress函数。。。
示例:
if ( defined( \'WPINC\' ) ) {
// the constant WPINC is defined, so WordPress is loaded
// you should not define a cosntant with this name... and if you absolutely need
// you can use another WordPress specific constant, examples here:
// http://phpxref.ftwr.co.uk/wordpress/nav.html?_constants/index.html
}
检查是否触发了函数+挂钩if ( function_exists(\'did_action\') && did_action( \'muplugins_loaded\' ) ) {
// this check if did_action function exists and the muplugins_loaded is fired
// if this is tru you can be pretty sure WordPress is loaded.
// muplugins_loaded is the first hook fired by WP (before any regular plugin is loaded)
// However if you define the constant SHORTINIT and set to a non-empty value
// this will never be true
}
我正在打电话load_plugin_textdomain 然而,一旦加载了插件,就不会发生这种情况。我确实激活了一个插件,所以这不应该触发吗?add_action(\"plugins_loaded\", \"test_override\"); function init_localization() { echo \"init_localization<br>\"; load_plugin_textdomain (&#x