1

I'm working on my first WP plugin, and I'm having trouble getting a file include to work. My directory structure looks like this:

  • myPlugin
    • classes
    • templates
    • css
    • js
  • myplugin.php

My main plugin file (myplugin.php) includes / requires various files in the classes directory, and that works fine. However, one of the files in the classes directory is supposed to include a file from the templates directory. I've tried everything I can think of, from plugins_url() to plugin_basename() to plugin_dir_path() but nothing seems to work.

How should this statement look to get it to do what I'm trying to accomplish?

3

Put this in the main plugin file (myplugin.php):

defined( 'MY_PLUGIN_URL' ) || define( 'MY_PLUGIN_URL', plugin_dir_url(__FILE__) );
defined( 'MY_PLUGIN_PATH' ) || define( 'MY_PLUGIN_PATH', plugin_dir_path(__FILE__) );

right before any other code, and use MY_PLUGIN_PATH to include files, or MY_PLUGIN_URL to load your assets.

E.G:

  • require_once MY_PLUGIN_PATH . 'templates/se.php';
  • wp_enqueue_style( 'my-css', MY_PLUGIN_URL . 'assets/css/style.css' );

Those constants can be accessible from any other file including the main plugin loader file and the files within the sub-directories. As long as the plugin is loaded correctly.

Hope that helps.

  • 1
    Thanks, Samuel - exactly what I was trying to think of. Worked like a charm! – Paul Srch Sep 5 '16 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.