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.

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
    /themes
    /folder2
    /somefiles
    /includeme.php

Within themes is

/default
/theme2

Then within each theme are the files E.g.

Header.php
Footer.php

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

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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';
share|improve this answer
    
I would also mention plugin_dir_url since it seems like he will be storing some assets for the front end in there. –  Wyck Oct 13 '12 at 1:37
    
Thanks a lot guys!!!! I'm pretty amateur and have been struggling for days. It works perfectly!!! –  equilibrium3 Oct 13 '12 at 3:07
    
You should not use a constant, that would pollute the global namespace. Just use a function or a variable. –  toscho Oct 13 '12 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. –  mattnewbie Mar 4 at 20:36

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.