WordPress.org

Support

Support » Plugins and Hacks » Hacks » How to load file from plugin directory instead of theme directory

How to load file from plugin directory instead of theme directory

  • Tom

    @jeffreeeeey

    Hi,

    I’ve written some code (of which its features are irrelevant). The code is currently placed in the themes functions.php file:

    function add_plugin($plugin_array) {
       $plugin_array['info'] = get_template_directory_uri().'/custom/tinymce/shortcodes.js';
       $plugin_array['warning'] = get_template_directory_uri().'/custom/tinymce/shortcodes.js';
       $plugin_array['success'] = get_template_directory_uri().'/custom/tinymce/shortcodes.js';
       return $plugin_array;
    }

    What I’m trying to do is move this from the themes functions.php file, and instead create a plugin.

    As you can see from the code above, that’s fetching the shortcodes.js file from a folder within the theme directory. I need it to fetch the file from within the plugin directory instead.

    I’m aware of:
    plugins_url
    ..but I’m not entirely sure how to implement it to my code as admittedly PHP’s not my strong-point.

    Any help would be greatly appreciated. Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • catacaustic

    @catacaustic

    very awesome

    Assuming that you’re calling this from the main file of your plugin, you’d use it like this:

    function add_plugin($plugin_array) {
       $plugin_array['info'] = plugins_url( 'custom/tinymce/shortcodes.js' , __FILE__ );
       $plugin_array['warning'] = plugins_url( 'custom/tinymce/shortcodes.js' , __FILE__ );
       $plugin_array['success'] = plugins_url( 'custom/tinymce/shortcodes.js' , __FILE__ );
       return $plugin_array;
    }

    There’s other options too, but that’s one way of doing it.

    Tom

    @jeffreeeeey

    I think I’ve figured it out (it works at least) but would still appreciate it if someone can tell me if this is the correct way of doing so:

    function add_plugin($plugin_array) {
       $plugin_array['info'] = plugins_url('shortcodes/tinymce/shortcodes.js', __FILE__);
       $plugin_array['warning'] = plugins_url('shortcodes/tinymce/shortcodes.js', __FILE__);
       $plugin_array['success'] = plugins_url('shortcodes/tinymce/shortcodes.js', __FILE__);
       return $plugin_array;
    }
    Tom

    @jeffreeeeey

    Wow, great timing @catacaustic. Thanks for confirmation 🙂

    catacaustic

    @catacaustic

    very awesome

    Always like to see that my answers are in line with someone else. Makes me feel slightly less paranoid sometimes. 😉

    I’m glad that it looks like it’s going to work as well. One thing that I’ve done in plugins that use these values a lot is define a constant for that plugin with the base URI and let everything else use that. Something like this.

    Tom

    @jeffreeeeey

    haha!

    I kind of understand how that works but I think my brain’s pickled enough for one night so going to leave it at that for now.

    Will definitely look at what you’ve suggested for a later version though.

    Thanks again for the help, most appreciated

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to load file from plugin directory instead of theme directory’ is closed to new replies.
Skip to toolbar