6 WordPress Code Snippets to Enhance Your Website’s Functionality

WordPress is a popular content management system that powers millions of websites. One of the advantages of using WordPress is the availability of a wide range of plugins and themes that can enhance your website’s functionality and appearance.

However, sometimes you may need to add custom code to your WordPress site to achieve specific features or functionality that are not available in plugins or themes. In this article, we will share ten useful WordPress code snippets that you can use to enhance your website’s functionality.

Redirect Users to a Custom Login Page

By default, WordPress uses the wp-login.php page for user authentication. You can redirect users to a custom login page using the following code snippet:

function custom_login_page() {
    if( $_SERVER['REQUEST_URI'] == '/wp-login.php' ) {
        wp_redirect( '/custom-login-page/' );
        exit;
    }
}
add_action( 'init', 'custom_login_page' );

 

Limit the Number of Posts on Archive Pages

If you have a lot of posts on your website, the archive pages can become very long, which can impact your website’s performance. You can limit the number of posts on archive pages using the following code snippet:

function custom_archive_posts_per_page( $query ) {
    if ( $query->is_archive ) {
        $query->set( 'posts_per_page', 10 );
    }
}
add_action( 'pre_get_posts', 'custom_archive_posts_per_page' );

 

Disable WordPress Updates

WordPress regularly releases updates to fix security issues and bugs. However, sometimes you may need to disable updates temporarily, especially if you have custom code that may break with updates. You can disable WordPress updates using the following code snippet:

function disable_wordpress_updates( $update ) {
    global $wp_version;
    return (object) array(
        'last_checked' => time(),
        'version_checked' => $wp_version,
        'updates' => array()
    );
}
add_filter( 'pre_site_transient_update_core', 'disable_wordpress_updates' );

 

Remove Query Strings from Static Resources

Query strings can impact your website’s caching and performance. You can remove query strings from static resources using the following code snippet:

function remove_query_strings( $src ) {
    if ( strpos( $src, '?ver=' ) ) {
        $src = remove_query_arg( 'ver', $src );
    }
    return $src;
}
add_filter( 'script_loader_src', 'remove_query_strings', 15, 1 );
add_filter( 'style_loader_src', 'remove_query_strings', 15, 1 );

 

Add a Custom Dashboard Widget

You can add custom dashboard widgets to provide quick access to frequently used features or information. You can add a custom dashboard widget using the following code snippet:

function custom_dashboard_widget() {
    wp_add_dashboard_widget(
        'custom_dashboard_widget',
        'Custom Dashboard Widget',
        'custom_dashboard_widget_content'
    );
}
function custom_dashboard_widget_content() {
    echo '<p>Custom Dashboard Widget Content</p>';
}
add_action( 'wp_dashboard_setup', 'custom_dashboard_widget' );

 

Display a Message to Logged-out Users

You can display a message to logged-out users using the following code snippet:

function custom_logged_out_message() {
    if ( ! is_user_logged_in() ) {
        echo '<p>Custom Message for Logged-out Users</p>';
    }
}
add_action( 'wp_footer', 'custom_logged_out_message' );