1

I'm trying to get my theme ready for publication on WordPress.org

I got this feedback:

function_exist() it is not preferable 3.0 design

Now I wonder what I'm doing wrong.

In the current structure I have:

functions.php:

/* Load JBST functions on 'after_setup_theme'. */
add_action( 'after_setup_theme', 'jbst_theme_setup' );

if ( ! function_exists( 'jbst_theme_setup' ) ):
function jbst_theme_setup() {

        /* Load custom Skematik header functions. */
    require( get_template_directory() . '/functions/skematik-header-functions.php' );
//more code
}

/functions/skematik-header-functions.php defines the function like:

add_action( 'skematik_header', 'skematik_doc_type', 9 );
function skematik_doc_type() {
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<meta name="viewport" content="width=device-width" />
<?php
}

In this case skematik_doc_type can be removed with remove_action but can't be overwritten, because it is not wrapped in a function_exists(). Will this be the problem?

Do I have to wrap all functions in a function_exists() and add them with add_action? What are the rules for using function_exists() or not?

4
  • use remove_action to remove the default function and then append your own prefix to function name you are adding on hook, to make sure it runs without conflict. so you should use bs_skematik_doc_type as your function name add_action( 'skematik_header', 'bs_skematik_doc_type', 9 );
    – Kumar
    Commented Nov 19, 2013 at 13:22
  • @dot1 thanks for your quick response. Yes, i understood, but should my theme wrap skematik_doc_type in a function_exists too? Commented Nov 19, 2013 at 13:35
  • 4
    As-written, this question is primarily opinion-based. There is no definitively correct answer. Using Pluggable callback functions is a design decision. Note: if this "feedback" came from a Theme Review, can you please link the ticket? I will handle it there. Thanks. Commented Nov 19, 2013 at 13:51
  • 1
    Use function_exists if the function is meant to be replaceable. That decision has to be made by the developer on a case by case basis. Voted to close as "primarily opinion based". There is no general answer to this question.
    – s_ha_dum
    Commented Nov 19, 2013 at 15:00

1 Answer 1

2

Do not use function_exists().

Technically, your approach works. In practice, you create problems in your code without any benefit.

  • Readability. When someone else reads your code (e.g. you, one year later), the first question will be: why don’t you know your own code? The second one is probably: OK, where is that function defined, how can I figure out which one is called?

  • Interoperability. When I want to replace your code, I am bound to your name. skematik_doc_type() is a poor name for a function that spits out doctype. I cannot replace it with skematik_doctype() or a class method.
    Oh, and you cannot rename it either, because other peoples code might rely on that name.

  • Inconsistent behavior.

    • You cannot reuse such a function, because you never know what it might do or return.
    • function_exists() behaves not like defined(), you cannot return from a file if the function returns true, because all other code in that file will be parsed earlier. See the comments in the manual for examples. So you cannot reorganize your file structure later.

Using function_exists() is a design decision as Chip pointed out. But a very bad one. It isn’t forbidden in the theme review guidelines, but it should be. The WordPress core will not introduce more “pluggable” functions for the reasons listed above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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