我有一个插件上的代码
function ajaxp_init(){
/*
* registro del script
*/
wp_deregister_script( \'jquery\' );
wp_register_script( \'jquery\', \'http://code.jquery.com/jquery-latest.pack.js\', false, \'\' );
//keep jQuery and Prototype compatible
$url = get_bloginfo(\'wpurl\').\'/wp-content/plugins/pablowishlist\';
wp_register_script( \'jquery_no_conflict\', $url . \'/pablowishlist.js\', array( \'jquery\' ), \'\' );
wp_enqueue_script( \'jquery_no_conflict\' );
global $post;
wp_localize_script(\'jquery_no_conflict\',\'MyAjax\', array(
\'postId\' => $post->ID,
\'action\' => \'ajaxp_add_wishlist\'
));
//wp_register_script( \'pablowishlist-js\', get_template_directory_uri() . \'/js/pablowishlist.js\', array( \'jquery\' ), \'2.2.1\', false );
//wp_register_script(\'pablowishlist-js\', plugins_url(\'pablowishlist.js\',__FILE__), array(\'jquery\')); // funcion de wordpress que registra un código, javascript.
/*
* load script
*/
//wp_enqueue_scripts(\'jquery\');
//wp_enqueue_scripts(\'pablowishlist-js\');
}
和同一目录中的JavaScript文件
jQuery(document).ready(function($){
$(\'#ajaxp_add_wishlist\').click(function(e){
//alert(\'hola\');
$.post(document.location.protocol+\'//\'+document.location.host+\'/wp-admin/admin-ajax.php\', MyAjax, function(response){
alert(response);
});
});
});
但是当我点击按钮时
ajaxp_add_wishlist
什么都没发生。
怎么了?
SO网友:Rizzo
您从未将ajax\\u init排入函数队列。您还可以使用plugins_url() 并用一个短代码拆分为两个函数,如下所示:
(未测试)
<?php
/*
Plugin Name: Ajax Plugin
Description: Plugin for adding ajax
Version: 1.0
*/
/*-------------------------------------------------------*/
/* Register Scripts
/*-------------------------------------------------------*/
add_action( \'wp_enqueue_scripts\', \'ajaxp_init\' );
function ajaxp_init(){
/*
* registro del script
*/
wp_enqueue_script(\'jquery\');
wp_register_script( \'jfile_general_js\', plugins_url(\'pablowishlist.js\', __FILE__));
wp_enqueue_script(\'jfile_general_js\')
}
/*-------------------------------------------------------*/
/* Add shortcode
/*-------------------------------------------------------*/
add_shortcode(\'ajaxplug\',\'jfile_manager\');
function jfile_manager() {
global $post;
wp_localize_script(\'jquery_no_conflict\',\'MyAjax\', array(
\'postId\' => $post->ID,
\'action\' => \'ajaxp_add_wishlist\'
));
}