Josh Pyzer
Josh Pyzer

Reputation: 65

Inserting PHP into external .js file

I need to be able to insert some PHP into an external .js file. Is this possible? I have created a slider using basic slider and need to have the page titles as markers at the top of the slider.

This is the code the .js file uses to generate the markers currently.

var slidenum    = key + 1, 
var marker = $('<li><a href="#">'+ slidenum +'</a></li>'); 

I need to replace '+ slidenum +' with the WordPress function 'get_title'. Can this be replaced with php?

Upvotes: 1

Views: 1212

Answers (5)

RRikesh
RRikesh

Reputation: 14381

You can define JS variables in your php files, then use those variables in your external js.

for example, in one of your PHP files, you can add:

<script type="text/javascript">
// variable1 = number
variable1 = <?php echo $var1; ?>;
// variable2 = string
variable2 = "<?php echo $var2; ?>";
</script>

And for your question:

<script type="text/javascript">
slidenum = "<?php the_title(); ?>";
</script>

the_title() reference: http://codex.wordpress.org/Function_Reference/the_title

Update - The Loop:

<?php 
$slider_titles = array();
if ( have_posts() ) : while ( have_posts() ) : the_post(); 
  // your codes go here
  $slider_titles[] = get_the_title(); // adds the title to the array  
endwhile; endif;
?>

<script type="text/javascript">
slidenum = <?php echo json_encode($slider_titles); ?>;
</script>

When your loop is over then you add the javascript part

Upvotes: 1

SDC
SDC

Reputation: 14222

Some people are trying to tell you to use Ajax for this. That isn't the solution in this case. The variable is static for the lifetime of the page load, so there's need to keep going back to the server to get the value.

Others are suggesting setting the server to parse JS files as PHP so that you can embed PHP code into them. This also isn't a good answer, because you will lose all the browser's ability to cache the JS file, which will slow down your site and increase your bandwidth costs.

The solution is simply to add a separate single chunk of JS code to your page header -- ie add a small <script> tag to your page template, setting the variable in question.

The variable will then be accessible as a global in any JS code your run elsewhere in the page.

Upvotes: 1

m4t1t0
m4t1t0

Reputation: 5721

Another solution, not the best IMHO, is tell Apache to parse js files as php files. Simply put this in your .htaccess file:

AddType application/x-httpd-php .js
AddHandler x-httpd-php5 .js

<FilesMatch "\.(js|php)$">
SetHandler application/x-httpd-php
</FilesMatch>

Upvotes: 0

Erwin Moller
Erwin Moller

Reputation: 2408

Not directly. You can jump through a few hoops to get it done.

Problem is that .js doesn't get interpreted by PHP at all.

So if you must, you can use mod_rewrite and the like to pretend your php script is actually an .js.

Or write the js file from PHP completely to filesystem.

Or use XHR (Ajax) to fetch a certain value.

Upvotes: 0

konqi
konqi

Reputation: 5217

Yes, you can replace slidenum with get_title. No, you cannot do this with javascript nor at runtime. You'll need to use AJAX for that.

Upvotes: 0

Related Questions