1

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?

1
  • You can't do it unless you tell your webserver to send those to PHP to be interpreted. There are alternatives. I see other answers already has those alternatives. Commented Oct 8, 2012 at 10:59

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

12 Comments

Note that if you're using a string and not a number/boolean you need to surround the PHP tags with either ' or "'s.
Yes, that's a good point to node. It can be easily missed. I updated the answer.
There's another problem: If you want to pass quotes or doubleQuotes it won't work. To avoid this problems i write all Values to pass into one PHP-Array and define the var like this: var variables = <?php json_encode($myArray); ?>;
OP wants to pass a post title, it's relatively safe to assume that he knows if quotes are present. addslashes can also be used if it is the case.
@rrikesh - json_encode and addslashes do pretty much the same thing here, but json_encode will do it better. In fact, addslashes is generally the wrong answer to any question; it doesn't escape everything that might need escaping in any context - if you value your code security/reliablility, you should never use addslashes for anything.
|
1

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.

Comments

0

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.

Comments

0

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.

Comments

0

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>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.