Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

David Jarrin
PLUS
David Jarrin
Courses Plus Student 11,182 Points

wordpress plugin using class functions problem

So this is my first time making a plugin using basic OOP principles. My plugin basically looks something like this at the moment:

/*
plugin info
*/
class plugin_name {
      public function __construct() {
            //this is repeated for a whole mess of other functions and I'm not sure what hooks to put them on (they are made up of api calls and basically functions with markup in them)
           add_action('wp_enqueue_scripts', array($this, 'scripts')
      }

     public function mark_up_function1() {
        ..........................
        ..........................
     }
}
$mm_stock_info = new mm_stock_info();

Then on a template page I call the markup functions like so

$mm_stock_info->mark_up_function();

when I try to load the page that is utilizing this plugin the most I get a mostly blank screen (but my css and js is loading onto the page). Does anyone have any suggestions to what my problem may be?

3 Answers

David Jarrin
PLUS
David Jarrin
Courses Plus Student 11,182 Points

This worked...

/*
plugin info
*/
class plugin_name {
      public function __construct() {
            //this is repeated for a whole mess of other functions and I'm not sure what hooks to put them on (they are made up of api calls and basically functions with markup in them)
           $this->register_callbacks() 
      }

     protected function register_callbacks() {
     add_action('mark_up_function1', array($this, 'mark_up_function1'));
     }

     public function mark_up_function1() {
        ..........................
        ..........................
     }
}
$mm_stock_info = new mm_stock_info();

Then in the theme something call the function with the hook you called in the plugin

do_action('mark_up_function1');

Is the function being called correctly? It looks like you've created a function called mark_up_function1 but you're calling a function called mark_up_function.

David Jarrin
David Jarrin
Courses Plus Student 11,182 Points

That was a typo on my part (was trying to write up a quick example) but in the actual code the function is the correct name.

David Jarrin
David Jarrin
Courses Plus Student 11,182 Points

What hooks do you recommend I use for these functions that make an API call and present it in HTML form?

Sorry can't help you there. Haven't really dug in to the plug-ins end of Wordpress yet.