Last active
February 6, 2024 14:11
-
-
Save Anatal/9f9068c1d0c7b48b50474f1f971a7633 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' ), | |
'post_id' => get_the_id(), | |
) ); // 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_click_count', 'click_count_func' ); // If called for logged users | |
add_action( 'wp_ajax_nopriv_click_count', 'click_count_func' ); // If called for non logged users | |
function click_count_func(){ | |
if(isset($_POST['btn_name'])){ | |
$meta_key = $_POST['btn_name']; | |
$post_id = $_POST['post_id']; | |
$click_count = get_post_meta( $post_id, $meta_key, true); // Get the click count from meta key post | |
$click_count++; // Rise the click_count by 1 | |
update_post_meta( $post_id, $meta_key, $click_count); | |
echo $post_id .": ". $meta_key . ": " . $click_count . " rised!"; | |
} | |
die(); | |
} | |
// 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'); | |
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' ), | |
'post_id' => get_the_id(), | |
) ); | |
} | |
add_action( 'wp_enqueue_scripts', 'hello_elementor_child_enqueue_scripts', 20 ); | |
/* Anatal's code */ | |
add_action( 'wp_ajax_click_count', 'click_count_func' ); // If called for logged in users | |
add_action( 'wp_ajax_nopriv_click_count', 'click_count_func' ); // If called for guests (non logged in users) | |
function click_count_func(){ | |
if(isset($_POST['btn_name'])){ | |
$post_id = $_POST['post_id']; // get the post id from back end | |
$meta_key = $_POST['btn_name']; // get the 'data-btn' from front end | |
$click_count = get_post_meta( $post_id, $meta_key, true); // Get the click count from meta key post | |
$click_count++; // Rise the click_count by 1 | |
update_post_meta( $post_id, $meta_key, $click_count); | |
echo $post_id . ": " . $meta_key . ": " . $click_count . " rised!"; | |
} | |
die(); | |
} |
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'), | |
post_id: click_ajax.post_id, | |
}, | |
success: function(msg){ | |
console.log(msg); | |
} | |
}); | |
}); | |
}); | |
// jQuery fix for Wordpress | |
jQuery(document).ready(function ($) { | |
$('.click-count-btn').on("click", function (event) { | |
//event.preventDefault(); | |
$.ajax({ | |
type: 'post', | |
dataType: 'json', | |
url: click_ajax.ajax_url, | |
data: { | |
action: 'click_count', | |
btn_name: $(this).attr('data-btn'), | |
post_id: click_ajax.post_id, | |
}, | |
complete: function(){ | |
//var href = $(this).next('a.elementor-icon').attr('href'); | |
//console.log(href); | |
//if ( typeof href !== 'undefined'){ | |
//window.location.href = href; | |
//} | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment