7 WordPress Functions.php File Tips & Tricks

Most of the beginners don’t know the importance of the functions.php file and how they can extend the functionality of WordPress theme or site by adding some functions. Every WordPress theme comes with the functions.php file. In this article, I will show you some Functions.php File Tips & Tricks that can be used to extend your theme functionality.

What is functions.php?

The functions.php file is where you add unique features to your WordPress theme. The functions.php file behaves like a WordPress plugin, adding features and functionality to a WordPress site.

Having said that, here are some extremely useful tricks for the WordPress functions file.

Change WordPress default Excerpt Limit.

Do you want to change the WordPress default excerpt limit which is 55 words to your own? so you just have to add this function to your themes functions.php file.

function qlc_custom_excerpt_length( $length ) {
return 20;
add_filter( 'excerpt_length', 'qlc_custom_excerpt_length', 999 );

In this function, the default excerpt limit 55 changed to 20. So in this way you can manage your own excerpt limit. Change 20 to the number of words you want to show in the excerpts.

Redirect 404 pages to Home Page

If you don’t want to show the 404 pages on your website and want to redirect 404 pages to the home page. so you just need to add this function to your functions.php file.

function redirect_404s() {
{ wp_redirect(home_url(), '301');
} }
add_action('wp_enqueue_scripts', 'redirect_404s');

This function will permanently redirect your 404 pages to the home page.

Register custom post type area in your theme

The most commonplace for widgets is sidebar’s Footer area etc. But if you want to place some additional content to header or body area like an advertisement, recent posts and anything you want to show.

The widget allows you to easily add content blocks to anywhere on the page, Header, footer etc. Typically every WordPress themes widget sidebars next to the content or in footer area not in the header or content blocks.

Paste the following code in your functions.php file:

add_action( 'init', 'my_custom_post_technology' );
function my_custom_post_technology() {
  $labels = array(
    'name'               => _x( 'technologies', 'post type general name' ),
    'singular_name'      => _x( 'technology', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'tech' ),
    'add_new_item'       => __( 'Add New technology' ),
    'edit_item'          => __( 'Edit technology' ),
    'new_item'           => __( 'New technology' ),
    'all_items'          => __( 'All technologies' ),
    'view_item'          => __( 'View technology' ),
    'search_items'       => __( 'Search technologies' ),
    'not_found'          => __( 'No technology found' ),
    'not_found_in_trash' => __( 'No technology found in the Trash' ), 
    'parent_item_colon'  => ’,
    'menu_name'          => 'Technology'
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our Technology specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  register_post_type( 'technology', $args ); 

Functions.php File Tips & Tricks and We also have a detailed tutorial on Create a Custom Post Type in WordPress

Enable WordPress to Upload Any File Type You Want

Some times we need to add an SVG image to our site but default WordPress not allowed to upload an SVG format. If you want to allow WordPress to upload an SVG file as well so just need to paste this function to your function.php file.

function my_myme_types($mime_types){
$mime_types['svg'] = 'image/svg+xml';
return $mime_types;
add_filter('upload_mimes', 'my_myme_types', 1, 1);

Replace “Howdy” message from the dashboard

Sometimes you may want to change the “Howdy” message from your WordPress dashboard and customize it according to your wish. Add the following code to the functions file.

function replace_howdy( $wp_admin_bar ) {
    $newtitle = str_replace( 'Howdy,', 'Logged in as', $my_account->title );
    $wp_admin_bar->add_node( array(
        'id' => 'my-account',
        'title' => $newtitle,
    ) );
add_filter( 'admin_bar_menu', 'replace_howdy',25 );

Just you need to change Logged in as message to whatever you want.

Display ads or anything using shortcodes

Sometimes you may not want to add something similar text or block of content to every page like an advertisement. Now the shortcode comes instead of placing whole content we need to make it as a shortcode like below.

This code will show you how you can create any kind of shortcode. just you need to copy the below code to functions.php file.

function show_google_ads() {
    return '
                place your ads code or content Here
add_shortcode('myshortcode', 'show_google_ads');

Insert the shortcode, [myshortcode] anywhere inside the posts and pages content where you want the ads to be displayed.

Disable/Change log error message on the admin login page

Tweak your functions.php by adding the following snippet to change the login error message to whatever you want.

function change_login_error_msg(){
add_filter( 'login_errors', 'change_login_error_msg' );

You may also refer WordPress Documentation https://developer.wordpress.org/themes/basics/theme-functions/

We hope Functions.php File Tips & Tricks helped you learn some new useful tricks for functions.php file in WordPress. Do you have any questions about these WordPress functions file Tips & Tricks and If so, ask away in the comments section below!

Leave a Reply