Code – Functions

Recently I was perusing the WordPress support forums in hopes to use some of my newly gained knowledge to help others that are learning just like me. I came across a thread that made me laugh a little bit. It was a practice I’ve never really seen and I think should be avoided; at least in my eyes.

The question was how to display the full content and not just an excerpt of the post. I thought to myself: This sounds pretty easy, let’s give this a whirl, shall we? Read the post and what the person is asking to do in show the full content when a person is viewing a specific category.

Simple. An

if

statement to check what the person is viewing and if it is true then show the full content otherwise don’t.

( is_category( 'videos' ) ? the_content() : the_excerpt();

That simple little line does so much with as few lines of code as possible. It is the ternary form of writing an if else statement. What is pretty much says is: If the category is videos the show the content otherwise show the excerpt. The snippet would be shown within a

category-video.php

file since that is the way the template hierarchy works within WordPress.

Now rather than posting code I gave the person the advice since they seemed to know a fair amount when it comes to programming. I am one of those people who if you give me some guidance I can then find my way. But I guess what he wanted was for somebody to do the work for him.

I think what really made me more compelled to not post the snippet was the way coded the functions. A way I’ve seen before but seems rather redundant and kind of pointless in my opinion. Wrapping a function within a function to test if that function exists is pointless when you know that function does not already exist. That was painful to say/type.

From all my books and random sites I’ve come to read about functions they have made more and more sense the more I use them. Functions are pretty much a simple tool. A way to make programming a little easier. The way I have seen functions being used is much like objects and I see those as multidimensional arrays. Yes, somewhat complex way of looking at it but it makes sense to me.

The thing that really threw me off was how this person was using functions to create HTML markup by escaping from PHP. While I have seen this, it was used in a manner that didn’t really call for it; you’re more likely to bloat a file that way and it’s very possible to get lost in all that escaping.

Writing code should not be complicated. You don’t need to change the wheel in order to get it right. HTML should rarely ever make its way inside of PHP. I believe HTML, from all that I have read, is more for static content and PHP dynamic. A good example would be the following snippet.

As you can see from the example the only things that are supposed to change are the title, content, meta information and of course the comments so why would you wrap that all within one function? I can see why to a certain extent but it just becomes redundant and just may hurt performance in the long run. Not only that it will be painful to resolve any errors if you have several functions doing that every single time.

/rant

Published by

Jose

Born in El Salvador. Loves to read, write, draw, paint, build, test, typography, hike, photography, art, design, sewing, and many other things.