Hostingheaderbarlogoj
Join InMotion Hosting for $3.49/mo & get a year on Tuts+ FREE (worth $180). Start today.
Advertisement

New wp-config Tweaks You Probably Don't Know

by
Gift

Want a free year on Tuts+ (worth $180)? Start an InMotion Hosting plan for $3.49/mo.

The wp-config.php file: One of the most loved WordPress feature for some, one of the worst nightmares for others.

There are countless tips and tricks in a plethora of articles, and you can't get enough of them; however, this article is aiming to be different. In this article, we're going to get familiar with eight new wp-config tricks that are less known than functionality such as turning off post revisions, increasing the memory limit, or other similar features.

Don't forget to backup your installation! The tips in this article do not change any database values nor do they play with any files (except wp-config.php, of course) so you can just download a copy of your wp-config.php file, but you can backup your database and files to make sure. Better safe than sorry—frequent backups are always good!

1. Setting a Default Theme for New WordPress Installations With WP_DEFAULT_THEME

Let's say you're a freelance web designer (or a web design agency) who uses a base WordPress theme for almost every client of yours. Wouldn't it be great if you don't have to change the theme from "Twenty-Something" to your "Awesome Base Theme" every time you create a new WordPress installation?

Well, the WP_DEFAULT_THEME constant has you covered:

define( 'WP_DEFAULT_THEME', 'default-theme-folder-name' );

You may ask: "How am I supposed to set a default theme by editing the wp-config.php file when that file is created while installing WordPress?" Excellent question, but we're not going to change the wp-config.php file this time: Instead, we're going to insert the code above into the wp-config-sample.php file!

I'm not talking about editing and renaming the file like it's 2006, I'm talking about editing the file and leaving it like that: WordPress can detect the changes and additions inside the wp-config-sample.php file (except the database credentials) and use them while installing your fresh copy of WordPress.

Neat, right? I haven't found any web page mentioning this hidden gem (and I found this technique by accident), so this tutorial might be the first time this feature is covered.

2. Disabling WordPress' Automatic Update Feature With AUTOMATIC_UPDATER_DISABLED

You may have your reasons to prevent WordPress from updating itself: Maybe you're a developer who needs to work on many versions of WordPress simultaneously, or your website is on a highly-unstable server that requires you to be extremely careful.

Whatever the case may be, we're here to offer you the solution of disabling automatic updates for WordPress:

define( 'AUTOMATIC_UPDATER_DISABLED', true );

3. Enabling the "Trash" Feature for Media Files With MEDIA_TRASH

Ever screamed in a high-pitched voice and cursed for seven minutes straight, after deleting a media file when you're not supposed to delete it? I know I did, before learning this WordPress constant:

define( 'MEDIA_TRASH', true );

Set it and you're never going to have to worry about deleting a media file—except if you disabled WordPress' "trash" feature altogether by setting the EMPTY_TRASH_DAYS constant to "0".

Be careful with that, too.

4. Letting WordPress Skip the wp-content Directory While Updating With CORE_UPGRADE_SKIP_NEW_BUNDLED

I don't use the default themes. Ever. I'm not against them nor do I dislike them, but I prefer creating and using my own themes. Thus, I don't need a new default theme when I install a major update.

Because WordPress is so flexible and I'm not unique in this, it gives us the option to skip updating the wp-content directory:

define( 'CORE_UPGRADE_SKIP_NEW_BUNDLED', true );

This might also give the updating process a slight speed boost. Don't expect anything more than a second, though.

Credit: WP Engineer

5. Allowing Unfiltered WordPress Uploads for Administrators With ALLOW_UNFILTERED_UPLOADS

WordPress has file type restrictions when uploading files into the Media Library. You can't upload anything except images, documents, audio or video files—and it's good for security. But what if you're an independent software developer and want to upload new releases of your software—are you going to have to use an FTP client every time you update your software?

