Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用AJAX将PHP值传递给Wordpress中的js脚本_Javascript_Php_Jquery_Ajax_Wordpress - Fatal编程技术网

Javascript 如何使用AJAX将PHP值传递给Wordpress中的js脚本

Javascript 如何使用AJAX将PHP值传递给Wordpress中的js脚本,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我试过wp_本地化_脚本和wp_添加_内联_脚本:它没有按我预期的方式工作 我的插件的PHP文件中有这个(它与其他地方的wp_enqueue_脚本一起排队,但我不需要在这里编写代码): 然后,在我的js文件中: var data = { action: 'custom_user_plugin_update_meta_rating_value', security: wp_ajax.ajaxnonce }; $.post(wp_ajax.ajax_url

我试过wp_本地化_脚本和wp_添加_内联_脚本:它没有按我预期的方式工作

我的插件的PHP文件中有这个(它与其他地方的wp_enqueue_脚本一起排队,但我不需要在这里编写代码):

然后,在我的js文件中:

var data = {
        action: 'custom_user_plugin_update_meta_rating_value', 
        security: wp_ajax.ajaxnonce
    };

$.post(wp_ajax.ajax_url, data, function(result) {

        console.log(wp_ajax);
        console.log(PHPVAR.plugin_url);

});
第一个控制台日志(与wp_localize_脚本相关)是一个对象,其中包含具有正确值的“ajax_url”和“ajax_nonce”,但“plugin_url”不会出现,并且在我使用wp_ajax.plugin_url调用它时未定义

第二个控制台日志(与wp_add_inline_脚本相关)告诉我没有定义PHPVAR


我不明白的是,为什么前两个值是使用本地化脚本正确传递的,而第三个值不是,而且,为什么wp_add_inline_脚本不起作用。

您需要在
wp_localize_脚本
之后添加
wp_enqueue_脚本
,并且您为
wp_localize_脚本
添加了错误的句柄,请检查下面的代码

wp_register_script(
    'custom-profile-script',
    "{$this->plugin_url}js/custom-profile-plugin.js",
    array( 'jquery' ),
    null,
    true
);

wp_localize_script(
    'custom-profile-script',
    'wp_ajax',
    array( 
        'ajax_url' => admin_url( 'admin-ajax.php' ), 
        'ajaxnonce' => wp_create_nonce( 'ajax_post_validation' ),
        'plugin_url' => "url"
    )
);

wp_enqueue_script(
    'custom-profile-script'
);

var data = {
    action: 'custom_user_plugin_update_meta_rating_value', 
    security: wp_ajax.ajaxnonce
};

$.post(wp_ajax.ajax_url, data, function(result) {
    console.log(wp_ajax);
    console.log(wp_ajax.plugin_url);
});

要传递PHP值,需要在页脚中注册脚本,如下所示:

wp_register_script(
    'custom-profile-script',
    "{$this->plugin_url}js/custom-profile-plugin.js",
    array( 'jquery' ),
    null,
    true // register script in the footer
);
而不是:

wp_register_script(
    'custom-profile-script',
    "{$this->plugin_url}js/custom-profile-plugin.js",
    array( 'jquery' ),
    null,
    false // default value of the wp_register_script function that registers script in header
);

谢谢你的回复。我试图在本地化后放入wp_enqueue_脚本,但结果仍然是一样的:wp_ajax.plugin_url未定义…好的,我找到了解决方案,您通过讨论声明顺序明确地让我走上了正轨:需要使用enqueue_脚本函数中的true参数将脚本添加到页脚。我将编辑我的问题以写出答案
wp_register_script(
    'custom-profile-script',
    "{$this->plugin_url}js/custom-profile-plugin.js",
    array( 'jquery' ),
    null,
    false // default value of the wp_register_script function that registers script in header
);