Tim Brocklehurst
asked on
How to reference a plugin class from a theme template in WordPress
I'm trying to incorporate Ajax Live Search in a custom WordPress plugin.
I have copied the necessary files into a subdirectory of my plugin, and followed the readme file instructions to set the variables and work the plugin into the site.
I get stuck on point 5 however, which reads like this:
5. Make sure `core/Handler.php` and `core/Config.php` are included in your (PHP) page and you have these lines at the very top of the file (Check `index.php`):
So I have put an include statement at the top of a custom template php file in my theme for Config.php and Handler.php like this:
So far so good... although if I try using require_once instead of include, as it does in the attached example index.php (supplied by Ajax Live Search), WordPress throws an error.
I'm not familiar with the 'use' statement in php so I'm not sure whether (nor how) to modify the paths it gives. That means that when I try calling the Handler class, with
So my question is - how can I reference the Handler class from my template file (from the theme directory of a WordPress installation), when the class resides in Handler.php which is in a subdirectory of my plugin folder: ie plugins_url(). '/my_plugin/AjaxLiveSearch /Handler.p hp ...?
Can someone give me what the php 'use' statement should be in this context (as per lines 2 & 3 of the first code sample) ?
I have copied the necessary files into a subdirectory of my plugin, and followed the readme file instructions to set the variables and work the plugin into the site.
I get stuck on point 5 however, which reads like this:
5. Make sure `core/Handler.php` and `core/Config.php` are included in your (PHP) page and you have these lines at the very top of the file (Check `index.php`):
```
use AjaxLiveSearch\core\Config;
use AjaxLiveSearch\core\Handler;
if (session_id() == '') {
session_start();
}
$handler = new Handler();
$handler->getJavascriptAntiBot();
So I have put an include statement at the top of a custom template php file in my theme for Config.php and Handler.php like this:
include plugins_url().'/my_plugin/AjaxLiveSearch/core/Handler.php';
include plugins_url().'/my_plugin/AjaxLiveSearch/core/Config.php';
So far so good... although if I try using require_once instead of include, as it does in the attached example index.php (supplied by Ajax Live Search), WordPress throws an error.
I'm not familiar with the 'use' statement in php so I'm not sure whether (nor how) to modify the paths it gives. That means that when I try calling the Handler class, with
$handler = new Handler();
... it causes a site error, presumably because it can't find it...?So my question is - how can I reference the Handler class from my template file (from the theme directory of a WordPress installation), when the class resides in Handler.php which is in a subdirectory of my plugin folder: ie plugins_url(). '/my_plugin/AjaxLiveSearch
Can someone give me what the php 'use' statement should be in this context (as per lines 2 & 3 of the first code sample) ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Its been a while, Julian, so apologies for the delay.
Your solution was indeed very helpful and solved the problem for me in the short-term. However, I continued to find complications with that particular plugin so went with something else in the end.
Thanks again for your help.
Best wishes
Tim
Your solution was indeed very helpful and solved the problem for me in the short-term. However, I continued to find complications with that particular plugin so went with something else in the end.
Thanks again for your help.
Best wishes
Tim
ASKER
I'll modify my script accordingly and let you know how it goes...
Tim