Back to Top

WordPress Virtual Pages

This wordpress function class code can be used to create virtual on the fly pages – note they won’t show up by default on widgets or in menues (they only exist when physically called via slug url), but can be custom linked. Possible to create links to them using header / footer filters/hooks. Also ideal if you want dynamic content displayed.

Replace the following with page urls (slug), title and contents..

‘slug’ => ”,
‘post_title’ => ”,
‘post_content’ => ”

if (!class_exists('wp_virtual_page_setup')){
    class wp_virtual_page_setup

        public $slug ='';
        public $args = array();

        function __construct($args){
            $this->args = $args;
            $this->slug = $args['slug'];

        public function virtual_page($posts){
            global $wp,$wp_query;
            $page_slug = $this->slug;

            if(count($posts) == 0 && (strtolower($wp->request) == $page_slug || $wp->query_vars['page_id'] == $page_slug)){

                //virtual page
                $post = new stdClass;
                $post->post_author = 1;
                $post->post_name = $page_slug;
                $post->guid = get_bloginfo('wpurl' . '/' . $page_slug);
                $post->post_title = 'page title';
                //put your custom content here
                $post->post_content = "Fake Content";
                //just needs to be a number - negatives are fine
                $post->ID = -42;
                $post->post_status = 'static';
                $post->comment_status = 'closed';
                $post->ping_status = 'closed';
                $post->comment_count = 0;
                $post->post_date = current_time('mysql');
                $post->post_date_gmt = current_time('mysql',1);
                $post = (object) array_merge((array) $post, (array) $this->args);
                $posts = NULL;
                $posts[] = $post;

                $wp_query->is_page = true;
                $wp_query->is_singular = true;
                $wp_query->is_home = false;
                $wp_query->is_archive = false;
                $wp_query->is_category = false;
                $wp_query->is_404 = false;

            return $posts;
    }//end class
}//end if

$args = array(
        'slug' => '',
        'post_title' => '',
        'post_content' => ''
new wp_virtual_page_setup($args);

Posted in Misc Topics |

Code to remove duplicate piwik domain entries

I have used the following code to remove any duplicate website entries in a piwik site statistics package – sometimes occurs if account addition apis are used elsewhere. Replace yourtoken with your piwik api token, and http://yoursite/piwik with your piwik sites main url path – do not include a final / at the end. Hope this helps someone.


$token = "yourtoken";
$piwiksite = "http://yoursite/piwik";

$get_sites = $piwiksite."/index.php?module=API&method=SitesManager.getAllSites&format=xml&token_auth=".$token;


$rows = new SimpleXMLElement($xml);

foreach ($rows->xpath('//row') as $row) {
if(in_array($name,$names)===false) {
} else {

if(count($dups)>0) {
foreach($dups as $id) {
$delete_site = $piwiksite."/index.php?module=API&method=SitesManager.deleteSite&idSite=".$id."&token_auth=".$token;

Posted in PHP |

Make categories widget only show current parent’s categories

This simple snippet of code either added as a plugin or in your themes functions.php script will do the following:

(1) Always show a category on the widget even if no posts in that category

(2) If child categories exist only displays on the widget the category of the current selected parent category.

Note: To make it easier to flow and to improve search engines, might also be worth adding breadcrumb navigation too.

function dunnies_wp_list_categories($cat_args){
$this_cat = (get_query_var('cat')) ? get_query_var('cat') : 0;
return $cat_args;
add_filter('widget_categories_args', 'dunnies_wp_list_categories', 10, 2);

Hope this helps someone.

Posted in Wordpress |

Importance of using XML encoding for doc objects

Unless you want loads of weird characters displayed in a php document object it is very important to use encoding and decode the orginal string to be processed first when loading the org. string

eg this uses utf-8

$xml = new DOMDocument();
$xml->encoding = 'utf-8';

Bye bye weird characters (doc objects in php tend to change the character codings, so this avoids the problem)

Posted in PHP |

Better PHP US Phone number validation function

I have replaced phone validation functions in contact scripts as seems to work better with a range of different US phone numbers..

function VALIDATE_USPHONE($phonenumber,$useareacode=true)
if ( preg_match("/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/",$phonenumber) || (preg_match("/^[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/",$phonenumber) && !$useareacode)) return eregi_replace("[^0-9]", "", $phonenumber);
if ( preg_match("/^([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})$/",$phonenumber) ) return eregi_replace("[^0-9]", "", $phonenumber);
if ( (strlen(eregi_replace("[^0-9]", "", $phonenumber))>=6)&&(strlen(eregi_replace("[^0-9]", "", $phonenumber))<=16) ) return eregi_replace("[^0-9]", "", $phonenumber);
return false;
Posted in Misc Topics |

Try this fix if you get a mysql error 2006 on importing database

Sometimes you just cannot get that mysql database file to import, even via the command line.

eg mysql -p -u username databasename < databasefile.sql and you get a nasty ERROR 2006 - MYSQL server has gone away message in return. The first obvious thought is that it is somekind of timeout issue, so you waste ages playing around with the interactive and wait timeout values. Try this instead, or in addition to the time out values.. Edit the /etc/my.cnf file and either add or edit the appropiate line in the [mysqld] block and make


Then restart mysql again:

service mysql restart

and try to import your database again – worked for me.

Posted in Misc Topics |

Adding nofollow dynamically to wordpress blogroll links

This code snippet can be added either as part of a wordpress plugin or part of a wordpress theme to set all the blogroll links to have the nofollow attribute (useful if you have a lot of websites linking back to other sites in your collection).

// add nofollow to all blog links

add_filter( 'get_bookmarks', 'mb_links_get_bookmarks', 10, 2 );

function mb_links_get_bookmarks($links, $args)

	if (is_array($links)) {
		foreach (array_keys($links) as $i) {
				$links[$i]->link_rel .= ' nofollow';
				$links[$i]->link_rel = trim($links[$i]->link_rel);

	return $links;
Posted in Wordpress |

Add common styling parameter to string image tags

This I have found useful for dealling with class/styling removed from simplepie rss incoming feeds in wordpress.. in this example all tags have a padding:10px; style automatically added. Useful for wordpress content filters.

$string = "<br><p><img src='image1.jpg'></p><br><img src='image2.jpg'>";
echo add_img_style($string,'padding:10px;');
function add_img_style($string,$style) {
$xml = new DOMDocument();
    foreach($xml->getElementsByTagName('img') as $link)
    $link->setAttribute('style', $style);
$o= preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $xml->saveHTML()));
$o= str_ireplace("\n","",$o);
return $o;
Posted in Misc Topics |