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' ] );
?>
7
  • 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
    Commented Apr 11, 2017 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
    Commented Apr 11, 2017 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
    Commented Apr 11, 2017 at 16:45
  • I tried all of the things you said.
    – codemonkey
    Commented Apr 11, 2017 at 16:53
  • Are you certain you are calling do_something after the_post action fires and not before?
    – Milo
    Commented Apr 11, 2017 at 18:25

2 Answers 2

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' ] );
7
  • 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
    Commented Apr 11, 2017 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). Commented Apr 11, 2017 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. Commented Apr 11, 2017 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
    Commented Apr 12, 2017 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) Commented Apr 12, 2017 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 and acknowledge you have read our privacy policy.

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