

Maruf scribbled this post.
Back in the day I knew extremely little about WordPress, and only a moderate amount of PHP, they were the times when I was most lethal. I actually used to modify the core wp-files to get the output I wanted. Consequently, when it came to upgrading wordpress versions it was a nightmare. I had to remember all the core files I updated, and then apply the hacked code into the new version.
As always, in retrospect, I now realise what a fool I was. Firstly, you should NEVER modify core wp-files unless you really feel you need to. Not only because upgrading becomes a pain, but you also run the risk of completely breaking your entire blog. For the most part, you can handle wordpress manipulation through either the wordpress admin section, through plugins, or by writing your own PHP code in the while loops on the theme pages.
Today I revisited one of the very first WordPress themes I had worked on- it was quite amusing to see how I had hacked apart code that didn’t really need to be hacked.
The scenario
For every article that was written for this particular blog, it always started with an image. For example, if the blog post is about a Porsche, they’ll be a picture of a Porsche to start the post off. That same image was required to appear next to each article on the results pages (e.g. index.php and the category pages). To get that output I started modifying the the_content() function, which is located in the post-template.php file. I started adding my own str_replace and preg_match functions. It was pretty crazy. I got the result I wanted, but it was a pretty dirty method. In fact, it was down right filthy.
Here’s an example of a post, with the image starting the entry:

Here’s an example of the index page, which shows all the recent entries. The images used in the posts are next to each article:

I modified the_content() to get the output:
1 2 3 4 5 6 7 8 9 10 11 | function the_content2($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(substr($content, 0, 400), '<img>'); if (preg_match("/<img/", $content)) { $pieces = explode(">", $content); ...etc |
Frightening, right?
The Smarter Method Of Manipulating WordPress Code
I upgraded the WordPress version for the first time in years for that blog, and naturally the entire blog broke because I had overwritten the core files which I had edited. But that was cool, it was all part of the master plan.
These days i’m a lot wiser *touches wood*, I know never to modify the core files, and to only mess with the theme files. So this time I pulled out all the data I needed from the while loop in the actual theme file.
I used the following PHP code to pull out the content. I couldn’t use the_content or the_excerpt class because I can’t manipulate classes in the theme files.
1 2 3 4 5 6 7 8 9 | <?php while (have_posts()) : the_post(); ?> <? $content = explode(">", $post->post_content); $content = str_replace(' width="300"/','class="content"', $content); echo "$content[1]>"; ?> <?php endwhile; ?> |
Now, that’s so much cleaner….and safer. Now when the blog is upgraded, the script won’t break unless variable names have changed. But that rarely happens, and if it does, it’s only a matter of finding out the new variable names.
So what have you learned today?
1)NEVER modify core wp-files
2)$post->post_content is the variable you need to output the content of a post in a while loop










comments
Need to make menu look good in two decks. How do I do that? I am using Fresh Editorial 2.1.
Thank you.
http://www.newberrycheer.com
I know just enough to be dangerous.
k