WordPress security hardening: the first steps before launch
A fresh WordPress installation is reasonably secure, but a few small changes reduce the attack surface a lot. None of them needs a plugin: they go into wp-config.php, .htaccess and functions.php, and most take a minute.
Problem: A new site is about to go live with the default settings, and you want to close the most common holes first.
Solution: Disable file editing in the admin, force HTTPS for logins, block direct access to sensitive files, hide login error details, and keep core, plugins and themes updated.
wp-config.php
<?php // No theme and plugin editor in the admin: a stolen admin password // should not give the attacker a PHP editor define( 'DISALLOW_FILE_EDIT', true ); // Logins and the admin only over HTTPS (when the site has a certificate) define( 'FORCE_SSL_ADMIN', true );
Use unique authentication keys and salts: generate them at https://api.wordpress.org/secret-key/1.1/salt/ and replace the eight define() lines. Changing them later logs out every user, which is also the quickest reaction to a suspected break-in.
A custom table prefix instead of wp_ is set in $table_prefix before installation. It only stops the most primitive automated SQL injection attempts; on a running site, changing it is not worth the risk.
.htaccess (Apache 2.4)
# Block direct access to wp-config.php
<Files wp-config.php>
Require all denied
</Files>
# No directory listings
Options -Indexes
On Apache 2.2 the same is written as Order allow,deny and Deny from all. On Nginx the rules go into the server block instead.
Whether to block xmlrpc.php depends on the site: the Jetpack plugin and the WordPress mobile apps use it. If nothing on the site needs it, block it the same way as wp-config.php.
functions.php
<?php
// Do not print the WordPress version in the page head
remove_action( 'wp_head', 'wp_generator' );
// Do not tell whether the username or the password was wrong
add_filter( 'login_errors', function () {
return __( 'Wrong username or password.', 'textdomain' );
} );
Hiding the version is a small thing (the version can be guessed in other ways), the important part is to keep it current.
File permissions and updates
Folders should be 755 and files 644, wp-config.php 640 or 600 if the server allows it. Nothing should be 777. Updates matter more than any of the settings above: most hacked WordPress sites run an outdated plugin with a known vulnerability. Remove plugins and themes you do not use, they can be attacked even when they are deactivated.
NOTE: Do not block the REST API for guests “for security”: many plugins, for example Contact Form 7, send forms through it. Restrict only the endpoints you really need to hide, such as the list of users.