Ajax-action.php找不到添加的操作

时间:2019-11-12 作者:Luuk Verhagen

尊敬的Wordpress插件开发人员同事:,

我目前正在开发一个WordPress定制预订插件。在这个插件中,我添加了一个日历短代码,它在页面上嵌入了一个Html月历。我还添加了一些Jquery,当用户单击相应的按钮时,这些Jquery应该会得到上个月或下个月的日历。jquery向管理ajax发送post请求。php,这将触发一个添加的ajax操作。唯一的问题是管理ajax。php返回400,这意味着它找不到操作。我知道会触发Wp\\u Lbs\\u日历的构造函数(这意味着必须添加操作),因为show函数成功地将日历返回到页面。

所以我希望你们能进一步帮助我。

我的代码:

日历类别:

class Wp_Lbs_Calendar {  

/**
 * The ID of this plugin.
 *
 * @since    1.0.0
 * @access   private
 * @var      string    $plugin_name    The ID of this plugin.
 */
private $plugin_name;

/**
 * The version of this plugin.
 *
 * @since    1.0.0
 * @access   private
 * @var      string    $version    The current version of this plugin.
 */
private $version;


/**
 * Constructor
 */
public function __construct( $plugin_name, $version ) {
    $this->plugin_name = $plugin_name;
    $this->version = $version;  

    $this->naviHref = htmlentities($_SERVER[\'PHP_SELF\']);

    add_action(\'wp_ajax_nopriv_get_next_calendar_ajax_action\', array(get_called_class(), \'get_next_calendar_ajax_action\') );
    add_action(\'wp_ajax_get_next_calendar_ajax_action\', array(get_called_class(), \'get_next_calendar_ajax_action\') );

    wp_enqueue_style( $this->plugin_name.\'_calendar\');
    wp_enqueue_script( $this->plugin_name.\'_calendar\');
    wp_localize_script( $this->plugin_name.\'_calendar\', \'my_ajaxurl\', admin_url( \'admin-ajax.php\' ) );
}

private function _createScript(){
    $nextMonth = $this->currentMonth==12?1:intval($this->currentMonth)+1;

    $nextYear = $this->currentMonth==12?intval($this->currentYear)+1:$this->currentYear;

    $preMonth = $this->currentMonth==1?12:intval($this->currentMonth)-1;

    $preYear = $this->currentMonth==1?intval($this->currentYear)-1:$this->currentYear;

    return \'<script>function prev(){getNextCalendar(\\\'\'.$this->version.\'\\\',\'.$preMonth.\',\'.$preYear.\');}
    function next(){getNextCalendar(\\\'\'.$this->version.\'\\\',\'.$nextMonth.\',\'.$nextYear.\');}</script>\';
}

...

public function show(){
    ...
}

public static function get_next_calendar_ajax_action(){
    if(isset($_POST[\'plugin_name\']) && isset($_POST[\'version\'])  && isset($_POST[\'year\']) && isset($_POST[\'month\']) ){           
        $plugin_name = htmlentities($_POST[\'plugin_name\']);
        $version = htmlentities($_POST[\'version\']);
        $year = htmlentities($_POST[\'year\']);
        $month = htmlentities($_POST[\'month\']);

        $Calendar = new Wp_Lbs_Calendar($plugin_name, $version);
        echo $Calendar->ShowByDate($month, $year);
    }
    wp_die();
}
}
我的快捷码类:

class Wp_Lbs_Shortcodes {

    /**
     * The ID of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $plugin_name    The ID of this plugin.
     */
    private $plugin_name;

    /**
     * The version of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $version    The current version of this plugin.
     */
    private $version;

    /**
     * Initialize the class and set its properties.
     *
     * @since    1.0.0
     * @param      string    $plugin_name       The name of the plugin.
     * @param      string    $version    The version of this plugin.
     */
    public function __construct( $plugin_name, $version ) {

        $this->plugin_name = $plugin_name;
        $this->version = $version;

    }

    public function calendar($atts = [], $content = null){
        include_once(\'partials/wp-lbs-calendar.php\');
        $Calendar = new Wp_Lbs_Calendar($this->plugin_name, $this->version);
        return $Calendar->show();
    }
}
样式加载到另一个文件中,如下所示:

public function enqueue_scripts() {

    wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . \'js/wp-lbs-public.js\', array( \'jquery\' ), $this->version, false );
    wp_register_script( $this->plugin_name.\'_calendar\', plugin_dir_url( __FILE__ ) . \'js/wp-lbs-get-calendar.js\', array( \'jquery\' ), $this->version, false );

}
javascript文件:

function getNextCalendar(plugin_version, year, month){
    jQuery.ajax({
        url: my_ajaxurl,
        type: "POST",
        data:{
            action: \'get_next_calendar_ajax_action\',
            plugin_name: \'wp_lbs\',
            version: plugin_version,
            year: year,
            month: month
        },
        success: function(result){
            $(\'#calendar\').parent().html(result);
        }
    });
}

1 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

您不能从短代码中钩住AJAX操作,而Wp_Lbs_Calendar 只在您的短代码内运行。

这意味着操作仅与wp_ajax_ 当您的短代码在页面上使用时。但是AJAX请求是对admin-ajax.php, 并且您的短代码没有在该页面上运行,因此操作没有挂钩,也不会运行。

你的add_action()AJAX的s需要挂接在一个允许它们在每次页面加载时运行的位置,以便处理程序在admin AJAX上可用。php。