Tag Archives: PHP

PHP strtotime() Function

It’s always a pain to deal with dates when coding. Most date-oriented functions expect UNIX timestamps, which is hardly a user-friendly format, and at a glance it’s near impossible to do something simple in theory, such as adding three days to a date. Generally the best strategy is to convert all dates into UNIX timestamp format, and then you can reformat it with date() when you want to display it in a human-readable format.

Fairly recently, when I was working on WP125, I came across a very useful function called strtotime(). It’s one of the coolest functions I’ve ever found in PHP. As the name suggests, it can convert a string into an equivalent timestamp.

Continue reading →

Add An Item to WordPress 2.7’s Favorites Menu

WordPress 2.7 introduced a little menu in the upper right corner of the backend. By default it includes commonly-used links, such as the New Post and Comments Management pages.

It’s a neat idea, but the developers didn’t give the average user an easy way to add and remove links from the list. Hopefully a plugin will be developed to allow it to be customized easily. In the meantime, you can use your theme’s functions.php file to add an item to the list. Just use a code snippet similar to this:

function add_favorite_item($actions) {
$actions['options-general.php'] = array('Settings', 'manage_options');
return $actions;
}
add_filter('favorite_actions', '
add_favorite_item');

The favorite_actions hook calls your function when it’s needed, which in turn adds an entry to the array that WordPress uses to construct the menu. You can duplicate the $actions[... line to add more than one item. Just substitute options-general.php with the URL of the page you want (everything after the “wp-admin/”). Then change the 'Settings' to whatever text you want for the menu label. The final argument is the minimum permission for the item to be displayed at.

This hook could also be used in a plugin, if you have a use for it there. Just use it with discretion, so people don’t end up with a long list of not-quite-favorite links added by numerous plugins. ;)

For further reading, see Ozh’s little write-up of the favorite_actions hook.

How To: Integrate Non-WordPress Pages in WordPress

Have you ever put together a cool script or page on your own, and then wanted to integrate it into WordPress so it would have the same look and feel as the rest of your site?

Maybe you have a custom survey you put together with PHP, or a web directory, a Job Board, or some other thing that you can’t really do inside WordPress.

Here’s a bit of code that will help integrate your script with your theme, and will also make other internal WordPress functions available to your script:

Continue reading →

Prelovac’s WordPress Optimization Guide

Vladimir Prelovac has an excellent article on optimizing WordPress for speed available now. It covers plenty of bases, such as

  • Caching using WP Super Cache
  • MySQL Caching
  • Checking plugins for slowdowns
  • Optimizing tables

If you ever experienced slow WordPress admin panel, “MySQL server has gone away” message, pages taking forever to load or you want to prepare your site for a major increase in traffic (for example Digg front page) this is the guide for you.

It’s not an all-encompasing guide, of course. There are plenty of little tweaks you can do that can shave precious milliseconds off your loading times, such as cutting out unnecessary template tags, such as bloginfo() tags, and replacing them with the values they output.

And of course there are plenty of other things to tweak. PHP, Apache, and MySQL can all be configured carefully for maximum speed, assuming you have root access to your server. You can optimize your templates to decrease loading and rendering times. Yahoo has a few tips.

Performance optimization is always a good idea, especially if your blog is on a shared hosting account, or if you have a very high traffic blog (I’m talking to you, TechCrunch and Mashable!).

WordPress Post Thumbnails

Thumbnails can really liven up an otherwise plain list of posts, and add a bit of visual interest. By associating thumbnails with posts you gain a lot of flexibility that you would not have if you just relied on images in post excerpts. This allows you to have more compact listings in archives, yet be a little more visual. (The only disadvantage is you can’t show the thumbnails in WordPress RSS feeds.)

First, a few examples of good use of post thumbnails:

Continue reading →

Easy PHP Reflections

Looking to create that cool reflection effect on an image? There are plenty of JavaScript solutions, such as Reflection.js, to handle the effect on the client side, but what if you want to do it on the server end, to ensure maximum compatibility or to take the load off the client machine?

Enter Easy Reflections, a PHP script that makes does just that. Available in PNG and JPEG/GIF flavors, it makes it wicked easy to generate reflected images. Just use an image tag like so:

<img src="reflect_v3.php?img=picture.jpg" />

There are extra parameters you can optionally pass, allowing you to alter the height and width of the resulting image, and adjust the size and tint of the reflection.

Don’t worry about putting too much stress on the server, there’s optional caching support.

Use WP125 as an Ad Rotator

I’ve received a few emails now asking for ad rotation features in WP125. Now, that’s not something I want to build into the plugin’s core files. Only a few people want it, and I don’t want to make the code needlessly bloated over something that doesn’t fit the ordinary uses and intention of the plugin.

After telling most of those people something along the lines “sorry, unsupported,” I came up with a quick solution (that one emailer was lucky enough to get). This solution doesn’t modify the plugin code, and seems to work pretty well. I figured I’d share it here, in case there’s anyone else who wants it.

Basically, if you want to have a rotation instead of a “normal” block of 125×125 ads, you just need to make use of the handy <?php wp125_single_ad(num); ?> template tag I created for “unforseen circumstances” when I wrote WP125. Just put this code where you want your rotation to appear:

<?php wp125_single_ad(rand(1,6)); ?>

Just replace the “6” with the number of ad slots you have.

Now, fill your ad slots with banners and you’re done.

Learning MySQL [Book Review]

Almost anyone who’s played around with PHP before has run into the subject of databases. MySQL databases are the most common method of storing massive amounts of data to later be sorted through and retrieved for display via script. WordPress stores all of it’s posts and settings in MySQL tables, forum scripts are powered by databases, sites like Amazon, YouTube, and of course IMDB make extensive use of databases. If you think about it, most modern websites are just pretty user interfaces for databases.

As common as they may be, databases sure are mysterious critters. It look me awhile to grasp the concept of them at first, and even longer for me to pick-up the skills required to make use of them. In addition to knowing a scripting language like PHP or Perl, and how to submit a query to the DB server, you also need to know the SQL language.

I just finished a great book on MySQL. Learning MySQL by Seyed M.M. Tahaghoghi and Hugh Williams is a comprehensive and well-explained book that teaches you from the ground up about MySQL databases and how to work with them. It introduces the concept of a database, walks you through installing the MySQL server software (if you’re not already running it), explains querying, then moves on to the real meat of the book: Structured Query Language, or SQL. It covers basic SELECTS and INSERTs, JOINS, nested queries, table and column types, and really everything you need to know to get started.

Following chapters cover topics like using PHP or Perl to interact with databases (as opposed to using a MySQL prompt) and securing web applications. There are also sections on planning database structures optimally, optimizing performance.

The book is written in a manner that should make it easy to follow, it’s full of code examples to try out, and overall is enough to give you a general to intermediate knowledge of MySQL. It also works as a handy reference.

Want Traffic? Have PHP/Design Skills?

How would you like to gain a steady stream of quality traffic to your website? Traffic that won’t vanish in a few weeks. Traffic that will keep on coming for a long time.

Write a WordPress Plugin, or if you lean more toward design rather than coding, make a WordPress theme. WordPress resources are the ultimate linkbait. It may take you awhile, but it doesn’t take much work to get the traffic coming. Write a page about the plugin on your blog, submit your theme or plugin to WordPress/Extend, post a quick note to WLTC News, and submit to a few social bookmarking sites. It won’t take long before the plugin spreads through the blogosphere, netting you some incoming traffic, and some good links and mentions on other blogs. I’ve done this twice, with WP125 and GoCodes, and both plugins have really added a helpful boost to Webmaster-Source.

If you Google “WP125,” a good many of the 12,000+ results are related to my plugin, though I have to admit there are some scrapers and non-plugin-related posts in there. “WP125 ad” returns higher quality results, and 4.890 at that. Not bad considering the plugin is only a month and a half old…

Now I know not everyone has the skills to put together a good theme or a useful plugin, and therefore may not be able to make use of this tip at the moment. But don’t give up on this idea yet. Anyone can learn PHP, or pick up blog design. If you’re already blogging with WordPress, chances are you’ll be able to pick up said skills without too much difficulty if you put your mind to it.

Print_r: A Very Useful PHP Function

Have you ever had an array or object returned by a PHP function, but you didn’t know how it was structured? In order to proceed writing your code, you would need to known what’s in the array, and how to access it. This somes up if you use XML-based APIs a lot, or if you’re dealing with WordPress’s $wpdb class.

That’s where the useful print_r function comes in.

Print_r is implemented simply by calling it, and passing a variable as an argument. The variable you should pass would be your array or object. Also, you need to surround it in <pre> tags (unless you want a jumbled mess that will be hard to read).

echo '<pre>';
print_r($the_variable);
echo '</pre>';

Simple enough, and insanely useful.

Page 10 of 12789101112