Optional WordPress code snippets for functions.php

On this page I have compiled a list with optional code snippets for functions.php. This page is camplementary to the page where I collected standard code snippets for functions.php . That page contains code snippets which I use on (nearly) every project. I keep adding new code snippets to this page.

  1. Remove Search functionality from website
  2. Change read more link
  3. Remove welcome panel from WordPress dashboard
  4. Enable shortcodes in text widgets
  5. Only use one copy of jQuery
  6. Remove all unused widget areas from a theme
  7. Kill the update WordPress message in the admin area
  8. Enable HTML markup in user profiles
  9. Automatic copyright date
  10. Configure manual under construction page
  11. Remove “Private” from title of private posts
    1. First method
    2. Second method (suggested)
  12. Edit the “Leave a comment” title
  13. Remove Emojo from WordPress

1. Remove Search functionality from website

Remove search functionality if you don’t use it in your website, theme or templates.

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;" ) );

Change the “read more’ link text at your desire. For multilingual websites you can add the translation code.

function modify_read_more_link() {
    return '<a class="more-link" href="' . get_permalink() . '">Your Read More Link Text</a>';
}
add_filter( 'the_content_more_link', 'modify_read_more_link' );

3. Remove welcome panel from WordPress dashboard

The welcome panel is not always usefull. Remove it with this code.

remove_action('welcome_panel', 'wp_welcome_panel');

4. Enable shortcodes in text widgets

Shortcodes are still being used in WordPress 5.x, here is how to add them to widgets

// Enable shortcodes in text widgets
add_filter('widget_text','do_shortcode');

5. Only use one copy of jQuery

This code ensures that only one copy of jQuery is included, and calls it from Google’s servers to save bandwidth and take advantage of google speed and cache.

// smart jquery inclusion
if (!is_admin()) {
	wp_deregister_script('jquery');
	wp_register_script('jquery', ("https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false);
	wp_enqueue_script('jquery');
}

6. Remove all unused widget areas from a theme

// disable all widget areas
function disable_all_widgets($sidebars_widgets) {
	//if (is_home())
		$sidebars_widgets = array(false);
	return $sidebars_widgets;
}
add_filter('sidebars_widgets', 'disable_all_widgets');

7. Kill the update WordPress message in the admin area

WordPress keeps nagging users with the update message. With this code it only nags the administrator.

// kill the admin nag
if (!current_user_can('edit_users')) {
	add_action('init', create_function('$a', "remove_action('init', 'wp_version_check');"), 2);
	add_filter('pre_option_update_core', create_function('$a', "return null;"));
}

8. Enable HTML markup in user profiles

Sometimes it is usefull to have html markup in user profiles. Here is how to allow it.

// enable html markup in user profiles
remove_filter('pre_user_description', 'wp_filter_kses');

9. Automatic copyright date

I always try to make WordPress low maintenance. This is a piece of code to solve a tiny little piece of annual maintenance.

function copyright($start_year, $site_name) {
  $year = date('Y');
  echo "&copy; Copyright ";
  echo $start_year;
  if($start_year != $year) echo "-$year";
  echo ", $site_name, All Rights Reserved.";
}

10. Configure manual under construction page

I try to use code instead of plugins to do common tasks. I love to keep the websites as clean as possible. There are many free “Under Construction” plugins available, but often it is only used incidentally, so I prefer to create a custom landing page where I redirect visitors to. The allowed IP addresses gives access to the client and our developers.

function temp_under_construction() {
	//allowed IP addresses
	$allow = array('192.168.1.11'); 
	if(in_array($_SERVER['REMOTE_ADDR'], $allow)) {
		// Do nothing and give these ip-addresses full access
	}else{
		// Block access to all users accept administrator
		// Redirect to index2.php and administrator can visit login page
		if ( ($GLOBALS['pagenow'] !== 'wp-login.php') &&($GLOBALS['pagenow'] !== 'wp-login.php') && ($GLOBALS['pagenow'] !== 'index2.php') && (!current_user_can('administrator'))) {
			wp_redirect("http://www.terencetengker.nl/index2.php", 302);
		}
	}
}
add_action('template_redirect', 'temp_under_construction', 1);

Source: https://digwp.com/2017/08/tips-to-keep-wordpress-secure/

11. Remove “Private” from title of private posts

WordPress adds the word “Private: ” to the beginning of the title of private posts to indicate their status. If you need to remove the prefix, you can use one of the following code snippets. The second method uses dedicated wordpress filters and is the one you should use!

11.1 First method

// Remove "Private: " from titles
// Make sure you adjust the code with the word "Private: " in your own language. 
function remove_private_prefix($title) {
	$title = str_replace('Private: ', '', $title);
	return $title;
}
add_filter('the_title', 'remove_private_prefix');

11.2 Second method (suggested)

// Remove "Private: " from titles
function remove_private_title_format( $format ) {
    return '%s';
}
add_filter( 'private_title_format', 'remove_private_title_format' );
add_filter( 'protected_title_format', 'remove_private_title_format' );

12. Edit the “Leave a comment” title

// Change the comments title
function edit_comment_title( $defaults ){
  $defaults['title_reply'] = __('Stel je vraag', 'customizr-child');
  return $defaults;
}
add_filter('comment_form_defaults', 'edit_comment_title', 20);

13. Remove Emojo from WordPress

// remove all actions related to emojis
function disable_wp_emojicons() {
   remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
   remove_action( 'wp_print_styles', 'print_emoji_styles' );
   remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
   remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
   remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
   add_filter('tiny_mce_plugins', 'disable_emojis_tinymce');
   add_filter('wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2); 
   // Also for admin
   remove_action( 'admin_print_styles', 'print_emoji_styles' );
   remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
}
add_action( 'init', 'disable_wp_emojicons' );

Stel je vraag

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *