尊敬的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);
}
});
}