Reclaiming the Front Page of the Androida Wordpress Theme
Users of the Androida theme already know about this. Readers of my blog may have thought I was doing something intentional. Basically, the content in the slider and the 3 “featured categories” area was always getting smooshed up. Line breaks/paragraphs just weren’t being respected.
The Short Version
I ramble. I mean, I REALLY ramble. So, to get the short version, jump right to the last heading in the article.
The Long Version: the Problem
One thing that immediately bothered me a bit about the otherwise spiffy‡ Androida theme is that using the Business layout resulted in front-page content that has all its HTML/XHTML tags stripped out. The implication is that you cannot have paragraphs (<p>), links (<a>), additional images (<img>), or ANYTHING. Just pure pure text.
An example of why this sucked: since rendered HTML allows for line breaks, this means that no matter how many times you hit “enter” in your post, you will never be able to put spaces between your paragraphs. I didn’t quite realize why at first, but I knew that all tags were being stripped out. Not even ugly old <br/> line-breaks were doing it. AAAARGH!! I NEED line breaks for the sake of readability and sanity!
I headed over to the Web2Feel forum to see if anyone else had ever encountered this. Someone HAD, but the forum admin himself thought that the post was just not formatted. I’m not sure if he’s the original coder, but even if he was, there’s no way he could be held responsible for every single plug-in and its idiosyncracies for a FREE theme. As I stated in their forum, it was clear that some sort of “sanitization” was happening. It was just a matter of figuring out where.
The Long Version: Finding The Culprit
No matter how you slice it, the santization was being called from the “meat” of the front page. The front page consists of header.php, layouts/business.php, and footer.php.
I figured it COULD be a script of some sort being called in the header, so I looked there first. No scripts with obvious names jumped out, so I didn’t bother looking any further. A growing trend is to include scripts in the footer instead, but I didn’t think that seemed likely, so I didn’t even bother. I jumped right over to business.php.
Now, anyone who has ever looked into Wordpress code knows that content is retrieved/displayed via a function called the_content(). I figured I’d look at the code surrounding this function and see if anything shook loose in my brain. As it turns out, I found this (actually, a bunch of these, but it only took one):
1 | <?php the_content_limit(750); ?> |
“That’s curious,” thinks I. I thinks, “Wonder if that’s a standard function that comes with Wordpress….” So after I thinks, I Googles for “the_content_limit” (keeping the underscores of course) and soon discover that it’s part of a plug-in. Closer inspection of my plug-ins reveals what I really should have known all along… when I installed Androida, one of the required plug-ins was “limit-post.php”, which as you can guess, is supposed to limit posts.
I look at the above code, and I think, obviously the number being passed to the function represents its length, presumably in characters. What does that have to do with stripped tags?
Only one thing to do… open an editor (I just used the one built into Wordpress) and inspect the Limit Posts plug-in (limit-post.php). The culprit stood out like a sore thumb. See if you can spot the baddie (hehe, I know you will, even if you’ve never touched a line of code in your life):
1 2 3 4 5 6 7 8 9 10 11 | ... function the_content_limit($max_char, $more_link_text = '(more...)', $stripteaser = 0, $more_file = '') { $content = get_the_content($more_link_text, $stripteaser, $more_file); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); $content = strip_tags($content); if (strlen($_GET['p']) > 0) { echo "<p>"; echo $content; ... |
Didja see that? Yup, you did.
The Long Version: The Solution
Here was the offending line inside the Limit Posts plug-in (limit-post.php):
1 |
I didn’t bother tracing through everything that went into creating $content up to that point in time… it was obviously going to be the content (duh!) of the post. And it was now about to be updated by a function called “strip_tags()”. Didn’t take a rocket surgeon to know that something called “strip tags” being applied to something called “$content” was going to be the problem!
Couldn’t have been easier to find the solution. First hit in Google (search term: “php strip_tags”) brought me to this page describing the function: PHP.net strip_tags
In short, if you do not pass an array to the function, it will just strip all tags. But if you pass a non-delimited (I assume the braces automatically delimit) array of permissible tags, you can retain the power of the function while permitting tags that you want to be considered safe. For example, if you want to allow paragraph tags (my biggest one!) and anchor tags (ie. links), you would update the line to look like this:
1 |
Images are still stripped out, which is usually going to be the best idea anyhow. If you are modifying an Androida theme, you will probably also want to add back in the <em> and <strong> tags as a minimum… I would go on to add back any HTML Voila! ‡(yes, I will always put that disclaimer… no matter how much I complain during the next several weeks, I chose this theme because it was cool) Addendum: Turns out I don’t like there to be links on the front page… at least not without being properly styled. Also, header links aren’t stripped out by the function, so they’ll need to be styled in order to keep the front page looking smooth-ish. Alternatively, I could start using Excerpts and include those instead!
Thanks for posting! I found this really helpful!
Cheers,
Rach.
Glad you found it helpful! If you’re using the Androida theme in particular, lots of plans ahead for customizing it further. If you’re using another theme but still found it helpful, what can I say except, “awesome!”
Cheers,
Greg
Nice work fella! Cheers