Title: Show $post_id via javascript function
Last modified: July 24, 2017

---

# Show $post_id via javascript function

 *  [punchlines](https://wordpress.org/support/users/punchlines/)
 * (@punchlines)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/show-post_id-via-javascript-function/)
 * Hi…
 * In comment_template.php there’s the id of the active post. In PHP, it’s $post_id.
 * If I do this in comment_template.php:
 * `echo $post_id`
 * The post id shows up in just above the comment:
 * `<img src="http://dalcotech.com/d3/dtest.jpg" alt="dtest" />`
 * How do I show it in a javascript function that I can call. This doesn’t work:
 *     ```
       	function getJSPostID() {
       		try {
       			var test='<?php echo $post_id; ?>';
       			return test;
       		} catch (e) {
       			return 'unavailable';
       		}
       	}
   
       	var ID = getJSPostID(); <--- Returns a string "<?php echo $post_id; ?>"
       ```
   
 * Thanks
    Fred
    -  This topic was modified 8 years, 8 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).
      Reason: put code in backticks

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [elsnertechnologies](https://wordpress.org/support/users/elsnertechnologies/)
 * (@elsnertechnologies)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/show-post_id-via-javascript-function/#post-9346349)
 * ou have to replace ” echo ” to ” print “.
 *     ```
       function getJSPostID() {
       try {
       var test='<?php print $post_id; ?>’;
       return test;
       } catch (e) {
       return ‘unavailable’;
       }
       }
   
       var ID = getJSPostID(); <— Returns a string “<?php print $post_id; ?>”
       ```
   
    -  This reply was modified 8 years, 8 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).
      Reason: put code in backticks
 *  Thread Starter [punchlines](https://wordpress.org/support/users/punchlines/)
 * (@punchlines)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/show-post_id-via-javascript-function/#post-9346606)
 * Thanks, but that doesn’t work either.
 * Here’s what I have:
 *  function getJSPostID() {
    try { var test=”<?php print $post_id; ?>”; return 
   test; } catch (e) { return “unavailable”; } }
 *  function showMe() {
    var ID = getJSPostID(); alert(“ID is ” + ID);
 * and here’s what happens:
 * ![dtest1](https://i0.wp.com/dalcotech.com/d3/dtest1.jpg)
 * Fred
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/show-post_id-via-javascript-function/#post-9347113)
 * Hi Fred,
 * Your JS is not interpreted as PHP, so PHP code simply appears as plain text. 
   For PHP within JS to be interpreted, the entire JS code has to be output as part
   of a page’s HTML output within <script> tags. This is OK for brief snippets, 
   but most JS should be part of an external file. That file is not interpreted 
   as PHP, so dynamic content there is out. (there’s a workaround for that but it’s
   generally not warranted)
 * As with passing comment parent IDs (as discussed on your other topic), post IDs
   can be passed in a similar manner by including it in the JS call that’s on an
   HTML page that is the result of interpreting PHP.
 * There is another option. This applies for general situations where values in 
   PHP need to be passed to JS code that is often times in external files. External
   JS files should be enqueued, which among other things, establishes a “handle”
   you can use to refer to that script file. You can pass PHP values using that 
   handle with `wp_localize_script()`. Using this function will result in a script
   block in the page’s head section that declares the values determined with PHP
   as JS variables.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Show $post_id via javascript function’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [8 years, 8 months ago](https://wordpress.org/support/topic/show-post_id-via-javascript-function/#post-9347113)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
