• 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() );
    }
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    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.

    Thread Starter rechazame

    (@rechazame)

    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?

    Thread Starter rechazame

    (@rechazame)

    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?

    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.

    Thread Starter rechazame

    (@rechazame)

    Nice… Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Whento use 'init' and when to use a function in functions.php?’ is closed to new replies.