WordPress-Christmas-2010-23

Separate Logic From Output

Once HTML is defined within a function for output, the HTML will be separated from the logic! In this case the function is defined twice. A function contains only logic and values ​​are returned only as return. The second function will contain HTML, test logic, loops or hooks and outputs values ​​as echo. Ok, the smart asses will say MVC, yeah you right, but WordPress does not pursue a Consistent MVC pattern!

Why WordPress doesn't follow MVC pattern consistently?

If we have a close look on WordPress, we can detect an MVC pattern. By separating the Themes (view) from the core (model) it gives the impression WordPress pursues an MVC pattern. But this patternn is found only in the frontend. Let's take a closer look and we see that there is no such logic for the admin area. So, the view for the admin area has to be anchored somewhere in the core. We can also notice that in the core itself are lose SQL queries within PHP. Well, a general separation of view and logic is not equal MVC. An MVC-Pattern is principally composed of a sort of object-oriented programming paradigm, in WordPress, these can also be found occasionally, but we find much more procedural than object-oriented programming paradigms. Conclusion WordPress itself does not pursue a MVC pattern, but it is quite possible MVC principles, as we used in the following.

function get_some_foo() {
	$my_foo = 'This is Foo!';
	return $my_foo;
}
 
function some_foo() {
	$foo = get_some_foo();
	echo $foo;
}

Why is this useful? We want the function some_foo() returns a predefined output. In another template we want to change the default behavior of the function some_foo(). In order to effectively reflect both requirements, two functions are defined. The first function consists only the elementary logic and is only returned as return values​​, it is always preceded by a get_.... The second function includes predefined information, which in turn can be HTML, test logic, loops, or a hook.

function get_some_foo() {
	$foo_bar_array = array( 'Foo', 'Bar', 'FooBar', 'BarFoo' );
	$foo_bar_array = apply_filters( 'some_foo_array', $foo_bar_array );

	return $foo_bar_array ;
}
 
function some_foo() {
    $foo_list = '<ul>'; 
        
    foreach( get_some_foo() as $foo_key => $foo_value ){
		$foo_list .= '<li>' . $foo_value . '</li>';        
    }
 
    $foo_list .= '</ul>';
	
    echo apply_filters( 'some_foo', $foo_list );
}

In the previous example in each of the two functions with the function apply_filters() the possibility will be created, to change returned values later as you desired. A manipulation can take place via the so-defined hooks some_foo_array or some_foo. Here is an example of how a possible manipulation can look with the two defined hooks.

In the next code example, we change an array $foo_bar_array and the HTML output. This involves the two hooks some_foo_array and some_foo, which were set with apply_filters('some_foo' .... With the function add_filter() filter functions for further processing in the global variable $wp_filter will be registered.

function get_my_some_foo( $foo_bar_array ) {
	$foo_bar_array[ count( $foo_bar_array ) + 1 ] = 'FuuBoo';
	
	return $foo_bar_array ;
}
 
function my_some_foo( $foo_list ) {
    $foo_list = str_replace( 'ul', 'ol', $foo_list ); 
	
    return $foo_list;
}
 
add_filter( 'some_foo_array', 'get_my_some_foo');
add_filter( 'some_foo', 'my_some_foo');
 
// do not forget, call some_foo()
some_foo();

Let's look briefly what's happening. What outputs some_foo() has without manipulation we see in the following example.

function get_some_foo() {
	$foo_bar_array = array( 'Foo', 'Bar', 'FooBar', 'BarFoo' );
	$foo_bar_array = apply_filters( 'some_foo_array', $foo_bar_array );
	
	return $foo_bar_array ;
}
 
function some_foo() {
    $foo_list = '<ul>'; 
        
    foreach(get_some_foo() as $foo_key => $foo_value){
		$foo_list .= '<li>' . $foo_value . '</li>';        
    }
 
    $foo_list .= '</ul>';
	
    echo apply_filters( 'some_foo', $foo_list );
}
?> 
 
// output
<ul>
	<li>Foo</li>
	<li>Bar</li>
	<li>FooBar</li>
	<li>BarFoo</li>
</ul>

In the next example we use the functions defined in the hooks some_foo_array and some_foo to add the array another element to make the unordered list, an ordered list.

function get_my_some_foo( $foo_bar_array ) {
	$foo_bar_array[ count( $foo_bar_array ) + 1 ] = 'FuuBoo';
	
	return $foo_bar_array ;
}
 
function my_some_foo( $foo_list ) {
    $foo_list = str_replace( 'ul', 'ol', $foo_list ); 
	
    return $foo_list;
}
 
// register Hooks in the stack ($wp_filter)
add_filter( 'some_foo_array', 'get_my_some_foo');
add_filter( 'some_foo', 'my_some_foo');
?>
 
//output
<ol>
	<li>Foo</li>
	<li>Bar</li>
	<li>FooBar</li>
	<li>BarFoo</li>
	<li>FuuBoo</li>
</ol>

How does add_filter and apply_filters work in the program flow?
Each hook which should be executed is registered with add_filter or add_action in the global variable $wp_filter. If the function apply_filters() is called, an appropriate filter function based on the transmitted variables will be searched in the $wp_filter, if this is registered with add_filter, the appropriate function will execute. Now values will be manipulated now and then passed back to apply_filters.

Let's take our example from above, it looks like this.

<?php
// the default function
function get_some_foo() {
	$foo_bar_array = array( 'Foo', 'Bar', 'FooBar', 'BarFoo' );
	$foo_bar_array = apply_filters( 'some_foo_array', $foo_bar_array );
 
	return $foo_bar_array ;
}
 
// the filter function
function get_my_some_foo( $foo_bar_array ) {
	$foo_bar_array[ count( $foo_bar_array ) + 1 ] = 'FuuBoo';
 
	return $foo_bar_array ;
}
 
// register function in $wp_filter
add_filter( 'some_foo_array', 'get_my_some_foo' );
?> 

Without hook functionality the whole thing would look like the following. Although we have much less code, but this is not reusable.

<?php
//Das ist unsere default Funktion
function get_some_foo() {
	$foo_bar_array = array( 'Foo', 'Bar', 'FooBar', 'BarFoo' );
	$foo_bar_array[ count( $foo_bar_array ) + 1 ] = 'FuuBoo';
  
	return $foo_bar_array ;
}
?>

Guest Post

Rene Reimann AvatarRené lives in Halle at Saale, he is married, has a daughter (8) and a dog (12). He is a professional media designer for digital and print media, with a flair for design and color combinations. He is a creative WordPress developer, designer and media consultant who always has a suitable solution. René is an instructor and member of the Examination Board of the Chamber of Halle/Dessau for the education of media designer for digital and print media.

Comments are closed.

2 comments

  1. Sebastian

    Hi René. I found this tutorial to be very useful in helping me understand the mechanics behind WordPress hooks and filters. Thank you for taking the time to write it!

One pingback

  1. Adventskalender vom 23. Dezember « F-LOG-GE