Link to home
Start Free TrialLog in
Avatar of Tim Brocklehurst
Tim BrocklehurstFlag for United Kingdom of Great Britain and Northern Ireland

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`):

	```
    use AjaxLiveSearch\core\Config;
    use AjaxLiveSearch\core\Handler;
    
    if (session_id() == '') {
        session_start();
    }
    
    $handler = new Handler();
    $handler->getJavascriptAntiBot();

Open in new window


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';

Open in new window


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();

Open in new window

... 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/Handler.php ...?

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
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tim Brocklehurst

ASKER

Thank you Julian - that's really helpful, and puts everything in perspective.
I'll modify my script accordingly and let you know how it goes...
Tim
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