WordPress.org

Ready to get started?Download WordPress

Forums

Best Practice for Conditions (2 posts)

  1. Abhik
    Member
    Posted 1 year ago #

    What is the best practice to implement conditions? Within callback or wrapping the hook?

    Like,

    function myfunction() {
    	if ( get_option() ) {
    		// Do Something
    	}
    }
    add_action( 'hook', 'myfunction' );

    Or,

    function myfunction() {
    	// Do Something
    }
    if ( get_option() ) {
    	add_action( 'hook', 'myfunction' );
    }
  2. designpromote
    Member
    Posted 1 year ago #

    The both are the same in my view. However, if myfunction() is called quite often, the second one provides a better performance. As stated for add_filter() (http://codex.wordpress.org/Function_Reference/add_filter), there is no test that the function exists nor whether the $function_to_add is even a string. It is up to you to take care and this is done for optimization purposes, so everything is as quick as possible. .

Topic Closed

This topic has been closed to new replies.

About this Topic