0

I've got jwplayer installed in wordpress and have it showing in a single.php page... the way I have it I need to get a custom field in the javascript and I;m having no luck echoing it in there. I know you cant straight up run PHP in a javascript function, hence I'm here asking noob questions :)

<script type='text/javascript'>
jwplayer("lbp-inline-href-1").setup({
'flashplayer': '/wp-content/uploads/jw-player-plugin-for-wordpress/player/player.swf',
'image': '/wp-content/themes/sometheme/images/vid.png',
'file': '<?php echo get_post_meta($post->ID, 'video', true); ?>',
'skin': '/wp-content/plugins/jw-player-plugin-for-wordpress/skins/skewd.zip',
'stretching': 'exactfit',
'controlbar': 'bottom',
'width': '640',
'height': '360'
});
</script> 

is it possible to get

<?php echo get_post_meta($post->ID, 'video', true); ?>

Running in there for the file path?

Thanks for any advice.

2 Answers 2

3

Have a read through this article:

Adding Scripts Properly to WordPress Part 2 – JavaScript Localization

What you are doing here is passing your PHP values to the WordPress JS localization system. This then allows your JS code to get access to those variables you have passed.

0
0

Echo out the JavaScript in your header.php file

<?php 

echo '<script type="text/javascript">'; 

echo 'loads of JavaScript code';

echo get_post_meta($post->ID, 'video', true);

echo '</script>'; 

?>
3
  • Shouldn't be putting JS in the header unless absolutely necessary. Secondly there are much better ways sending PHP variables to JS
    – Scott
    Jul 7, 2011 at 12:44
  • 1
    Yeah, it's nasty and crude, but hey, when you need a fix!
    – tim
    Jul 7, 2011 at 12:46
  • Seems to do the job fine :) but I'll look into the recommended wordpress methods. Thanks for helping out!
    – DOA
    Jul 7, 2011 at 13:06

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.