PHP

Get Child Pages List In Wordpress

It is possible to print out a list of pages in Wordpress, but these functions are designed to print from the root pages to a certain level. I often need to print out a list of pages that are children of the current page being looked at so I created the following function.

Remove The Last Line From A File In PHP

Removing the last line from a file is an easy process and can be done in just a few lines of code.

Parsing XML with PHP

XML data extraction can be a common task, but to work directly with this data you need to understand how PHP parses XML. There are various different functions involved in parsing XML in PHP, all of which work together to extract data from a XML document. I will go through each of these functions and tie them together at the end.

xml_parser_create()

This function is used to create the parser object that will be used by the rest of the process. This object is used to store data and configuration options and is passed to each of the functions involved.

$xml_parser = xml_parser_create();

xml_set_element_handler()

Next we need to set up the functions that will be used in the parsing of the script. The xml_set_handler() method takes the following parameters:

Round Timestamp To Nearest Time With PHP

If you want to create a "rounded" time stamp, for example, to the nearest 15 minutes use this as a reference:

PHP IP To Location

Converting an IP address into some useful location information can be useful if you want to find out where sites are hosted or customise content to users depending on their location.

All this code is freely available over at github.

There are several ways to do this, all of which have their advantages and disadvantages, but sticking with one can cause rewriting a lot of code in the future. So rather than pick one and stick with it I decided to use dependency injection to allow different classes to be used that convert IP addresses to locations in different ways. The first task is to create an abstract class that will be used to construct the rest of the IP location classes. Each class that extends this abstract class will contain a method called getIpLocation() that will convert an IP address into a location, and a method that will update the data source for the location lookup. Rather than lump all of the classes into a single directory I have created a directory called Service, into which all of the different classes that lookup IP addresses will be kept.

parse_url Warning Bug In Wordpress 2.9.2

Whilst setting up a new Wordpress install for a site development I found an odd little issue that would only occur in certain circumstances. My current development platform consists of an Apache server on which I create a virtual host for every development site I need. However, rather than setting a DNS entry for each address I just listen to diferent ports. In this particular instance I used the port 59419.

When the install was complete I was presented with the following error at the top of every admin page.

Selectively Turn Off Drupal Caching

It is sometimes necessary to turn off caching on certain pages on a Drupal site. This might be when trying to do something out of the ordinary, like write information to a file, or randomly generate a section of a template. The following code can be used to turn off caching just on the front page of the site.

function theme_preprocess(&$vars, $hook) {
    if ($vars['is_front'] == true) {
        $GLOBALS['conf']['cache'] = false;
    }
}

Adding false to the cache item of the conf globals array at run time will turn off the cache. This is within an if statement that checks to see if the current page is the Drupal front page so it will only be used on the front page.

Default Function Parameters In PHP

When creating functions in PHP it is possible to provide default parameters so that when a parameter is not passed to the function it is still available within the function with a pre-defined value. These default values can also be called optional parameters because they don't need to be passed to the function. I have seen this sort of code being used incorrectly quite often recently so I thought I would go over it in a post.

Creating a default parameter in a function is very simple and is quite like normal variable assignment. The following function has a single parameter that is set to 1 if it is not passed when calling the function.

function testFunction($a = 1)
{
    return $a;
}

Format A List Of Items In PHP

It is usual when writing a list of items to separate each item with a comma, except the last two items, which are separated with the word "and". I recently needed to implement a function that took a string and converted it into a list of this type so I thought I would expand on it and post it here.

The function takes a single parameter, which can either be an array or a comma separated string. If an array is passed to the function then it is converted into a comma separated string and then passed onto the next part in the function. The function then removes any trailing commas, any commas that have nothing in between them and then makes sure that each comma has a single space after it. The final step is to replace the last comma with the word "and". Once the manipulation is complete then the resulting string is returned. If the string (after removing any trailing commas) doesn't contain any commas then it is simply returned.

PHP Overloading

Overloading in PHP describes the way in which properties and methods of an object can be dynamically created or accessed without having to define them first. Traditionally, the word overloading in programming is used to describe accessing object methods with the same name but with different parameters. It is not possible to do this in PHP as it will complain about methods having the same name, so the term describes calling a method or accessing a property that hasn't previously been set or is out of scope. In object orientated terms this means that the method or property is private.

Object Property Overloading

Property overloading allows you to access the property of an object through a method without having to write them first. It can also be used to access any properties that are inaccessible. There are two basic property overloading methods available, these are __set() and __get(), both contain a double underscore (_) in their name.