Wordpress how to : Using and customizing

Questions and answer to better use Wordpress and customize it as needed.

How to display the last modified posts on the home page

Wordpress by default displays last created posts. But if you've recently updated an article, because the topic is back in the news, you might prefer that this article is not buried in the depths of the site, but appears on the first page.

To do this, in the index.php file, add the following line before the loop:

<?php query_posts($query_string . '&orderby=modified&order=desc'); ?>

And after the loop:

<?php wp_reset_query(); ?>

The whole code becomes:

<?php query_posts($query_string . '&orderby=modified&order=desc'); ?>
<?php while ( have_posts() ) : the_post() ?> ... <?php endwhile ?> <?php wp_reset_query(); ?>

This will also apply to the display by category.

Display the last modified date

Put this line inside the loop:

<p>Last modified: <?php the_modified_date(); ?></p>

You can set the display format by giving it as a parameter to the function in a string. More detail in the Codex.

How to create readable URLs

The first step after installing Wordpress is to configure it to create pretty URLs.

The pretty URLs are built from the title of the post and have the advantage of containing the keywords matching the content which is good for SEO. To have meaningful URLs, go to configuration, permalinks, and enter the following descriptor:

/% postname%

You can include the category to URLs but this has drawbacks and it is not the first choice. For more options, click on the info link in the configuration page.

How to display summaries on the home page?

And also on search by categories or others. Add this code:

<!-- More -->

after the first paragraph (it may contain an image). Or through the wysiwyg editor in visual mode, click on the icon that represents a splitted page.

How not to save previous versions

Since its version 2.6, Wordpress backup version of an article after every change, which usually has no interest and in certain scenarios may overload your database! If you change a text to add a comma, you do not need the text without the comma!
To override this behavior, add a flag to the wp-config.php file in the root of the CMS, from the installation:


How to delete the revisions?

The solution that seems the simplest is this SQL query from phpMyAdmin:

DELETE FROM wp_posts WHERE post_type = "revision";

However, links are created between the posts and other tables, especially for categories, that must be removed also. This request has been proposed:

DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c ON (a.ID = c.post_id)
WHERE a.post_type = 'revision'

The suffix could be different of wp_ in your database (see wp_config.php).
Save your database before to try the command.

There is also a plugin, if you do not want to use SQL.

Removing auto enumeration

The way Wordpress manages user names makes the site vulnerable. Here is a code to avoid that:

RewriteEngine On
RewriteCond %{REQUEST_URI} !/wp-admin.*
RewriteCond %{QUERY_STRING} ^author=.*$
RewriteRule (.*) /? [L,R=302] RewriteCond %{THE_REQUEST} wp.config.php [NC]
RewriteRule .* - [F,L]

Removing Emoji JavaScript code from the head section

This looks like an injection of code, but it is not, since version 4.2, it is the software itself which adds this JavaScript block at top of your pages without asking your opinion!

This hardly serves for the content written in Kanji (Japanese) and we do not know why they decided to include it by default in Wordpress. This is not the first of their whims. To remove this JS block, add these two lines at the end of functions.php file:

remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );

Using Google Docs as a text editor for Wordpress

Documents made ​​on line with Google Docs can be published as pages on your Wordpress site/blog.
To do this, once composed documents and closed the page...

  1. Look at the list of documents.
  2. In front of the page to post, open the list of actions and click on "Publish".
    A new page appears which offer to publish or Post to blog.
  3. Click on Post to blog.
  4. Or if your site has not been associated with Google Docs yet, click the command "setting".
    You get this form it remains to fill ...