WordPress tips for beginners

Just in case you haven't noticed, I've slightly updated the blog with a new theme :) Here is a little collection of WordPress tips/tricks I've learnt over the years. This is mostly a mental note to myself too!

My first rule is to always check the Codex before checking anywhere else. It has a lot of examples and describes thoroughly all the template functions.

Installation tips

Change your database tables prefix

Changing your database table prefix makes your WP installation a little bit more secure and also allows you to have multiple installations of WordPress in one single database. Add this line to wp-config.php

$table_prefix = 'prefix_here';

Disallow file edit in admin

Although very useful, it can be dangerous to allow file editing inside of WordPress admin. Disable this by adding this line to wp-config.php

define('DISALLOW_FILE_EDIT', true);

Generate secret keys

Go to https://api.wordpress.org/secret-key/1.1/salt/ to generate secret keys and add them to your wp-config.php file. If you change these keys, it will force all logged-in users to log in again.

Protect your installation

I'm often on the move, so I don't like the idea of limiting access to the admin to certain IPs. There are other easy and quick ways to make your installation more secure:

<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>
<Files /wp-config.php>
Order Allow,Deny
Deny from all
</Files>

The Codex gives a lot of other tips in their Hardening WordPress page.

Be discreet

Personally, I like “hiding” my WordPress theme folders. I am not loading anything from /wp-content/themes, my theme's JS & CSS files are loaded with wp_enqueue_script() and wp_enqueue_style() but they're not in the theme directory. I'm also hiding my WordPress version with this line of code:

remove_action( 'wp_head', 'wp_generator');

You can go further and change WordPress wp-content folder name and location but some plugins are hard coding “/wp-content” into their code so it's a little risky. Make sure your WordPress directories have the right permissions and are not publicly accessible.

Templating tips

Template hierarchy is your friend! Template hierarchy allows you to easily set up custom templates and markups for categories, custom post types, taxonomies, archives, pages, etc. If you memorize the file naming scheme and the template hierarchy, your theming process will be much more faster! :)

get_template_part() is also your friend, it makes your code more maintainable by avoiding duplicates and if you need to keep your variables, use:

include(locate_template('template-file.php'));

Codes you want to use often should be in functions to avoid duplication and make maintenance easier.

Avoid query_posts()

query_posts() modifies the main query, so there might be some unwanted side effects (especially if you forget to reset the query with wp_reset_query()). As mentioned in the Codex (http://codex.wordpress.org/Function_Reference/query_posts), if you need to modify the main query, do it with “pre_get_posts” instead.

function five_posts_on_homepage( $query ) {
  if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( 'posts_per_page', '5' );
  }
}
add_action( 'pre_get_posts', 'five_posts_on_homepage' );

I prefer using get_posts() in most cases. Also, if you use template hierarchy, you shouldn't have to make to many custom queries. :)

Organize your functions

functions.php can get really big quickly, so break it down into plugins, or several files. The starter theme Underscores has an “/inc” folder where they have different function files. Call the files by adding this line in functions.php

require get_template_directory() . '/inc/other-functions.php';

Your functions.php file will be much easier to read now!

Check if function exists

If you're using a plugin's function, check if the function exists first. If you don't and the plugin is deactivated/not installed, you may break your theme.

<?php if ( function_exists( 'plugin_custom_function' ) ) plugin_custom_function(); ?>

Learn more about function_exists().

Load assets properly

Use wp_enqueue_script() and wp_enqueue_style() to load your CSS and JS files, and remove unused scripts and styles from plugins and/or load them conditionally.

Maintenance tips

Delete spam

Even though spams are automatically deleted from the database after 30 days, they can pile up pretty quickly and make your database huge. Make sure to delete them regularly.

Akismet is making the database huge

My database size was over 50mb, even though I regularly removed posts revisions, spams, etc. The culprit was Akismet. The table wp_commentmeta was taking 46mb by itself! It seems that I'm not the only one suffering from this.

An useful query to delete orphaned post meta (though I only found 4 results with it) and other useful queries

Plugins spring cleaning

Installing a lot of plugins can be tempting and after a while, you find yourself with a ton of activated but unused plugins. They can slow down your website and increase your database size. Deactivate the plugins you don't use and delete the ones you're certain you won't use anymore. Use plugins that are well-maintained (updated regularly).

Use Jetpack Photon

Photon loads your images from WordPress CDN. Useful to load your images faster! One big limitation is that there is no cache invalidation: once your image is copied to their CDN, it stays this way. If you need to update your image, you need to change its name. It also doesn't work on gif files and can be buggy on very large files. Here is a great snippet to disable Photon in some situations.

2 comments

  1. ALIBERT 26 February 2014 at 07:14:03

    Hola Karine,

    Merci pour ton aide précieuse, grâce à toi, j'ai découvert plein de choses intéressantes sous WordPress.
    Peux-tu nous donner le nom du thème que tu as utilisé ?
    J'aimerais l'adpater pour le nouveau site de notre Jazz Club à Lyon : la Clef de Voûte.

    Bien à toi et bonne continuation,

    Julien

    • Karine 26 February 2014 at 14:55:16

      Bonjour Julien, contente d'avoir pu aider ! Le thème que j'utilise est un thème perso que j'ai développé moi même, désolée !

Comments are closed.