How to limit a WordPress plugin to just one page?

How to limit a plugin to just one page, or a few pages?  How to load a plugin only where you need it?

I encountered a problem, where plugin Contact Form 7, if activated, would slow down entire WordPress site to a crawl.  Contact Form 7 is great, and I want to continue to use it.  However, this one plugin makes every page load to the count of 13 or 14.  Without it, every page loads to the count of 2 or 3.  What can I do?

The solution turns out to be a very simple one.  Simply disable entire plugin unless you are loading certain pages that require this plugin.

Where to make the change? Each plugin has a PHP file with the name exactly the same as plugin itself.  In this case, open “wp-contact-form-7.php” file (located in the root of the plugin) and inserted this one single line of code after comments, but before the first line of code:

if ($_GET["page_id"] <> 12345) return;

Code 12345 stands for a page ID, where you store your contact form.  That is it. You only need this plugin on page with Contact Form 7 on it.  Do I need this plugin anywhere else?  What about Admin pages?  Do they still work?

OK.  That is right.  You also need this plugin on general plugin.php page (for option Settings to appear).  And you also need to load this plugin of a configuration page for the plugin. To take all this into account our simple IF statement grows into a more complex one:

if ($PHP_SELF == "/wp-admin/plugins.php" ) {
    if($_GET["page"] != null && $_GET["page"] != "wpcf7") return;
}
else{
    if ($_GET["page_id"] != 12345 && $_GET["page"] != "wpcf7") return;
}

This complicated negative IF statement will only allow proceeding to a next statement in 3 cases:

01 .  if  $_GET[“page_id”] == 12345
02.  if ($PHP_SELF == “/wp-admin/plugins.php” && $_GET[“page”] == null
03.  if $_GET[“page”]  == “wpcf7

Now we achieved the best of both worlds: we are still able to utilize the slow plugin, and we do not slow the entire WordPress site down.  Excellent!

Side Effect and Potential for Improvement

Contact Form 7 - TwiceIt is very interesting, but with my modest modification, I noticed a curious side effect:  Contact Form 7 plugin is now appears on plugin.php page twice.  Very interesting, but this doesn’t really harm anything.  I will wait, if someone knows, why something like this is happening.  Happy endeavors.

  [2013-02-14 TH  11:17]

Unexpected Solution

I found solution on the Internet over a lunch break. It turns out to prevent any plugin to be listed twice on your plugin.php page, simply delete a backup copy of your PluginNameBACKUP.php page that you created for backup purposes. For example you might have ContactForm7BEFORE.php in plugin root directory.   Simply delete it, or change your php extension into phpBACKUP (or anything else unrecognizable by your Web server).

(Visited 223 times, 1 visits today)

Be the first to comment

Your question, correction or clarification Ваш вопрос, поправка или уточнение