31.10.2009

Lately a fellow web developer of mine asked me why can’t he manage to save the current time in a MySQL table. The problem was that he didn’t understand how the PHP timing mechanism works. That’s why I decided to write this article.

time() and date()

time()

The time() function receives no parameters and returns the current time measured in the number of seconds since January 1 1970 00:00:00 GMT.
This may sound a bit weird but I find it to be clever.

Because one doesn’t want to display dates and times in PHP as a very long number (e.g. “The current time is 1256702873″), the date() function was created.

date()

The date() function returns a string formatted according to the given format parameter.

The function is documented as follows:
string date ( string $format [, int $timestamp ] )
While I’d prefer to think of it as
string date ( string $format , int $timestamp=time())
Because if you don’t pass the second parameter, it uses the time() function instead.

Lets see a couple of examples:
<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone

$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
?>

I don’t want to get into the format parameter, but you can read about it at the PHP manual.

Saving the time in a MySQL Database

The wrong way

My friend was using some sort of a special “date”/”time”/”timestamp”/”datetime” column type because he didn’t know what he was getting from the time() function.
This is not the way to store the result from time(). While it may be useful in certain circumstances, I’d rather save the number of seconds since January 1 1970 00:00:00 GMT for later easier manipulation with PHP.

The right way

All you need to do is use a simple INT or BIGINT column type to store what you get from your time() call and in order to display it, use the date() function, passing it as the second parameter.

A theme consists of many files, this can get very confusing very fast. What files are needed? What files runs when and why? Here are the explanations and tips about WordPress template files.

First things first, what is a template exactly? A template is a PHP file that is a part of a theme. For example, there is a template for displaying the home page, one for displaying pages and another one for displaying posts.

What Are The Existing WordPress Tempaltes?

Here is a list of the existing WordPress templates that can be used in themes:

Here is a very useful image provided by WordPress Codex (click to enlarge):

WordPress Template Hierarchy

Custom Templates

There are two main types of custom WordPress templates.

The first one are templates that WordPress looks for automatically, and if one is found it will be used. What the heck am I talking about?
If you have a category called “News” with the ID of 512 and you want the archive page of that category to have it’s own unique template you can create a new template file called category-512.php in the theme’s folder. And WordPress will use it.

This is the list of custom wordpress templates you can use:

WordPress Custom Template Drop Down

Now for the second type of WordPress custom templates.

Let’s say you have more then one page* you want to be displayed in a different way then the others. How do you do that?
Create a new file called whatever.php at your theme’s folder and inside write whatever you want instead of single.php or index.php (depending what displays the single posts at your theme).
Add this comment to the top of the file:

<?php
/*
Template Name: The Whatever Template Yo
*/
?>

(Of course change The Whatever Template Yo to the new template’s name)

Now edit (or create) the pages which you want to use this template and in the right side pick the desired template from the drop down menu. Just like in the screenshot at the right side.

*If you want to do this for posts rather then pages then you will find the Custom Post Template plugin very useful.

Any questions/corrections? Comments are always welcome.

Google Text Size Change - Before and after
Are you asking yourself “Did Google just change their front search page?”, The answer is yes.
Now officially announced in their blog, The size of the buttons, text, logo and text box has increased at google.com or at any other local google home page.

This change is not browser specific and it has been verified that the change includes but not limited to: Google Chrome, Firefox, Internet Explorer and Safari.
Google says at their blog post that this change was made in order to emphasize their goal as a search engine and to show that their focus is always on search.

When I first saw the change I was convinced that me screwing around with Google Chrome’s preferences or something simillar has caused the text to be bigger until I saw Google’s post at their blog. Then I realized that It does not only happen in my computer, it happens everywhere.
Anyway, now that we are aware of actual reason, I wish you all Happy Googleing!

21.08.2009

YouTube New LogoI have received a request from someone that wants even more from my WordPress Custom Fields Plugin. He has a category called “videos” which has posts with YouTube videos embedded with some plugin. He wants to put the videos thumbnail as the post’s thumbnail.

First, how is the video embedded in the post? It uses the following syntax:
[youtube $video_key]

Now you can embed it like you would normally do.

Second, how do we get the thumbnail itself after we extracted the video key? We build the image’s URL with the following syntax:
http://i2.ytimg.com/vi/$video_key/default.jpg

Now, how do we put all of this together into a plugin that inserts the thumbnails?
You don’t have to because I have already done it for you.

This plugin runs through the posts in your blog and if it finds a YouTube video embedded in a post it sets the thumbnail to be the video’s thumbnail.
That’s it, have fun playing with the configuration.

If you encounter any issues please comment/contact me.

13.08.2009

Eilat Israel Beach View
I’m now writing this post while I really should package my bag instead but never mind :).
Tomorrow in 8 o’clock I will be on a bus on my long way south to Eilat with my friends and we are going to stay there for four days until Sunday. We hired an apartment for those three nights and we split the rent between all of us (We are six).

Mainly I wanna say that I’m not gonna be here for some time so I’ll answer the pile of mails only when I get back. You can still send email just don’t expect to get a replay anytime soon.

How can I publish this post without some useless map from Google Maps:

1000 Downloads on the WordPress Plugin Directory
As you can see in the plugin‘s stats page, It currently has 1050 downloads!

