I have a plugin which I need to include a file in but it doesn't include.

Here's the example of the tree:

plugin folder

Within themes is


Then within each theme are the files E.g.


The plugin simply switches these themes via the admin.

Now in header.php I added

<?php include('includeme.php'); ?>

Which didn't work.

A friend told me since it is two levels up the tree I should actually add

<?php include('/../../includeme.php'); ?>.

Both didn't work.

I googled something that said to try.

<?php include([$_SERVER]['DOCUMENT_ROOT']'./wp-content/plugins/plugin-name/includeme.php'); ?>.

That also didn't work. This is a basic plugin which renders theme based on admin settings combined with user agent detection.

Any suggestions or further code required?

The code is located at http://pastebin.com/A3buAA0R


1 Answer 1


In your main plugin file, use plugin_dir_path to define a constant that you can then use in all of your includes. see the example on the above codex page.

// define the constant in your main plugin file
define( 'MYPLUGINNAME_PATH', plugin_dir_path(__FILE__) );

then, to include your file:

include MYPLUGINNAME_PATH . 'includeme.php';
  • I would also mention plugin_dir_url since it seems like he will be storing some assets for the front end in there.
    – Wyck
    Commented Oct 13, 2012 at 1:37
  • Thanks a lot guys!!!! I'm pretty amateur and have been struggling for days. It works perfectly!!! Commented Oct 13, 2012 at 3:07
  • You should not use a constant, that would pollute the global namespace. Just use a function or a variable.
    – fuxia
    Commented Oct 13, 2012 at 6:48
  • So when I need to include a php file in the frontend of a plugin, should I use plugin_dir_path with init as the hook? Thank-you.
    – user28566
    Commented Mar 4, 2014 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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