[wp-hackers] howto including full path in javascript file.

Hikari lists at hikarinet.info
Mon Feb 8 19:23:32 UTC 2010


I've needed that once too :)


Let's say you add your script:



<?php
add_action('wp_print_scripts', 'prefix_scriptAction');

function prefix_scriptAction(){
    wp_enqueue_script('prefix_js', plugin_dir_url(__FILE__).'myjs.js');
}
?>



First of all, you should avoid running your script just during the page load, because if you do so you'll make the page load slowly. 
Use the 'onload' event or add it to the footer ('wp_print_footer_scripts' action).

Either way, use a JavaScript variable to store the path to your plugin's root folder. This variable should be present when your 
script needs it, so if your script runs after or just after the page load, I believe you can use the 'wp_print_scripts' action to 
add the variable to the header. Just change the previous function to include it.



<?php
add_action('wp_print_scripts', 'prefix_scriptAction');

function prefix_scriptAction(){
    wp_enqueue_script('prefix_js', plugin_dir_url(__FILE__).'myjs.js');

?>

<script type="text/javascript">
    var prefix_path = <?php echo plugin_dir_url(__FILE__); ?>;
</script>

<?php
}
?>


I didn't test the code, but it's an exemple. This way, your script can try for 'prefix_path', and if the code is right it will store 
the URL path to your plugin's root folder, and using it you can append subfolders and files, having access to what you want.


I've already used a proprietary dropdown menu that its JS considered it would be always used in homepage, and when it run inside 
posts and other resources its images were being deleted, because they weren't where the script was expecting :P


---------------------------
Hikari -  A Luz ilumina a PAZ
http://Hikari.ws
http://ConscienciaPlanetaria.com

Tenha seu próprio email meunome @ ConscienciaPlanetaria.com.br!: http://seunome.ConscienciaPlanetaria.com.br



More information about the wp-hackers mailing list