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.

Short Version

I need to give in the absolute path of a Javascript file that is to be hooked into something. It works if I hardcode it into

'http://mywebsite.com/folder/script.js'

However I prefer not to hardcode it in. I can do dirname(___FILE___), which gives me this:

'/home/public_html/folder/script.js'

But that fails, the script isn't loaded. What's the best practice to find the absolute path (starting with 'http://')?

-

Long Version

On this page in the Wordpress documentation relating to the tinyMCE plugin creation, I found this:

// Load the TinyMCE plugin : editor_plugin.js (wp2.5)
function add_myplugin_tinymce_plugin($plugin_array) {
   $plugin_array['myplugin'] = URLPATH.'tinymce/editor_plugin.js';
   return $plugin_array;
}

There's a note at the bottom saying:

Note: when using the mce_external_plugins filter, the url should point to the actual plugin file, as in the example above.

A bit higher in the documentation they say

mce_external_plugins: passes in/out an associative php array 'plugin_name' => 'plugin_url'. The url should be absolute and on the same domain as your WordPress installation.

I am completely puzzled because I can't find any information about 'URLPATH' which is used in their example. I don't think it exists in PHP, WP or TinyMCE, but I could be wrong.

Incidentally, were this to be an error in the WP documentation, what would I replace URLPATH with to make it work?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Assuming your resource is included in your Plugin:

  • plugins_url() is the URL to \wp-content\plugins\ (Codex ref)

So if you have a .js file in \wp-content\plugins\plugin-slug\file.js:

<?php echo plugins_url() . '\plugin-slug\file.js'; ?>

BUT WAIT!!

If your purpose is actually to load a javascript file, you actually want to enqueue that file, using wp_enqueue_script(), hooked into the wp_enqueue_scripts hook.

And wp_enqueue_script() uses the filepath rather than the URL, so you could use dirname(___FILE___) if that' what you're comfortable with.

share|improve this answer
    
Is there a reason you are not using the parameters to plugins_url(), so it will determine the plugin name too? I once read that plugins should keep working, even if the site owner changes the individual plugin folder name. –  Jan Fabry Jul 18 '11 at 21:04
1  
fixed it with: plugins_url( '' , FILE ); thanks both –  joon Jul 18 '11 at 21:23
3  
You should use the first parameter too instead of string operations. plugins_url('script.js',__FILE__); for example. –  Otto Jul 18 '11 at 22:34

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.