Category: WordPress Troubleshooting

Query All Custom Page Templates in WordPress

Make your WordPress quality assurance easier by using this custom query to easily find all custom pages templates and the pages that use them.

Here is a query you can use/run to find an example of any custom page template created and used on your WordPress site:

The Custom Page Templates Query

SELECT posts.ID, meta.meta_value
FROM wp_posts AS posts
LEFT JOIN wp_postmeta AS meta ON posts.ID = meta.post_ID
AND meta.meta_key = '_wp_page_template'
WHERE posts.post_status = 'publish'
AND meta.meta_value IS NOT NULL
GROUP BY meta.meta_value

The Custom Page Templates Results

That query will then return results that show the post ID as well as the page template file name so you can then easily see all your custom page templates in place. (See image below as an example)

Using the Results for Quality Assurance

In order to then actually see the pages that use the custom page templates, you would use the following URL format:

http://www.yoursite.com/?p={ID}

Real world example using this web site as well as the post ID of 123:

http://jzelazny.wpengine.com/?p=123

To do your final QA – you would just substitute the post ID with each found in the query results. You can then be sure you have tested a sample of each custom page template.

Focus on Your Code Not Your Environment

We are big fans of WP Engine. There are a lot of great reasons to use them for WordPress hosting – but one of the coolest services they offer is the ability to set up and use their staging area. Recently – we created a new theme and used the staging environment for all development. Since we loved it so much – we wanted to share on how to use it!

Staging Environments Made Easy

staging

When we started the development – we simply went to our WP Admin area, clicked on one button “Copy from LIVE to STAGING” and within minutes – an email was sent letting us know it was all set up. All content, themes, plugins, settings – all the same as the live site. Since working with the live content, plugins, etc – is crucial for quality assurance – being able to do this in one simple step is a real time saver. We then were able to stage everything, adjust content, widgets, etc and then click on “Copy site from STAGING to LIVE” and viola – everything we changed was then in place on the live site.

Staging: Not Just For Theme Updates

While the staging environment was great for the new theme – it can also be extremely helpful for general content changes, plugin update testing, and general debugging. Want to test out a mobile theme or a new plugin? Click the staging button and test. If you like it – simply push your changes from staging to live with one click.

Thanks WP Engine for making this staging environment so simple! If you are a WP Engine user and have not used this feature – take a few minutes now and check it out.

Analyze Your WordPress Queries

There are many ways to debug and optimize WordPress, and here is one more of my favorites: save queries for later analysis (from the Codex). If you are using a complex theme, several plugins, or even a few custom plugins – you may might notice your site is either slow, sluggish, or just seems to not load as fast as you would like.

Generally people will install a free theme or plugin(s) (I have seen sites with over 100 plugins) and think “wow – I have every possible custom functionality in place and I did not have to do any custom development!” Then the same proud individual will contact us because their site is not performing as they would like.

One of the first things we do is take a look at all the queries that are used on each page load and we do that by editing two files:

wp-config.php
define( ‘SAVEQUERIES’, true );

footer.php

<?php
if ( current_user_can( 'administrator' ) ) {
    global $wpdb;
    echo "<pre>";
    print_r( $wpdb->queries );
    echo "</pre>";
}
?>

Please note – these changes should only be used when debugging. If you keep them in place – it will have a definite impact on performance.

Once you have the changes in place (assuming you are logged in as the administrator) you will be able to view each query, what function called it, and how long the query took to execute.

From the output – you should easily be able to see what queries are most expensive, what plugins you might need to re-consider, etc.

Debugging with WPDB

One of the best things about developing with WordPress is the WordPress Codex. The amount of documentation and examples makes development and debugging much more enjoyable compared to working with other code.

If you do custom WordPress development or even plugin and theme development and are not familiar with the following options available with WPDB, take a few minutes to read more about them and then test them out. These few commands have saved hours of frustration for us when debugging custom development.

You can turn error echoing on and off with the show_errors and hide_errors, respectively.

<?php $wpdb->show_errors(); ?> 
<?php $wpdb->hide_errors(); ?> 

You can also print the error (if any) generated by the most recent query with print_error.

<?php $wpdb->print_error(); ?> 

Finally – you can also easily see your last query and last result using:

<?php $wpdb->last_query(); ?> 
<?php $wpdb->last_result(); ?> 

Further documentation from the WordPress Codex on these time saving helpers.

