Categories
Blog Bugfixing WordPress

WordPress Beginner tips: Gotchas when Starting out in WordPress Development

I really enjoy working on WordPress projects. For every one I kick out of the gate, the more comfortable I am with using it. However, getting to where I am now meant learning a few things first to ‘git gud’. Here are some wordpress beginner tips.

Permalinks are your friend

Sometimes, when you create custom post types & taxonomies, or mess with custom queries, there’s a good chance you’ll hit a few 404s along the way. The first step to try and resolve this is to go to your WordPress admin ‘permalinks’ page and save it. No need to actually change any settings, just save. This will essentially flush the permalinks that WordPress has running in the background. Somewhere, maybe in a cache of some sort?

Child themes are better

When I started out, I tended to work with the themes that came bundled with WordPress core. If you intend to make customisations to your theme, it’s recommended practice to create a child theme to make your edits. This preserves any changes that get applied to it in an update. Though you may need to copy the changes across if they affect a file on the child theme. An added benefit is that you only need to include the files you need to change. The templating system in WordPress is smart enough to look in your parent theme files first, before checking your child theme.

This approach can also be applied to your own bespoke themes. The idea is that you bundle all your functionality into your parent theme, like modifications to the admin area, then the child theme is reserved for template markup edits. This is probably the most important of my wordpress beginner tips.

Template hierarchy

As you know, WordPress being able to locate files within a theme. The template hierarchy is an essential feature to familiarise yourself with. Take a look at https://developer.wordpress.org/themes/basics/template-hierarchy/ A lot of the stuff is done on the background, so where you can , get around needing to mess with permalinks too much.

There’s a lot of power in your wp-config file

The config file is super powerful for quick global settings. Here’s a useful link covering most – if not all – of them:

https://gist.github.com/MikeNGarrett/e20d77ca8ba4ae62adf5

Some of the ones I’ve found particularly useful are:

define( 'WP_HOME', 'http://' . $_SERVER['HTTP_HOST'] );
define( 'WP_SITEURL', 'http://' . $_SERVER['SERVER_NAME'] );

I use these constants all the time, as it allows me to move files between servers, local setups and so on without worrying about redirects. Just remember that the $_SERVER value of your current environment may be stored on the database, if you save anything whilst logged in. This normally isn’t an issue with the site functionality, though I reserve these constants for switching between local and staging environments. I comment them out when the site goes live.

If you’re managing a site for someone and you’re a little nervous about giving them full admin rights, these values come to the rescue when pressed for time:

// Kill the WordPress file editor
define( 'DISALLOW_FILE_EDIT', true ); 
// Don't allow users to update core, plugins, or themes
define( 'DISALLOW_FILE_MODS', true ); 

A couple more I use here. The first one is enabled if I have a system in place to manage my updates. I don’t want it getting in the way if I’m already automating my backups and updates. The second applies to the contact form 7 plugin, and removes unwanted p tag formatting.

define('AUTOMATIC_UPDATER_DISABLED', true)
define('WPCF7_AUTOP', false)

I cover some other useful settings in my article, WordPress HTTPS Redirect Problem? Destroy it Like a Boss

Watch your pagination

You’re bound to run into pagination issues with WordPress at some point. For me, the issue was returning an incorrect number of pages. I had a custom query that had a limit on the amount of results per page. However, when I paginated over the last few page results, I had a 404 error. I tried to give the permalink settings a reset, but the problem persisted.

One solution I found in my searching was to add this before the query loop:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

Then you use $paged as the value for ‘paged’ in the query arguments. This can usually fix this issue, but not this one.

The solution here seemed to be a bit simpler than that. Under ‘WordPress Settings > Reading > Blog pages show at most‘ there’s an option define the maximum number of posts shown. This has a habit of conflicting with custom query values. So what I needed to do was ensure that my query posts_per_page value was less than or equal to the value set in the admin options page.

Turn off IIS

I found that using IIS locally, in tandem with a php server, could cause problems. In particular, I found that WAMP wouldn’t start up properly. Disabling IIS fixed the issue (stopping the server completely). If IIS is installed, I had to sometimes do it manually, as I found that IIS would sometimes restart. When you’re looking for wordpress beginner tips, this is probably the least obvious, but it’s one worth remembering for those weird local server issues.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.