Articles

Creating A Custom Page Template In Wordpress

There is an easy way to create a custom page for a particular page using Wordpress that doesn't involve adding custom page ID checking code. This might be useful if you want to remove certain aspects of the theme (like a side menu) just for that page. The way this is done is by using the Template Name tag in the following manner.

<?php
/**
 * Template Name: Page Template
 */
?>

Make a copy of your default page template in your theme directory and put this snippet of code at the top. You will now find that when you open the page to edit it there is a section called Page Template. This contains a drop down list of all of the templates available, including the default. You can create multiple page templates by just changing the value of the Template Name tag.

Quickly Run Any PHP Code Through A Form

NOTE: This bit of code is potentially very dangerous and should NOT be uploaded to your web host. I only use this function on my localhost to quickly check that a snippet of code works.

If you want to quickly run some PHP code, and don't want to have to go through creating a file just to see the outcome of a simple calculation is then this snippet might be of some use to you.

It works by taking the contents of the textarea and creating a file with the filename of 'tempcodefile.php', which contains that code. The created file is then included in order to run the code, the output of which is displayed at the top of the screen.

CodeSniffer File Doc Comment And Class Doc Comment

Yesterday I wrote two posts about CodeSniffer and a common warning that I couldn't find any decent documentation on. I thought that today I would go over something that I also had trouble finding out about, this is the file doc error. When you run CodeSniffer on a class you might find the following error being produced.

PHP Version Not Specified Warning In CodeSniffer

In my last post I talked about the PHP CodeSniffer, so today I thought I would solve a common problem that doesn't seem to have any documentation. Whilst correcting a class file I had there was this one warning that just wouldn't go away.

FILE: myclass.php
--------------------------------------------------------------------------------
FOUND 0 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 11 | WARNING | PHP version not specified
--------------------------------------------------------------------------------

There is no documentation (that I could find) on the CodeSniffer site about how to solve this, so I dove into the source files for the CodeSniffer extension and found this line.

Write PHP Standards Compliant Code With PHP CodeSniffer

One of the biggest problems I have found with PHP is the reputation that it gets. This is due to the amount of abuse that the language undergoes by many developers every day. They write code that works, and is efficient, but trying to figure out what it does can take hours as there are no comments, and the code is generally written in any old way. PHP is quite lenient with regards to how code is written.

This is where CodeSniffer comes in. CodeSniffer is a PHP5 script that goes through any PHP (or JavaScript) file that you give it and detects coding violations from a defined set of coding standards. These standards start out with the simple things like using spaces instead of tabs (due to the way in which tabs are displayed differently on different computers) to the more complex things like prefixing private variables with an underscore.

Find The Number Of Days For A Given Month With PHP

There are two ways to find out the number of days for a given month. The first is to use the date() function in conjunction with the mktime() function to create a date and format this value as the number of days in a given month.

$monthDays = date("t",mktime(0, 0, 0, 12, 1, 2008));

The second way is to use the function cal_days_in_month(). This function takes three parameters.

Find And Replace On All Files In And Below A Directory

The following shell command uses the find function to find all files in or below the current directory that have the extension php. It then passes each file found onto a sed command which then replaces all <? with the longer <?php version.

find . -name '*.php' -exec sed -ie 's#<?#<?php#' {} \;

The -name argument in find will look at the base of the file name, that is, the file without any directory path. The -exec command is used to pass each file found onto another command, in this case sed is used.

PHP Function To Work Out Factorial Numbers

Factorial of a number is defined as the product of the number and all of the numbers small than it. So if you take the number 4 the factorial of that number is 24 or 1 x 2 x 3 x 4.

Factorials are useful for a number of applications, for example when working out how many times a set of objects can be combined in different ways.

Use the following PHP function to work out the factorial of any given number. The first thing it does it make sure that the number is greater than 1 because the factorial of the number 1 is 1.

Get Fibonacci Numbers Using PHP

Fibonacci numbers not only have a few uses, but are also quite a nice little number sequence in themselves.

The sequence starts at 0, the next number is 1, and every number after that is the sum of the last two numbers. So the third number is 1, and the second number is 2.

To create this number sequence in PHP we need to create the first two items in the array. As we know that these are 0 and 1 we can create the array like this.

$fibarray = array(0, 1);

To create the third number we just add the first two numbers together.

$fibarray[2] = $fibarray[0] + $fibarray[1];

This can be continued for as much as we want if we add it to a loop.

for ( $i=2; $i<=10; ++$i ) {
 $fibarray[$i] = $fibarray[$i-1] + $fibarray[$i-2];
}

This will create an array with the following values.

PHP Script To Turn Image Into ASCII Text

Use the following snippet to convert any jpeg image into the equivalent image in ASCII format. It works by loading an image using the PHP GD2 library function ImageCreateFromJpeg() and then figures out the height and width of it. It then uses these values to loop through every pixel in the image and figures out the colour of that pixel. It uses this value to create a "span" element that uses the text colour of a # to change the colour of the text.

An additional time (and space) saver for this function is that it detects any pixels that are just off white and simply displays a &amp;nbsp; character instead.