0

I'm just guessing it's possible so I tested and don't work for me. My Scenario here is :

function load_javascripts(){  
    wp_register_script( 'user-script', plugins_url( '/js/youtube-script.php', __FILE__ ), array( 'jquery' ) );
    wp_enqueue_script( 'user-script' );  
}
add_action('init','load_javascripts');

I am including a PHP file that has a majority of javascript in it (the include file)

Do you guys have any suggestion please ?

3
  • What exactly is the problem/question? Commented Jan 31, 2013 at 10:01
  • I'm not sure if I did it right. To have a php included on my plugin with javascripts in it Commented Jan 31, 2013 at 10:05
  • It should be no problem. As long as you have the extension .php for your file, the server will parse it as a php file and it will be send to the client as a javascript file. Commented Jan 31, 2013 at 20:03

1 Answer 1

0

You should use wp_localize_script() to inject PHP variables in your javascript files.

PHP Example

 <?php
wp_enqueue_script( 'some_handle' );
$array_to_js = array( 'some_string' =>'hello world' );
wp_localize_script( 'some_handle', 'object_name', $array_to_js );
?> 

Javascript:

 <script>
alert(object_name.some_string); // alerts 'hello world'
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

hmmmn, I don't seem to understand it. I am trying to include a javascript file which is also using PHP variables to my wordpress plugin. not just an javascript object
You are sending your PHP variable using wp_localize_script and then retrieving it as an object. Read this and that to get a better idea.
thanks @rrikesh, I have done it and it works.. can you suggest me for a more passing values here stackoverflow.com/questions/14633450/…
You should consider accepting my answer if it helped you (click on the green tick sign next to my answer. I answered your other question: stackoverflow.com/a/14644486/1370034
hi, yeah I appreciate your answer and helped me. where's the tick you're saying? I can't vote up on your answer yet.

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.