0

I have a plugin that I'm working on and I'm having trouble getting the post id so jquery can use it. I'm able to get it using the admin_head hook but I'm not sure how I can return it to jquery. I'm not sure if admin_head is what I need since it does not return anything. Not sure if I can use global?

Also this is in the admin screen so not on the front end.

Here is what I have so far:

$custom_ID = 0;

function my_custom_admin_head(){
    global $post;
    $custom_ID = $post->ID;
    global $custom_ID;
}
add_action('admin_head', 'my_custom_admin_head');

add_action( 'admin_footer', 'my_action_javascript' );

function my_action_javascript() {?>
  <script type="text/javascript" >
    jQuery(document).ready(function($) {
        $('#get-rates-button').click(function(){
          var result = "<?php echo $custom_ID; ?>";
          alert(result);
        }
}

Is there a function that I could call from jquery that would be able to return the id from a hook or does someone know of a better way?

Thanks!

1 Answer 1

2

Using your code you could something like this:

function my_custom_admin_head(){
    global $post;
    echo '<script type="text/javascript"> var js_post_id =' . $post->ID . ';</script>';


}

And then just use the var js_post_id. You shoudl try to read about the script localization function and use it.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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