Code snippets are small pieces of code that can be added to a WordPress website to customize and extend features without editing the entire source code. Using code snippets helps you add advanced functionality without breaking the theme or plugins. In this article, we will show you how to use code snippets to customize WordPress easily and effectively.

Table of Contents

    Step 1: Install Code Snippets Plugin

    To easily manage and add code snippets to your WordPress site without having to edit files functions.php theme, you can use the Code Snippets plugin. This is a powerful tool that helps you add, manage, and edit code snippets without causing page errors.

    Install Code Snippets Plugin

    • Visit the WordPress dashboard and go to Plugins -> Add New.
    • Search for plugins Code Snippets and click Install Now (Install now).
    • Once installed, click Activate (Activate).
    Instructions for Using Code Snippets to Customize WordPress
    Code Snippets plugin interface on WordPress.

    Step 2: Add New Code Snippet

    After installing the Code Snippets plugin, you can start adding custom code snippets to change or extend the functionality of your WordPress site.

    How to Add New Code Snippet

    • Go to Snippets in the WordPress admin menu and click Add New (Add new).
    • Enter a name for the code snippet to make it easier to manage (e.g. “Hide admin bar for non-admin users”).
    • Add the code you want to use in the Code pane.
    • Click Save Changes and Activate to activate the code.

    Example: Hide Admin Bar for Non-Admin Users

    
    function an_thanh_quan_tri() {
        if (!current_user_can('administrator')) {
            show_admin_bar(false);
        }
    }
    add_action('after_setup_theme', 'an_thanh_quan_tri');
    

    The above code will hide the admin bar on your WordPress site from non-admin users.

    Step 3: Manage and Edit Code Snippets

    Code Snippets plugin allows you to manage and edit code snippets easily without having to edit directly in the file functions.php.

    How to Edit Code Snippet

    • Go to Snippets and find the code you want to edit.
    • Click the snippet’s name to open the editor.
    • Make the necessary changes and click Save Changes to save.

    How to Disable or Delete Code Snippet

    • To disable a piece of code without deleting it, simply uncheck the box Active next to the snippet name.
    • To remove the code, click Delete and confirm deletion.

    Step 4: Commonly Used Code Snippets

    Here are some commonly used code snippets to customize WordPress:

    1. Turn off Gutenberg Editor

    
    add_filter('use_block_editor_for_post', '__return_false', 10);
    

    This code will disable the Gutenberg editor and switch back to the classic editor.

    2. Change the Number of Posts on the Blog Page

    
    function thay_doi_so_bai_tren_blog($query) {
        if ($query->is_home() && $query->is_main_query()) {
            $query->set('posts_per_page', 5);
        }
    }
    add_action('pre_get_posts', 'thay_doi_so_bai_tren_blog');
    

    This code changes the number of posts displayed on the blog page to 5.

    3. Automatically Delete Trash After 7 Days

    
    define('EMPTY_TRASH_DAYS', 7);
    

    This code will automatically delete posts in the trash after 7 days.

    Tips for Using Code Snippets Effectively

    To use code snippets effectively and safely, you should note the following tips:

    • Check compatibility: Before adding any code, make sure it is compatible with the WordPress version and plugins you are using.
    • Website backup: Always back up your website before adding or editing code snippets to prevent risks.
    • Use plugins to manage: The Code Snippets plugin helps you manage and track code snippets easily without editing WordPress root files.

    Code snippets WordPress

    Using code snippets is an effective way to customize and extend WordPress features without installing too many additional plugins. By using the Code Snippets plugin, you can manage and add code snippets easily, securely, and flexibly. Start exploring the right code snippets for your site today to get the most out of WordPress customization!

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Dark mode