WordPress.org

Ready to get started?Download WordPress

Forums

How to load file from plugin directory instead of theme directory (6 posts)

  1. Tom
    Member
    Posted 1 year ago #

    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

  2. catacaustic
    Member
    Posted 1 year ago #

    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.

  3. Tom
    Member
    Posted 1 year ago #

    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;
    }
  4. Tom
    Member
    Posted 1 year ago #

    Wow, great timing @catacaustic. Thanks for confirmation :)

  5. catacaustic
    Member
    Posted 1 year ago #

    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.

  6. Tom
    Member
    Posted 1 year ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.