First page of the Functions archive.

Just __return_* It

Posted by The Doctor on Apr 4, 2012 with No Comments | Short Link
in Functions, Tips
as , , , , ,

WordPress has a lot of helper functions built into its core. Some of these functions are obvious and some are obscure … and almost every one of them can be easily put to use.

A chat group I was in the other day lead me to looking for a set of these helper functions in the code we were discussing … I didn’t find them. Well, I did find them in use but I did not find where they were defined. Next stop, the WordPress core … and there they were.

Now, as the title may suggest I am referring to the following WordPress core functions:

  • __return_true() – returns the Boolean state of true
  • __return_false() – returns the Boolean state of false
  • __return_zero() – returns a value of zero (0)
  • __return_empty_array() – returns an empty array, as in array()
  • __return_null() – returns null (or void)

All of these functions have a very similar structure and were implemented at WordPress version 3.0.0, with the exception of `__return_null` which will be introduced at WordPress version 3.4 per their definitions in ‘../wp-includes/functions.php‘. For example, here is the code for `__return_true`:

function __return_true() {
	return true;
}

Why are these great helper functions? Simple really … they provide easy to remember, and very useful, callback functions for filters. You should not need to write your own return functions now, just __return_* it instead.

Remove Comment Form Website Section

Posted by The Doctor on Jan 17, 2012 with 1 Comment | Short Link
in Functions, Tips
as , , ,

From time to time an interesting Five-Minute-FixTM comes along that sparks a great reason to add another WordPress Tips post. The idea for this tip started with this comment at BuyNowShop.com:

… how to remove the website section when leaving a reply/comment …

The solution may be obvious to some and can be readily derived from the information found on the WordPress codex page for the `comment_form` function, but if you are new to WordPress and/or not familiar with writing a function to use with `add_filter`, here is a very simple and working snippet to use in your theme:

To see the tip click here.To hide the tip click here.
function wpfa_remove_comment_website_section(){
    $commenter = wp_get_current_commenter();
    $req = get_option( 'require_name_email' );
    $aria_req = ( $req ? " aria-required='true'" : '' );
    $fields =  array(
        'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
                    '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
        'email'  => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
                    '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
        'url'    => '' /** removes website section */,
    );
    return $fields;
}
add_filter( 'comment_form_default_fields', 'wpfa_remove_comment_website_section' );

Ideally, this will be placed in the ‘functions.php’ file of your theme, but better yet this should be included in the Child-Theme you created of the theme you want to modify.

Do Not Display Comments HTML Tags

Posted by The Doctor on Aug 7, 2011 with 18 Comments | Short Link
in Functions
as , ,

A standard function found in many themes is the comment_form. This is a core function of WordPress that produces a standard comment form generally consisting of text fields for the name and email address and a textarea for the actual comment. You will also find there are some standard text outputs produced by this function [...]

the_widget() as a Shortcode Base

Posted by The Doctor on Sep 21, 2010 with No Comments | Short Link
in Functions
as , , ,

This post will further expand on the possible uses of the_widget() as the basis of the code you can use to create a shortcode for a WordPress sidebar widget.

An Implementation of wp_nav_menu

Posted by The Doctor on Sep 12, 2010 with 3 Comments | Short Link
Last modified by The Doctor on December 9, 2010
in Functions
as , ,

This article will show how to implement the wp_nav_menu() function using the wp_list_pages() function as a fallback.

Add the_shortlink()

Posted by The Doctor on Jul 22, 2010 with No Comments | Short Link
Last modified by The Doctor on September 9, 2010
in Functions
as ,

I believe there are many reasons for themes to use this function or add it to your WordPress web site. I will briefly discuss a couple of the ones that come to mind. Note, if you are using the default permalink structure this may not be for you as the_shortlink() creates a URL that looks [...]

Upgrade wp_list_categories() to wp_nav_menu()

Posted by The Doctor on Jul 20, 2010 with 6 Comments | Short Link
in Functions
as , , ,

In this third installment in the upgrade to wp_nav_menu() series of posts, I will be dealing with moving from wp_list_categories() to wp_nav_menu(). Let’s start with the same basic outline and list the default options for each function. We will start with wp_nav_menu() 1 first: We follow with wp_list_categories() 2, 3 which is often wrapped in [...]

Upgrade wp_list_pages() to wp_nav_menu()

Posted by The Doctor on Jul 16, 2010 with 1 Comment | Short Link
in Functions
as , , ,

A few options to address and with possibly a minor edit to the theme, you can upgrade wp_list_pages() easily to wp_nav_menu() in your theme.

Upgrade wp_page_menu() to wp_nav_menu()

Posted by The Doctor on Jul 12, 2010 with 11 Comments | Short Link
Last modified by The Doctor on July 25, 2010
in Functions
as , , ,

Upgrading from wp_page_menu() to wp_nav_menu() may be as easy as a simple find and replace operation.

Modified Post Function

Posted by The Doctor on Mar 18, 2010 with 1 Comment | Short Link
in Functions
as , , ,

A simple function to show who modified a post last and when in WordPress versions 3.0 and greater.