Final state: As Nathan Johnson stated, I change my plug in to a class, still can't use post id in increment_like function...
<?php .... //irrelevant parts skipped
class wpse_263293 {
protected $ID;
//* Add actions on init
public function init() {
add_action( 'the_post', [ $this, 'createLikeButton' ] );
add_action( 'wp_footer', [ $this, 'footer' ] );
add_action('wp_enqueue_scripts',[ $this ,'button_js']);
add_action('wp_ajax_increment_like', [$this,'increment_like']);
add_action('wp_ajax_no_priv_increment_like',[$this,'increment_like']);
}
public function footer() {
//* Print the ID property in the footer
echo $this->ID; //This one works
}
public function createLikeButton( $post ) {
$this->ID = $post->ID;
$myId=$post->ID;
echo "<button onclick=\"likeButton()\" p style=\"font-size:10px\" id=\"likeButton\">LIKE</button>";
}
public function button_js(){
wp_enqueue_script( 'likeButton', plugins_url( '/like.js', __FILE__ ),array('jquery'));
wp_localize_script('likeButton', 'my_ajax_object', array( 'ajax_url' =>admin_url('admin-ajax.php')));
wp_localize_script('likeButton', 'new_ajax_object', array( 'ajax_url' =>admin_url('admin-ajax.php')));
}
public function increment_like() {
echo $this->ID."test"; //I only see test
/* irrelevant code*/
}
}
//* Initiate the class and hook into init
add_action( 'init', [ $wpse_263293 = new wpse_263293(), 'init' ] );
?>
when i sent it via JQuery to a JS function.
? I don't see where you're sending it to JS.