Let’s say this was in my plugin:
class pluginslug_foo {
public function bar() {
//stuff
}
}
and I wanted to make the method bar available for use outside of the plugin, for instance in a theme file so it could be called with pluginslug_bar();
.
I tried:
function pluginslug_get_foo() {
$foo = new pluginslug_foo();
return $foo;
}
function pluginslug_bar() {
$bar = $foo->bar;
}
But I got an unknown variable error for $bar when I tried pluginslug_bar();
in my theme:(
An alternative way is to use static class methods in plugins, and optionally write functions as alias:
in Plugin:
in Theme:
or
Of course static methods and variables not always fit the scope, and this is a general theoric example: without know your real scope/code is impossible to say if it’s good for you or not.
If your arenât very familiar with PHP, use simple actions and filters in your theme, and register callbacks for those in your plugin class.
A basic example
Plugin
Theme
You have made a mistake in your functions.
pluginslug_bar
function doesn’t contain$foo
variable, you need to initialize it first:Then in your theme’s
functions.php
file you can call it like this: