WordPress codex doesn’t know it all: get_ probably exists
There is (often) a get_<echo_or_print_function> even if the WordPress Codex doesn’t say so (or something really similar).
Situation:
You want to style the next_posts_link() and previous_post_links() in such a way that requires a container/wrapper div. Unfortunately two functions echo (display) their output so you cannot perform a conditional operation on them in PHP. So your container div (and it’s styling) appear regardless of whether there are more posts or not.
WordPress functions such as blog_info() do the same thing but also offer a “get_” version which returns the value instead e.g. get_blog_info().
You check the WordPress Codex for get_next_posts_link() but it does not seem to exist.
Solution:
Try get_next_posts_link() anyway because it does exist.
In reality if there is a function that ONLY echoes or prints it’s value it probably always maybe does. In fact after further review of the WordPress source files if a function echoes its output then it probably always maybe calling the get_version itself (found in wp-includes/link-template.php):
/**
* Display the next posts pages link.
*
* @since 0.71
* @uses get_next_posts_link()
*
* @param string $label Content for link text.
* @param int $max_page Optional. Max pages.
*/
function next_posts_link( $label = 'Next Page »', $max_page = 0 ) {
echo get_next_posts_link( $label, $max_page );
}
Despite the fact that the Codex says many pages are incomplete and that many existing links to function references 404 I still treat the Codex like gospel. It’s a shame that the documentation isn’t more thorough, but I guess that is where we are supposed to be giving back to the community. I’m posting this here as a general tip and mainly for a reminder for myself that Codex is far from complete and that a quick search of the WordPress source can do wonders for the health and cleanliness of your own WordPress PHP code.
In some cases there is not an exact “get_function” that will work. For next/previous post links, next_post_link() and previous_post_link() you actually need get_next_post() and get_previous_post() all of which use adjacent_post_link() to return the values echoed or not.
In my case the problem came up while making an older/newer posts widget that allowed for CSS buttons to be cleanly displayed in the widgetized sidebar. I spent 15 minutes trying to figure out a workaround (after I checked the Codex) before remembering the “get_” functions. I blame the heat. The atypical cool weather here in San Diego has ended and the much more realistic July heat has returned and my brain is turning off.
UPDATE: While I don’t have the time to properly add the new pages to the Codex I at least made a reference link to the get_ versions of these functions so that others will know they at least exist. next_posts_link() and previous_post_links() now provide mention and links to get_next_posts_link() and get_previous_posts_link().


4 Responses to WordPress codex doesn’t know it all: get_ probably exists
Thanks for the interesting post! May I ask where you get your sources from?
yours
Antwan Deniken
My only source for this is the Codex and the actual WordPress source code files.
Unless a WP function has an argument to “echo” or not (and other exceptions that WP seems riddled with for very good reasons I am sure), a function called display_this() is quite possibly simply “echoing” get_display_this().
Brilliant! Thanks so much for figuring this out.
There are a lot of convoluted solutions out there – it’s great to know it can be this straightforward (and that I can check for “get_ probably exists” for other functions as well). Many thanks!
It sounds kind of absurd saying I offered a non-convoluted solution (guess, trial and error, search source code) but hopefully it helps. Take care Alex Miles Younger. Awesome name by the way.