Wrox Programmer Forums
BOOK: Professional WordPress Plugin Development
This is the forum to discuss the Wrox book Professional WordPress Plugin Development by Brad Williams, Ozh Richard, Justin Tadlock; ISBN: 978-0-470-91622-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional WordPress Plugin Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 7th, 2011, 03:28 PM
Registered User
Join Date: May 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question Functions: Call Before Defintiion?

I noticed in the example on page 33 (boj-random-blog-posts.php below) that the function boj_randomly_order_blog_posts is called BEFORE it is defined. The function is defined immediately after the line that invokes it in the add_action hook. I am new to PHP (obviously ), but I do know that in other languages the function must be defined before they are called.

Is this allowed in PHP? If so, what is the best practice regarding this? Is it better to go one way or the other?

Example from book:


add_action( 'pre_get_posts', 'boj_randomly_order_blog_posts' );

function boj_randomly_order_blog_posts( $query ) {

if ( $query->is_home && empty( $query->query_vars['suppress_filters'] ) )
$query->set( 'orderby', 'rand' );

Old June 15th, 2011, 08:42 AM
Ozh Ozh is offline
Registered User
Join Date: Jun 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

The example you've copied from the book is best practice and the function is not "called" before it is defined.

First of all, there's nothing wrong in PHP to call a function "before" it is defined. For instance, this works fine:
function myfunc_do_something() {
echo "I'm doing stuff";
The PHP engine first reads and compile files, then execute them.

In the book example, the custom function boj_randomly_order_blog_posts() is not "called": it's associated with an action hook. Basically, it tells WordPress: "hey, when the event 'pre_get_posts' occurs, please execute my function 'function boj_randomly_order_blog_posts', thanks buddy!".

Note also that if you hook a nonexistant function, or a nonexistant hook, no error occurs. In other words:
add_action( 'pre_get_posts', 'xwkwmklm' );
add_action( 'xklmkxmxklm', 'wxcklwmklm' );
add_action( 'ccwxklm', 'a_real_function_that_exists' );
won't break anything, which is nice to know: hooking to a deprecated hook (removed in a future WP version) or relying on a function that's missing (removed in WP or used by a plugin that got deactivated) won't do the expected action, but it won't break your blog with a fatal error.

Finally, I've said in my reply introduction that this is best practice: in WordPress, be sure not to execute functions directly from the plugin, but always hook them into an action. Refer to "Do not check too early" page 119 for more.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto call VBA functions using Java kotaiah Java Basics 2 June 20th, 2007 10:13 AM
Dynamically call javascript functions austinf Javascript 1 May 13th, 2006 07:31 AM
call functions of dll Digon Classic ASP Basics 1 December 13th, 2005 06:12 AM
How to call C# COM overloaded functions from ASP? khaledriyal C# 0 October 11th, 2005 03:13 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.