Although the number may be not as big as other plugins (All in On SEO got over 2.5 million downloads), it means a lot to me. Every time you see numbers on the internet you don’t really think about them as you would outside the monitor. When viewing YouTube videos, if I see a video that has less then 5k views most of the times I won’t enter it and try to find another one with millions of views.

But when you think about the actual numbers it’s a huge amount of people sitting on their chair at home or at work, entering YouTube and watching the video. The same goes to plugin downloads, It’s really cool to know that my plugin helped over 300 (?) other people around the world that have their own website.

Update: It how has over 3000 downloads!

WordPress Stats Tab
As I’m sure you have all seen at least one time, each plugin at the wordpress plugin directory has a “stats” page.
The stats page tells you the statistics of the plugin downloads, how many downloads have been today, yesterday, this week and all times. It also displays a cool graph with all the information in it.I don’t know why but I just have this feeling that says I “must” click the stats tab and check the numbers out.

After looking at many stats of many plugins I noticed a similar pattern in the graphs. There is a wave action that looks like this (if you want you can see it for yourself in the popular plugins section):
Plugin Stats Wave

Then I did some research on the dates that the waves began and I realized that those waves are happening every time an update is released.

That’s pretty cool and all but It got me thinking, If every download counts, even the update downloads and every big plugin has 4+ updates that means that the “All Time” download counter is 6 times or more (plus people that download the plugin to check it out and then delete/disable it) the actual number of websites using it.

I just want to let everyone know that the download counter is really far away from the plugin users number/the number of blogs using the plugin. Next time try to say download count instead of plugin downloads.

The new version of Mass Custom Fields Manager is out!
Why do you need it? For example if you have custom fields that old plugins left of your posts and you want to get rid of them you can now do it with a click of a mouse. There are virtually endless occasions where this plugin can save you valuable time and effort.

I added new features that you asked for, fixed some minor bugs and did some code optimization.

Download it now!

If you encounter any bugs or have an idea on how to improve the plugin then I would love to hear it from you.

I hate those small evil graphics

If you want to put an end to those annoying smilies that replace your text in posts & comments, follow the next instructions.

First, what are smilies?

Smileys, also known as “emoticons”, are little faces that help you display various emotions. Text smileys are created by typing two or more punctuation marks like :) and :-(

Here is the full list of smilies in WordPress

icon text text full text icon full text
smile :) :-) :smile: lol :lol:
biggrin :D :-D :grin: redface :oops:
sad :( :-( :sad: cry :cry:
surprised :o :-o :eek: evil :evil:
eek 8O 8-O :shock: twisted :twisted:
confused :? :-? :???: rolleyes :roll:
cool 8) 8-) :cool: exclaim :!:
mad :x :-x :mad: question :?:
razz :P :-P :razz: idea :idea:
neutral :| :-| :neutral: arrow :arrow:
wink ;) ;-) :wink: mrgreen :mrgreen:

How do I get rid of them?

It’s actually pretty easy. All you got to do is go to your admin panel -> Settings -> Writing where you will see the following checkbox:
Convert emoticons like :-) and :-P to graphics on display
Uncheck the check box that says “Convert emoticons like :-) and :-P to graphics on display” just like in the image above and click “Save Changes”.

How to replace them

If you want to keep the smilies but you don’t like the current set of smilies you can change them.
Open your FTP program (I use FlashFXP) and upload (overwrite) your custom smilies to /wp-includes/images/smilies.

Looking for more information? Check out the WordPress codex page on smilies.
What about you, do you like them? I just prefer the plain text, it makes it easier to understand. :P

My No Meta WordPress Plugin finally got his own WordPress dot Org page.
Now I’m waiting for my Mass Custom Fields Manager to get approved.
I always wanted to have my own plugin page :). It just makes me proud to be an owner of an open-source plugin.

Howto get a WordPress plugin page

In order to get a plugin page at WordPress.org (like http://wordpress.org/extend/plugins/plugin-name-here) you need to do a couple of things as described below:

  1. Think about a good idea for a WordPress Plugin.
  2. Develop the plugin yourself / hire a developer to code the plugin for you / contact me and ask me to code the plugin for you.
  3. Register as a member at WordPress.org
  4. Ask WordPress to host your plugin.
  5. Grab yourself some cookies & milk and wait until they send you an email saying they approved your plugin (Usually within one week). If you receive an email saying that they didn’t approve your plugin then… Well I have no idea because I never got one.
  6. The email will say that you will receive access to your SVN within the next hour so this step is just more waiting :(.
  7. Once you got your access, read this tutorial on How to Use Subversion to upload your plugin (=get a plugin page), and upload your plugin files to the /trunk folder. If you encounter any problems with this step like I did (this is the toughest step), contact me through the contact page or by leaving a comment and I’ll get back to you + Update this post with the solution so it helps other people as well.
  8. Wait up to fifteen minutes (WordPress checks for updates every 15 minutes) for WordPress to create your plugin page and then use the WordPress plugin search to find it.
  9. Be pround of yourself and comment here to let me & other people know that this tutorial works.

Frequently Asked Questions

I hope this guide will help some people to get started with WordPress plugin developing, because it’s just amazing what you can do with it. The way that WordPress was built lets you change virtually anything and tweak the system in any way you want.

Next Page »


אורן יומטוב