twitter facebook dribbble email

WordPress Functions

Careful! This post is looking a little old and could be inaccurate in many, many ways

Over the past few months blogs have accounted for an increasing percentage of my workload with many clients requiring a blog as well as their main site. In most cases we would provide a simple wordpress blog located on a sub-domain and add in any extra plug-ins necessary, keeping the workload to a minimum.

Sometimes to make a plug-in or a design work requires a bit of digging around WordPress’ own code. At times I have needed to check a WordPress functions output, usually to determine if there is any output. With this I can then show any code that is necessary only when the function returns a value. It is while doing this that I have come across a problem.

It seems with most WordPress functions you can not check if it returns a value, instead the function just outputs it’s value (if it has one) and ignores any code of mine. This is because most functions use echo rather than return. For those not too sure of the difference:

Echo: An echo will output a value directly to the screen when the browser executes the code.

Return: The return stores a value as an output of the function to be output to the screen elsewhere in the code.

With the return you are able to do more with a function as you can store it to a variable, interrogate the value of the function or echo the function. You can also directly output the function using <?=the_function();?> saving the need to use an echo.

Luckily WordPress is open source so you can change this if you wish but navigating through WordPress’ maze of functions can be quite daunting and making changes can be tricky for most. Even if you are brave enough to edit the WordPress code you must ensure that you keep track of any code you change as upon upgrading wordpress your changes will be lost.

So the question I have is why do the WordPress team feel that echo is a better way of returning a function when it appears so constrictive and so in my novice view has no benefits over using return?