简体   繁体   English

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

[英]How to pass PHP values to js script in Wordpress using AJAX

I have tried both wp_localize_script and wp_add_inline_script: it doesn't work the way I expect.我已经尝试过 wp_localize_script 和 wp_add_inline_script:它没有按我期望的方式工作。

I have this in my plugin's PHP file (it is enqueued with wp_enqueue_scripts somewhere else but I don't need to write the code here right?):我在插件的 PHP 文件中有这个(它在其他地方与 wp_enqueue_scripts 一起排队,但我不需要在这里编写代码对吗?):

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

        wp_enqueue_script(
            'custom-profile-script'
        );

        wp_localize_script(
            'custom-profile-plugin',
            'wp_ajax',
            array( 
                'ajax_url' => admin_url( 'admin-ajax.php' ), 
                'ajaxnonce' => wp_create_nonce( 'ajax_post_validation' ),
                'plugin_url' => "url"
            )
        );
        wp_add_inline_script( 'custom-profile-plugin', 'const PHPVAR = ' . json_encode( array(
                'plugin_url' => "url"
        ) ), 'before' );

And then, in my js file:然后,在我的 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);

});

The first console log (related to wp_localize_script) is an object that contains "ajax_url" and "ajax_nonce" with the right values, but "plugin_url" doesn't appear and is UNDEFINED when I call it using wp_ajax.plugin_url.第一个控制台日志(与 wp_localize_script 相关)是一个 object,其中包含具有正确值的“ajax_url”和“ajax_nonce”,但“plugin_url”没有出现,并且在我使用 wp_ajax.plugin_url 调用它时未定义。

The second console log (related to wp_add_inline_script) tells me that PHPVAR is not defined...第二个控制台日志(与 wp_add_inline_script 相关)告诉我 PHPVAR 未定义...

What I don't understand is why the first two values are passed correctly using localize script, but not the third one and, furthermore, why wp_add_inline_script doesn't work.我不明白为什么前两个值使用本地化脚本正确传递,而不是第三个值,此外,为什么 wp_add_inline_script 不起作用。

You need to add wp_enqueue_script after wp_localize_script and also you add wrong handle to wp_localize_script check the below code.您需要在wp_enqueue_script之后添加wp_localize_script并且还向wp_localize_script添加错误的句柄检查下面的代码。

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);
});

The script needs to be registered in the footer for the PHP value to be passed, like this:该脚本需要在页脚中注册,以便传递 PHP 值,如下所示:

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

Instead of:代替:

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
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM