Пересмотр моего ответа, чтобы, надеюсь, лучше объяснить и все же помочь.
Также я не ответил на ваш первый вопрос о том, что show_product_list
не срабатывает при нажатии.
Я не вижу в предоставленном вами коде ничего, что бы указывало на срабатывание при нажатии.
Вы пробовали:
jQuery('a').on('click', function(){
jQuery(this).show_product_list();
});
Возвращаясь к проблеме AJAX, для product_shortcode.js и вашего AJAX, способ передачи "do_shortcode ('[products "integrated-devices-microcontroller"]')"
не будет работать по двум причинам:
- Строка разорвана, потому что двойные кавычки должны быть экранированы.
- Вы не можете передать функцию и такой параметр.
Разбивая его, в следующем фрагменте кода вы отправляете action
и post_id
на admin-ajax.php :
data : {
action: "do_shortcode ('[products "integrated-devices-microcontroller"]')",
post_id: post_id
},
admin-ajax.php пытается выполнить отправленные вами действия.
С admin-ajax.php :
if(! has_action( 'wp_ajax_' . $_REQUEST['action'] )){....
Где $ _ REQUEST ['action'] исходит из фрагмента кода, о котором мы впервые говорили о (action: "do_shortcode ('[products "integ.....)
.
С этим Wordpress ищет такие действия, как:
'wp_ajax_nopriv_do_shortcode ('[products "integrated-devices-microcontroller"]')'
'wp_ajax_do_shortcode ('[products "integrated-devices-microcontroller"]')'
Что, как вы видите, не работает по нескольким причинам.
При определении хуков wp_ajax_my_action
и wp_ajax_nopriv_my_action
, my_action - это действие, которое вы отправляете вместе с вашим запросом AJAX.
В /js/product_shortcode.js:
jQuery(document).ready( function() {
jQuery.fn.show_product_list() {
gciAlert ("in product_shortcode function")
var post_id = jQuery(this).data('id');
jQuery.ajax({
type : "post",
url : ajax-script.ajaxurl,
data : {
action: "show_product_list", // This is the action you pass
post_id : post_id, // This goes along with it
shortcode: "[your shortcode here]" // You can pass the shortcode
},
success: function(response) {
console.log(response); // See the response
if(response.type == "success") {
var subcat_txt = jQuery(e.target).text()
jQuery("#productBox").html("<h3>"+subcat_txt+"</h3")
jQuery("#productBox").html(response)
}
else {
alert("The products could not be listed")
} // end response
} // end success:function
}) // end jQuery.ajax }) //end jQuery
die();
} // end function
}) // end document ready
В ваших функциях. Php:
$script = 'http://pmbus.staging.wpengine.com/wp-content/themes/PMBus/includes/js/product_shortcode.js';
wp_register_script( 'ajax-script', $script, array ('jquery') );
wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
wp_enqueue_script( 'ajax-script' );
// When defining the hooks below, I made sure to use show_product_list
// because that is the action we sent.
// Once the action is found, it's executed, and the function show_product_list runs.
add_action( 'wp_ajax_nopriv_show_product_list', 'show_product_list' );
add_action( 'wp_ajax_show_product_list', 'show_product_list' );
function show_product_list(){
// Here you can get the shortcode you sent like
$shortcode = $_POST['shortcode'];
// Then do the shortcode
echo do_shortcode($shortcode);
}
Надеюсь, это имеет больше смысла.