[wp-hackers] anonymous functions for do_action and apply_filters

Jeremy Clarke jer at simianuprising.com
Tue Dec 28 01:50:38 UTC 2010


Relevant to this discussion are the adorable and useful __return_* functions
that were added in 3.0. They give you standardized ways of disabling hooks
that just need a false or zero returned without creating a new function.

Efficient and hilarious, they also offer a scalable and elegant model for
situations they don't account for. Just generalize your special needs as
much as possible and create you're own __return_* functions.

Here they are from wp-includes/functions.php

/**
 * Returns true
 *
 * Useful for returning true to filters easily
 *
 * @since 3.0.0
 * @see __return_false()
 * @return bool true
 */
function __return_true() {
    return true;
}

/**
 * Returns false
 *
 * Useful for returning false to filters easily
 *
 * @since 3.0.0
 * @see __return_true()
 * @return bool false
 */
function __return_false() {
    return false;
}

/**
 * Returns 0
 *
 * Useful for returning 0 to filters easily
 *
 * @since 3.0.0
 * @see __return_zero()
 * @return int 0
 */
function __return_zero() {
    return 0;
}

/**
 * Returns an empty array
 *
 * Useful for returning an empty array to filters easily
 *
 * @since 3.0.0
 * @see __return_zero()
 * @return array Empty array
 */
function __return_empty_array() {
    return array();
}

-- 
Jeremy Clarke • jeremyclarke.org
Code and Design • globalvoicesonline.org


More information about the wp-hackers mailing list