1

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' ] );
?>
  • It is null when you echo from the PHP function or null when i sent it via JQuery to a JS function.? I don't see where you're sending it to JS. – Howdy_McGee Apr 11 '17 at 16:25
  • Sorry, I didn't include that part because it was irrelevant, in javascript I use an alert box to see what echo prints , but the point is it's null in the PHP function. – codemonkey Apr 11 '17 at 16:31
  • why not use the global outside of a function and set $myId right after then jsut call $myId without redeclaring global again? other than that add $post_object to your function – rudtek Apr 11 '17 at 16:45
  • I tried all of the things you said. – codemonkey Apr 11 '17 at 16:53
  • Are you certain you are calling do_something after the_post action fires and not before? – Milo Apr 11 '17 at 18:25
0

The way I'd do it is to not use global variables.

This is a simple class that uses 3 methods and 1 property to hook into the_post and echo the post ID in the footer.

/**
 * Plugin Name: WPSE_263293 Example
 */

class wpse_263293 {
  protected $ID;

  //* Add actions on init
  public function init() {
    add_action( 'the_post', [ $this, 'the_post' ] );
    add_action( 'wp_footer', [ $this, 'footer' ] );
  }
  public function the_post( $post ) {
    //* Only do this once
    remove_action( 'the_post', [ $this, 'the_post' ] );

    //* This is the property we're interested in
    $this->ID = $post->ID;
  }
  public function footer() {
    //* Print the ID property in the footer
    echo $this->ID;
  }
}
//* Initiate the class and hook into init
add_action( 'init', [ $wpse_263293 = new wpse_263293(), 'init' ] );
  • Thank you for response, but I'm not sure about how to use a class for a plugin. In examples on the tutorials, they are all about functions. – codemonkey Apr 11 '17 at 19:35
  • You can literally copy and paste the above code into a file in your wp-content/plugins folder and it will work. Or include it as part of your existing plugin (without the plugin header). – Nathan Johnson Apr 11 '17 at 19:39
  • WPSE stands for WordPress StackExchange and 263293 is the number of your question. Feel free to use whatever name for the class that you wish. It has to be unique though. – Nathan Johnson Apr 11 '17 at 20:34
  • I put all of my hooks to init() and functions to this class, I can see the page id as well on the footer, but still when I pressed the button, and I printed the $this->ID on the alert box, it's 0. – codemonkey Apr 12 '17 at 10:45
  • Then the issue is passing the variable to your javascript. Please open a new question showing how you're trying to pass the variable from PHP to javascript. (Hint: it should use wp_localize_script) – Nathan Johnson Apr 12 '17 at 12:13
0

Yeah, that's because wp_ajax_* fires before the_post. I found the solution on the second answer of that question: Getting the post_id in wp_ajax function

 $url     = wp_get_referer();
 $post_id = url_to_postid( $url ); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.