-
-
Save barbareshet/4081a52f920329a892f9ae5401c3e213 to your computer and use it in GitHub Desktop.
WP Ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hello_elementor_child_enqueue_scripts() { | |
wp_enqueue_style( | |
'hello-elementor-child-style', | |
get_stylesheet_directory_uri() . '/style.css', | |
[ | |
'hello-elementor-theme-style', | |
], | |
'1.0.0' | |
); | |
wp_enqueue_script( 'hello-elementor-child-script', get_stylesheet_directory_uri() . '/script.js', array('jquery'), true ); | |
wp_localize_script( 'hello-elementor-child-script', 'click_ajax', | |
array( | |
'ajax_url' => admin_url( 'admin-ajax.php' ), | |
'handleClickNonce' => wp_create_nonce('click_count'), | |
) | |
); // must be the same name of the wp_enqueue_script | |
} | |
add_action( 'wp_enqueue_scripts', 'hello_elementor_child_enqueue_scripts', 20 ); | |
add_action( 'wp_ajax_cart_click_count', 'click_count_func' ); // If called from admin panel | |
add_action( 'wp_ajax_nopriv_click_count', 'click_count_func' ); // If called from front end | |
function click_count_func(){ | |
$result = [ | |
'success' => false, | |
] | |
if ( !isset($_POST['btn_nonce']) || !wp_verify_nonce($_POST['btn_nonce'], 'click_count')) { | |
$result['message'] = __('בקשה לא חוקית.', 'textdomain'); | |
wp_send_json($result); | |
} | |
if ( !isset($_POST['btn_name']) ) { | |
$result['message'] = __('בקשה לא חוקית.', 'textdomain'); | |
wp_send_json($result); | |
} | |
$meta_key = $_POST['btn_name']; | |
$click_count = get_post_meta( get_the_ID(), $meta_key, true); // Get the click count from meta key post | |
$click_count++; // Rise the click_count by 1 | |
update_post_meta( get_the_ID(), $meta_key, $click_count); | |
$result = $meta_key . ": " . $click_count . " rised!"; | |
wp_send_json($result); | |
} | |
// if the wp_localize_script don't work | |
function third_party_tracking_code_header(){ | |
echo '<script type="text/javascript"> | |
var ajaxurl = "' . admin_url('admin-ajax.php') . '"; | |
</script>'; | |
} | |
add_action('wp_head', 'third_party_tracking_code_header'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jQuery fix for Wordpress | |
jQuery(document).ready(function ($) { | |
$('.click-count-btn').on("click", function (event) { | |
event.preventDefault(); | |
console.log(click_ajax); | |
$.ajax({ | |
type: 'post', | |
dataType: 'json', | |
url: click_ajax.ajax_url, | |
data: { | |
action: 'click_count', | |
btn_name: $(this).attr('data-btn'), | |
'btn_nonce': click_ajax.handleClickNonce | |
}, | |
success: function(msg){ | |
console.log(msg); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment