WordPress functions.php snippets

You can modify the functionality of your site, add custom functions and more to your WordPress website by adding or modifying code from within your themes functions.php file.  On this page you will find a number of functions you can use to add or modify various features of your WordPress website. A word of warning however, changes or additions made to your themes functions php file can be lost if you update or change your active theme, for this reason it is recommended to use a child theme or a plugin such as Code Snippets to add any of these to your website.  It is also important to back up your site before making changes to your themes functions, as if they are not implemented correctly, they can break your website.

Disable Search Bar & Search Functionality

If for whatever reason, you do not want your website to use the built in search functionality, you can use the snippet below.

Credit to original snippet source – WPEngineer

function fb_filter_query( $query, $error = true ) {
if ( is_search() ) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->query[s] = false;
// to error
if ( $error == true )
$query->is_404 = true;
}
}
add_action( 'parse_query', 'fb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );

Protect Your Site From Malicious Requests

You can use various different plugins to protect your site from malicious requests or use a third party firewall, however you can also add protection to your site from these sort of requests by adding the following code to your themes functions.php file.

Credit to original snippet source – – WP Snippet

global $user_ID; if($user_ID) {
 if(!current_user_can('administrator')) {
 if (strlen($_SERVER['REQUEST_URI']) > 255 ||
 stripos($_SERVER['REQUEST_URI'], "eval(") ||
 stripos($_SERVER['REQUEST_URI'], "CONCAT") ||
 stripos($_SERVER['REQUEST_URI'], "UNION+SELECT") ||
 stripos($_SERVER['REQUEST_URI'], "base64")) {
 @header("HTTP/1.1 414 Request-URI Too Long");
 @header("Status: 414 Request-URI Too Long");
 @header("Connection: Close");
 @exit;
 }
 }
}