1

I am creating a plugin that has a php file in the plugin directory which is accessed directly via a custom rewrite url.

I need this file to be able to use the three functions mentioned in the title.

At the moment, I am including the wp-load.php file which gives me access to all of these functions. However, I've been reading that wp-load shouldn't be included because it may not always be in the same place and it includes wordpress files that may not be needed.

This is how I'm including wp-load:

$wp_base = explode($_SERVER['PHP_SELF'], $_SERVER['SCRIPT_FILENAME']);
require($wp_base[0] . '/wp-load.php');

I've found that the functions I need are located in these wordpress files:

wp-includes/general-template.php
wp-includes/functions.php
wp-includes/link-template.php

Should I instead include only these three files, and will they always be located in the same place?

Or is there a better way to go about do this?

Thanks

  • There is most certainly a better way to do this. If you include these files, you are setting yourself up for all sorts of maintenance issues. What are you trying to do by executing a PHP file directly? My guess is that there is a better way to do what you want. – tollmanz Apr 8 '12 at 1:46
0

These functions are already accessible if you use the rewrite API. When your file is parsed WordPress has loaded already, so do not include core files a second time.

| improve this answer | |
  • The functions don't seem to exist without including wp-load, but I'm using the same method to create my rewrite as explained in the link you provided. After the rewrite is created I found this line added to my .htaccess file: RewriteRule ^mypage$ /wp-content/plugins/plugin-name/mypage.php [QSA,L] Wouldn't this bypass wordpress and load the php file directly? – Sarathi Hansen Apr 8 '12 at 3:18
  • Then something strange happend. Usually, there is no need for an extra line in the .htaccess. – fuxia Apr 8 '12 at 3:30
  • I think the problem is that I'm not rewriting to index.php so it adds the rule to htaccess instead of running it through wordpress. But I'm not sure how I could use index.php and still have it load my page. – Sarathi Hansen Apr 8 '12 at 3:58
  • Did you read the page I linked to? Everything is nicely explained there. For a code example see my Speed Dial Plugin. – fuxia Apr 8 '12 at 4:01
  • Thank you! I guess I didn't read the article well enough, but I got it to work by adding the query variable then requiring my php page on the template_redirect hook. – Sarathi Hansen Apr 8 '12 at 4:14

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.