2

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

2

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 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. – fuxia 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 '14 at 20:36

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.