OOP Plugin – Exposing Class Functions
-
I’m trying to build a plugin in OOP fashion using Tom McFarlin’s excellent plugin boilerplate.
In the constructor I have:
add_action( 'end_loop', array( $this, 'hereman1' ) );In the core funtion area I have:
public function hereman1() {It is my understanding that a public function should be callable from outside the object, but when my single page template trys to call the function I get a Fatal Error that says the function is undefined.
I can call the function if my template calls
myobj::hereman1()What am I doing wrong?
-
If you’re using those two examples, you’ve got a problem with the object scope.
If you can call
myobj::herman1();in your templates, then the functionherman1()in yourmyobjclass isstatic. This means that you don’t need an object to run it on, you can call it straight from the class definition.When you try and set the action to
add_action("end_loop", array($this, "herman1"))the reference to$thismeans in the current object, not the current class.I haven’t seen the boilerplate that you’ve mentioned, but here’s an idea of how I do things inside an object…
class MyClass { public function __construct(){ add_action('init', array($this, "func1")); } public function func1(){ // Do something in here. } } $my_class = new MyClass ()And, how I do things from outside the object:
class MyClass { static public function func1(){ // Do something in here. } } add_action('init', array ('MyClass', 'func1'));Hopefully that will give you an idea of how things are supposed to work, and where you might be ahving an issue.
Maybe it’s me. I’ve taken both of your examples and pared down (deleted all the boilerplate code) to the point that my plugin.php is in the case of your second example these few lines:
<?php class PHZplug { static public function hereman1() { return 'Yup, I am Here' ; } } add_action( 'init', array( 'PHZplug', 'hereman1' ) ); ?>I’m still unable to make a call to hereman1() in my template. I know the plugin is being loaded because the error goes away if I edit the template to read PHZplug::hereman1().
Shouldn’t the add action call make it so I can call the function directly?
Hang on… where are you trying to add the action? From your last response there it sounds like you’re trying to use add-action() in your template file? That’s never going to work. Any time you need to use add_action() it needs to be done in your themes functions.php file, or in your plugins main file (or another file included by either of these). It won’t work in a template because add_action() doesn’t product any content. It only adds that function to a list that’s run when that action is called, and the ‘init’ action is always called way before any template files are read in.
Nom no! The add_action is in plugin.php, just like in the example above.
In my original example I was using end_loop as the hook, but simplified it because that was the hook you cited as an example.
What’s in the template is a call to the function:
$result = hereman1() ;That function is undefined. If I prepend PluginName:: it works.
So when does your template file call
do_action('end_loop')? That waht you need to do to actually run your function/functions.Any time you add an custom action like that, your function is run when the action is called using the function above. If you don’t use that code then your function won’t ever be called.
The template file doesn’t call do action.
Then that’s your problem. You have to call do_action() to run the functions that are set for that action. That’s why your function isn’t being run like that – your template never tells it to run.
I think that light just came on.
So,how the do you get a plugin to “just do a job” so that templates don’t have to be edited?
Say, for example, I want my plugin to run every time at the end of a post or page?
By using do_action(). That’s all that you need. You use add_action() to add the action when your plugin is set up, and then do_action() in the template when you want it to run. You can use one of the existing actions, or create your own like you’ve done with ‘end_loop’ now.
OK you have me thouroughly confused now. The end_loop hook is no longer in play. I replaced it with init several messages back.
Let’s fresh out a second.
My plugin.php reads as follows:
<?php class PHZplug { function __construct() { add_action( 'init', array( $this, 'hereman1' ) ); } public function hereman1() { return 'Yup, I am Here' ; } } $newplug = new PHZplug() ; ?>In the constructor is the add_action, and the public function hereman1 returns a string.
In the template file I have:
$result = do_action('hereman1') ; echo $result.'aaaaa<br>' ;Now I don’t get the unidentified function error, but I’m not getting the return value of the function either.
Can you show me a page where that’s being used?
When you add a function to the ‘init’ action it is run when the system is first initialised, so that is way before any templates are read or anything is output. You cannot use that to run something in a template file.
This is more like what you want…
Class file:
class PHZplug { function __construct() { add_action( 'end_loop', array( $this, 'hereman1' ) ); } public function hereman1() { return 'Yup, I am Here' ; } } $newplug = new PHZplug() ;In your template file:
do_action('end_loop');That’s all that you need.
I don’t think end_loop is a good tag since, like init, is is an action hook name that WP fires when the loop is finished.
I need to play with this a bit.
What I’m trying to do is create a plugin that fires automatically, without needing a spcific action call whenever the loop finishes on a page.
I’ve seen other plugins do it without having to modify theme files, so I know it can be done, but I can’t seem to even get a simple public function to fire.
Sigh!!! Thanks for your help, I’ll get back to you.
You’re using the wrong action. Looking at the actions list, the correct action is
loop_end, notend_loop.OK, guests are gone <grin>
Even if I take your last example, and make the call loop_end, the function is not firing. In my template file I have:
$result = '#'.do_action('loop_end') ; echo $result.'&<br>' ;Perhaps it is because the function returns a value instead of echoing it, and do_action somehow returns something different than the return value of the function.
STILL!!! That’s not the point.
If plugin.php contains the following:
<?php function hereman1() { echo 'Yup, I am Here' ; } add_action( 'loop_end', 'hereman1') ; ?>Then right after the comments there appears a line that says, ‘Yup, I am Here’ because the function fires when WP ends the loop.
So how do I get a plugin written in oop to do the same.
THAT’S the question!
Thanks again for putting up with me.
Ed
I have got no idea why it isn’t working for you. I’ve just tested this code in my own site, and it works correctly:
class MyTest { public function __construct () { add_action ("loop_end", array ($this, "output")); } public function output () { echo "<p>After Loop Notice</p>"; } } $my_test = new MyTest ();That’s nothing different to the code that I posted before, but in this case I can guarantee that it works because I’ve just had it working here.
The topic ‘OOP Plugin – Exposing Class Functions’ is closed to new replies.