Support » Plugins » Hacks » PHP: Why use add_action?

  • Hello 🙂
    I’ve been using wordpress for 6-7 years, and I’m starting to get into the “php side” of it.

    I’m currently building a simple theme just to learn some basics.

    My question is the following:

    Why should I use “add_action” instead of simply declaring the things I want to do?
    For example, why write:

    function arphabet_widgets_init() {
    
    	register_sidebar( array(
    		'name'          => 'Home right sidebar',
    		'id'            => 'home_right_1',
    		'before_widget' => '<div>',
    		'after_widget'  => '</div>',
    		'before_title'  => '<h2 class="rounded">',
    		'after_title'   => '</h2>',
    	) );
    
    }
    add_action( 'widgets_init', 'arphabet_widgets_init' );

    instead of just write:

    	register_sidebar( array(
    		'name'          => 'Home right sidebar',
    		'id'            => 'home_right_1',
    		'before_widget' => '<div>',
    		'after_widget'  => '</div>',
    		'before_title'  => '<h2 class="rounded">',
    		'after_title'   => '</h2>',
    	) );
    

    I think I’m missing something very important here 🙂

    Thanks!
    Best

    • This topic was modified 5 years, 7 months ago by chickenrun.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Rep

    YOu need to tell WordPress when to do this. You do this by hooking into the apply_action that occurs when widgets are being initialized. Otherwise, the sidebar is registered after everything else is done, too late to do you any good.

    Thread Starter chickenrun

    (@chickenrun)

    Hi Sterndata, thank you for the explanation.

    Just for the sake of understanding: the code is actually working in both ways (I’m including via an include in the functions.php), why is that?

    Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Rep

    show me your code. use pastebin.com or gist.github.com and put a link here.

    Thread Starter chickenrun

    (@chickenrun)

    Hi Sterndata,
    thanks for your patience 🙂

    I’ve created a gist with the wrong code (which is actually working)
    https://gist.github.com/langhenet/1c8a6a0dd66705084a002ba73dfb99c7

    Moderator bcworkz

    (@bcworkz)

    I think Steve doesn’t mind my jumping in so you get an answer sooner. Your code executes when the theme is loaded. WP is still unstable at this point, but since all of your assignments are static (I mean not dependent on other code, not the formal PHP static declaration), it’s no matter, it runs before it’s needed so all is good — for now.

    If you use the widgets_init action, then your code runs at a different time where it’s appropriate to register widgets when WP is stable and any dynamic content can be reliably established. It’s best to use the proper action even when it’s not technically needed for consistency’s sake. Then next year when you come back and add dynamic content, your code will work without creating mysterious errors that are difficult to track down.

    Thread Starter chickenrun

    (@chickenrun)

    Hi bcworkz, thank you.. everything is clearer now 🙂

    Best

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘PHP: Why use add_action?’ is closed to new replies.