WP wp_ajax_function未调用PHP函数

时间:2019-06-17 10:15:37

标签: javascript php ajax wordpress

我正在构建一个cookie通知的wordpress插件,我希望使用PHP创建cookie。通知栏中的按钮应该运行php函数,而ajax现在还不能真正与我配合使用。

据我所知,我的ajax请求正在处理中,在网络标签中,它显示状态为200的admin-ajax.php,但它不会调用实际功能。

这是我的代码

主插件文件

function setAjaxCallbacks(){

add_action('wp_ajax_accepted', 'accepted');
add_action('wp_ajax_nopriv_accepted', 'accepted');

}

add_action('init', 'setAjaxCallbacks');


function accepted(){

    die('test');
    echo 'LOLALLES';
    global $wpdb;
    $whatever = intval( $_POST['whatever'] );
    $whatever += 10;
    echo $whatever;
    wp_die('0 ', 400);

    echo '<script>
        alert("jhwgvbht ij")
    </script>';
    wp_die();
}

isset($_COOKIE['cookieBar']) or setCookieBar();

function setCookieBar()
{
    if (!is_admin()){

        add_action('wp_enqueue_scripts', 'enqueue_script_custom');

        function enqueue_script_custom()
        {
            wp_enqueue_style('CookieBarStyle', plugin_dir_url(__FILE__) . 'css/styles.css');
            wp_enqueue_script('ajax-script', plugins_url('/js/my_query.js', __FILE__), array('jquery'));
            wp_localize_script('ajax-script', 'ajax_object',
                array('ajaxurl2' => 'https://wordpressjip.jmulder.dt2/wp-admin/admin-ajax.php', 'we_value' => 1234));

            wp_print_scripts('ajax-script');
        }
        ?>

        <div class="cookieBar" id="cookieBar" style="display: block">
            <p align="center"></p>

            <button id="submitCookies" class="submitCookies" name="accepted">Accepteer</button>
        </div>
    <?php
    }
}

my_query.js

jQuery(document).ready(function ($) {
jQuery(".submitCookies").click(function (event) {
    event.preventDefault();

    jQuery.ajax({
        url: ajax_object.ajaxurl2,
        type: 'POST',
        data: {
            action: 'accepted',
            whatever: 1234
        },
        succes: function (response) {
            return response;
        }
    })
})
});

1 个答案:

答案 0 :(得分:0)

名为“ accepted”的函数在另一个函数内部,这就是为什么它不起作用的原因。您需要了解ajax在wordpress中的工作方式。请通过以下链接。它将教您如何在wordpress https://medium.com/techcompose/how-to-use-ajax-precisely-in-wordpress-custom-themes-dc61616720a8

中使用ajax

以下代码可能会帮助您:

<?php

function setAjaxCallbacks()
{
    add_action('wp_ajax_accepted', 'accepted');
    add_action('wp_ajax_nopriv_accepted', 'accepted');
}

add_action('init', 'setAjaxCallbacks');


function accepted()
{
    echo 'LOLALLES';
    global $wpdb;
    $whatever = intval($_POST['whatever']);
    $whatever += 10;
    echo $whatever;
    exit;
}

add_action('wp_enqueue_scripts', 'enqueue_script_custom');

function enqueue_script_custom()
{
    wp_enqueue_style('CookieBarStyle', plugin_dir_url(__FILE__) . 'css/styles.css');
    wp_enqueue_script('ajax-script', plugins_url('/js/my_query.js', __FILE__), array('jquery'));
    wp_localize_script(
        'ajax-script',
        'ajax_object',
        array('ajaxurl2' => 'https://wordpressjip.jmulder.dt2/wp-admin/admin-ajax.php', 'we_value' => 1234)
    );

    wp_print_scripts('ajax-script');
}


function setCookieBar()
{
    if (!is_admin()) {
        if (! isset($_COOKIE['cookieBar'])) {
            ?>

        <div class="cookieBar" id="cookieBar" style="display: block">
            <p align="center"></p>

            <button id="submitCookies" class="submitCookies" name="accepted">Accepteer</button>
        </div>
    <?php
        }
    }
}

add_action( 'wp_footer', 'setCookieBar' );