Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks! this is exactly what I've been searching for! –  DOA Jul 7 '11 at 13:05

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>'; 

?>
share|improve this answer
    
Shouldn't be putting JS in the header unless absolutely necessary. Secondly there are much better ways sending PHP variables to JS –  Brady Jul 7 '11 at 12:44
1  
Yeah, it's nasty and crude, but hey, when you need a fix! –  tim Jul 7 '11 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 '11 at 13:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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