So – if you are ever unsure why a query is not working as you expect – simply add in the show error and print error code and you will instantly see exactly what is really going on.

Debugging Errors With WordPress

Debugging your PHP errors is very easy with just a few lines in your wp-config.php file. By adding the following code to the wp-config.php file – you will turn off displaying the errors to users while logging them to a log file for your review.

define('WP_DEBUG', true); // or false
if (WP_DEBUG) {
  define('WP_DEBUG_LOG', true);
  define('WP_DEBUG_DISPLAY', false);
  @ini_set('display_errors',0);
}

Documentation: http://codex.wordpress.org/Editing_wp-config.php#Configure_Error_Log

*Note from the documentation: The log file will be called debug.log located in /wp-content. You may need to create it with appropriate permissions (666) first if your web server does not have write access.

Hopefully by logging the errors – you can easily debug any issues without having any users see them while browsing the site.

HOWTO: Show Page Template Name

A cool function to simply display the path of the WordPress template used on a particular page:

<?php echo get_page_template() ?>

How/when to use this?
If you are trying to debug a site and want to quickly see what page template is used – you can put that PHP code into footer.php and it will then echo out the path so you can quickly and easily see what template is being used.

Be Smart: Update/Backup WordPress

Over the last two weeks we have been busy fixing a lot of issues for people that could have been avoided all together if two simple tasks were done:

  1. Backup your WordPress data
  2. Keep WordPress up to date

Luckily for most people – both of these tasks are simple and even automated. First of all – make sure you always have a recent backup of your WordPress database. There is a great plugin available (free) that will actually automate the process and even email you a copy of the database every x days, x weeks, etc. If you have one plugin installed with your WordPress site – install AND use WP-DB-Backup.

If you have a lot of media on your local server – make it a point to back that up every x days (whatever you can live with if something were to happen).

As for keeping WordPress up to date – it has never been more simple. When there is a new version of WordPress available – you will be notified when you log into your site. Simply click on the link to either download the latest version or to update WordPress right then and there. You cannot imagine how much time, effort, stress, and money simply updating WordPress can save you in the long run.

WordPress: Keep Up to Date – No Excuses

Over the past few days, there has been a lot of buzz going around about keeping WordPress up to date because of a specific worm going around that is causing havoc for out of date WordPress versions. The worm creates a user, hides it from the user list, and then inserts all kinds of junk into your posts.

There are multiple ways to easily find out when WordPress has been updated.

Since everyone who uses WordPress should stay on top of these updates, here are a few ideas on how to find out when a new version has been released:

  1. Pay attention the the “Upgrade Now” notice when you log into WordPress.
  2. Get notified by email with the Upgrade Notification by Email plugin
  3. Read regularly/subscribe to the WordPress blog
  4. Read regulary/subscribe to this blog

Updating is quick and easy – and should always be done after you have a database backup. Database backups are quick and easy as well with WP-DB-Backup plugin.

Not really sure if you want to do the upgrade yourself? We would be happy to do it for you – just contact us.

WordPress 2.8/Plugins Issues

I have updated over 25 sites so far and for the most part – everything has gone smoothly. There were a handful of sites though that had issues with a few plugins (including this site) due to one common issue: jQuery. If you are seeing any strange behavior – post upgrade, the first thing you will want to do is to make sure all your plugins are up to date. After that – if you are still having problems – check to see if any of your plugins rely on jQuery. If they do – next check the code (either look at the js files in the plugins, or look at your site’s source code to see if it is loading an old version of jQuery). There is a good chance they are using an older version, or are relying on something that no longer works with the latest release of jQuery (1.3.2 as of today).

Some plugin developers are aware of the issues but have not released an updated plugin. Because you are most likely not alone with your issues – check out their support forums, or post comments to see if someone came up with a quick fix so you can be back in business.

While most WordPress updates can be performed quickly and easily by almost any user – there are times when you run into unexpected problems that can cost you dearly. When this happens – don’t hesitate to call us. We have been working through WordPress updates for years and can help out!

View All Options for WordPress

Ever need to view all options for all your plugins (and standard WordPress options like home URL, etc)?
You can view every option available (and edit most) by going to: http://www.yoursiteurl.com/wp-admin/options.php (assuming WordPress is in the root folder).

So now if you need to look at options, make changes, or see a particular value – you can do so on one page.