Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I noticed a lot of developers of WordPress themes would use this in functions.php

if (function_exists('register_nav_menus')) {
    register_nav_menus (array('primary' => 'Header Navigation'));
}

For me, I alwayse use add_action for every function I am using in functions.php

So, the above would be written as:

add_action('init', 'my_register_nav_menus');

function my_register_nav_menus() {
    register_nav_menus (array('primary' => 'Header Navigation'));
}

I have two questions:

  1. Why use the if statement in the first method?

  2. Which one is the correct way?

share|improve this question
add comment

3 Answers

The if function_exists approach allows for a child theme to override the function definition by simply defining the function themselves. Since child theme's functions.php files load first, then they will define the function first and the parent's definition will not get loaded.

The other approach of using action or filter hooks works just as well, and child themes can override these by removing the relevant actions after they are added, using the after_setup_theme action hook.

share|improve this answer
add comment

Checking to see if built in WordPress functions exist before calling them is for backward compatibility which IMHO is not needed.

So if you see if ( function_exists( 'register_nav_menus' ) ) the theme author is supporting versions earlier than 3.0.

You still sometimes see if ( function_exists( 'dynamic_sidebar' ) ) Why? I couldn't tell you because dynamic_sidebar was introduced in 2.2.

Another reason to use it is to make your theme or plugin pluggable. A pluggable function is one that can be overridden in a child theme or another plugin.

This is done on the definition not the call and you use the ! operator to make sure it doesn't already exist before you define it.

if ( ! function_exists( 'my_awesome_function' ) ) {
/**
 * My Awesome function is awesome
 *
 * @param array $args
 * @return array
 */
function my_awesome_function( $args ) {
  //function stuff
  return array();
  }
}

When this is done a child theme or other plugin can override that function with there own.

share|improve this answer
add comment

The function_exists function is not an other way to load the function like add_action its for check your code to see that there are no other function with that same name so it will not break your code. From php.net:

Checks the list of defined functions, both built-in (internal) and user-defined, for function_name.

If you have the same function twice in your code it will break, thats why you prefix Your function with something else than wp_.

Read more: http://php.net/manual/en/function.function-exists.php

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.