WordPress.org

Ready to get started?Download WordPress

Forums

Whento use 'init' and when to use a function in functions.php? (6 posts)

  1. rechazame
    Member
    Posted 2 months ago #

    Let's say I want to disable users from viewing the dashboard.

    I can add the following to functions.php:

    function hide_dashboard_init() {
    	if ( is_admin() && !current_user_can( 'manage_options' ) ) {
    		wp_redirect( home_url() );
    		exit;
    	}
    }
    add_action( 'init', 'hide_dashboard_init' );

    But why not just add a regular function to functions.php? Something like that:

    function hide_dashboard() {
    if ( is_admin() && !current_user_can( 'manage_options' ) ) {
    wp_redirect( home_url() );
    }
    }

  2. bcworkz
    Member
    Posted 1 month ago #

    Calling code in functions.php without hooking a filter or action can lead to difficult to debug errors. functions.php code runs when the theme is being loaded and WP is not yet in a stable state. By hooking 'init', you ensure WP is in a known state when you execute your code.

    In particular for your example, when the theme is loaded, the current user has not yet been properly established, the code without hooking 'init' can behave erratically, working when it shouldn't or not working when you think it should.

    Unless you are very familiar with what's happening behind the scenes when WP is loading, it is safest to hook 'init' or similar to be sure everything is as it should be.

  3. rechazame
    Member
    Posted 1 month ago #

    I understand from this that I am better off hooking all of the functions I run from functions.php with the appropriate hooks, right?

    If so, the way to go is to look for a hook index or something and append the appropriate code to the functions?

    And lastly, you mentioned "Unless you are very familiar with what's happening behind the scenes when WP is loading". Where can I find a quality walk-through or guide regarding the whole process you talked about?

  4. rechazame
    Member
    Posted 1 month ago #

    Okay so after taking a look in my functions.php and reading about hooks of actions and filter, I adapted a better understanding in the matter.

    Please help me verify the following:

    functions.php typically contains functions, not calls to functions. When indeed a call to a function is needed, like the example of hiding the dashboard I described above, it is better be hooked to a known action of WP in order to have it called at a known state of the WP core page build-up process. Also, as a result of what said, there should be no situation whatsoever where a function is being called directly from functions.php.

    Did I got it right?

  5. Steven Jones
    Member
    Posted 1 month ago #

    As bcworz eluded to - unless you're hooking on to an action then you can't be sure that conditions such as is_admin() have been set, and therefore your function might not work.

    In addition to that functions.php is just a method available to add functionality to your theme. What you're doing is adding a function to redirect users away from the dashboard. However, if you changed themes then you'd lose this functionality unless you copied the code across to the new theme (which you don't really want to be doing). Therefore this is plugin territory.

    Lastly, it's always best to check for the most appropriate action to hook on to. 'init' runs on every page request in the front end and the backend - therefore you should be using 'admin_init' for an admin function like this so it does not run in the front end.

  6. rechazame
    Member
    Posted 1 month ago #

    Nice... Thanks.

Reply

You must log in to post.

About this Topic

Tags

No tags yet.