Nope, you just need to define the ALLOW_UNFILTERED_UPLOADS to ALLOW_UNFILTERED_UPLOADS like this:

define( 'ALLOW_UNFILTERED_UPLOADS', true );

This code doesn't allow every user to upload every file type, though. Only administrators are allowed to upload whatever they want.

6. Setting WPLANG Dynamically in Multilingual Websites

If you have a multilingual website and need your website to change the language of your theme and your administration panel, you can do a little trick to detect a visitor's browser language and define the WPLANG constant based on that information.

First, you need to create a wp-lang.php file and fill it with the code below, then save it in the same directory wp-config.php is in:

<?php 
// start the session 
session_start(); 

// if there's a "lang" parameter in the URL... 	
if( isset( $_GET[ 'lang' ] ) ) { 

	// ...set a session variable named WPLANG based on the URL parameter... 	
	$_SESSION[ 'WPLANG' ] = $_GET[ 'lang' ]; 

	// ...and define the WPLANG constant with the WPLANG session variable 
	define( 'WPLANG', $_SESSION[ 'WPLANG' ] ); 

// if there isn't a "lang" parameter in the URL... 	
} else {

	// if the WPLANG session variable is already set...
	if( isset( $_SESSION[ 'WPLANG' ] ) ) {

		// ...define the WPLANG constant with the WPLANG session variable 
		define( 'WPLANG', $_SESSION[ 'WPLANG' ] ); 	

	// if the WPLANG session variable isn't set...
	} else { 
		
		// set the WPLANG constant to your default language code is (or empty, if you don't need it) 		
		define( 'WPLANG', 'tr_TR' ); 
			
	} 
} 
?>

Then, in the wp-config.php file, replace the line where WPLANG is defined with the line below:

require_once( dirname( __FILE__ ) . '/wp-lang.php' );

And voilà! Your visitors can now change the language of your theme and admin panel by simple clicking buttons with a lang=fr_FR URL parameter.

Credit: A Stack Overflow Discussion (I modified the code.)

7. Defining Your WordPress.com API Key as a Constant With WPCOM_API_KEY

If you're using a plugin that requires a WordPress.com API key (like Jetpack or Akismet) and have lots of WordPress websites to maintain, it might be a little bit annoying to enter your WordPress.com API key every time you install that plugin.

There's an easy way, though. You can define your API key in the wp-config.php file as a constant:

define( 'WPCOM_API_KEY', 'YourKeyHere' );
Check if your other favorite plugins have this feature. Plugins like SlideDeck and Gravity Forms support defining their API keys as a constant, too.

8. Redefining Allowed HTML Tags From Scratch With CUSTOM_TAGS

There's a function in WordPress, wp_kses() which, according to WordPress Codex:

"...makes sure that only the allowed HTML element names, attribute names and attribute values plus only sane HTML entities will occur in $string".

You can edit its rules with filters, like this:

<?php 

function my_filter( $string ) {

 	global $allowedtags;
 	$allowedtags['img'] = array( 'src' => array () );

	return $string;
}
add_filter( 'pre_kses', 'my_filter' );
?>

But if you need total control, which means rewriting the $allowedposttags, $allowedtags and $allowedentitynames variables, you can do it by defining the CUSTOM_TAGS constant to true in wp-config.php:

define( 'CUSTOM_TAGS', true );
$allowedposttags = array();
$allowedtags = array();
$allowedentitynames = array();

You have to rewrite each variable below. You can just copy & paste the variables in wp-includes/kses.php, then edit the code you pasted.

Credit: OttoPress.com

Conclusion

About two years ago, I wrote the post Conquering the wp-config.php File—11 Good Practices and it was one of the most popular WordPress posts that time. And two years later, I'm sharing another wp-config-related post, but this time, I chose to share tips that you may never even heard about.

I really hope you like it.

What about you, do you know any good wp-config tricks? Post a comment to share your experience! And if you liked the post, don't forget to share!

Advertisement