Please ensure Javascript is enabled for purposes of website accessibility

Guide to Using Code Snippets in WordPress: Unlocking WordPress Customization

Read Time: 7 minutes

Disclaimer: links may be affiliate links.

Table of Contents

WordPress is a popular platform allowing users to create and manage websites without coding experience. However, there may be instances where you want to add custom functionality to your website that cannot be achieved through the default options. In such cases, using code snippets can be a viable solution.

Code snippets are small pieces of code that can be added to your WordPress site to add custom functionality or modify existing features. While adding code snippets directly to your theme’s functions.php file is possible, this is not recommended as it can cause compatibility issues with future theme updates. A better alternative is to use a plugin like “Code Snippets” that allows you to manage code snippets easily and safely.

This article will discuss using the “Code Snippets” plugin to add code snippets to your WordPress site. We will also provide examples of code snippets for hiding the admin bar, customizing the WordPress login, and modifying WooCommerce functionality.

Getting Started with Code Snippets Plugin

Before we begin, let’s go through the steps required to install and activate the “Code Snippets” plugin:

  1. Log in to your WordPress dashboard and navigate to Plugins > Add New.
  2. In the search bar, type “Code Snippets” and hit enter.
  3. Click the “Install Now” button next to the “Code Snippets” plugin.
  4. Once the plugin is installed, click on the “Activate” button.
  5. After activation, you will see a new menu item called “Snippets” in the WordPress dashboard sidebar.

Now that you have installed and activated the “Code Snippets” plugin let’s add code snippets.

Adding a Code Snippet

To add a new code snippet, follow these steps:

  1. Go to Snippets > Add New in the WordPress dashboard.
  2. Give your snippet a title that describes its purpose.
  3. In the “Code” section, paste the code snippet you want to add.
  4. You can add a description of the snippet in the “Description” section if you wish.
  5. Click on the “Save Changes and Activate” button.

Your new code snippet will now be added to the WordPress site.

Example 1: Hiding the Admin Bar for non-admins 

The admin bar is a feature in WordPress that allows users to access important functions such as creating a new post or page. However, you may want to hide the admin bar for certain user roles or for all users.

To hide the admin bar, follow these steps:

  1. Go to Snippets > Add New in the WordPress dashboard.
  2. Give your snippet a title such as “Hide Admin Bar.”
  3. In the “Code” section, paste the following code:
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
  show_admin_bar(false);
}
}

This code will remove the admin bar from the WordPress site.

  1. Click on the “Save Changes and Activate” button.

Now the admin bar will be hidden on your WordPress site.

Example 2: Customizing the WordPress Login

The default WordPress login page may not match the branding or design of your website. You can customize the WordPress login page by adding a code snippet.

To customize the WordPress login, follow these steps:

  1. Go to Snippets > Add New in the WordPress dashboard.
  2. Give your snippet a title such as “Customize WordPress Login”.
  3. In the “Code” section, paste the following code:
function custom_login() {
    echo '<link rel="stylesheet" type="text/css" href="'.get_stylesheet_directory_uri().'https://cdn.nahnucloud.com/custom-login.css" />';
}
add_action('login_head', 'custom_login');

This code will add a custom CSS file to the WordPress login page.

  1. Create a new file named “custom-login.css” in your theme directory.
  2. Add the CSS code for customizing the login page in the “custom-login.css” file. For example, you can add the following code to change the background color of the login page:
body.login {
    background-color: #f7f7f7;
}
  1. Upload the “custom-login.css” file to your theme directory.
  2. Click on the “Save Changes and Activate” button.

Now, the WordPress login page will have a custom background color.

Example 3: Modifying WooCommerce User Name creation Functionality

WooCommerce is a popular plugin for creating e-commerce websites on WordPress. However, The default display name will be their “username” and not real name. To change that, we can add the following code:

  1. Go to Snippets > Add New in the WordPress dashboard.
  2. Give your snippet a title that describes its purpose.
  3. In the “Code” section, paste the code snippet for modifying WooCommerce functionality. For example, to change the number of related products displayed on the product page, you can use the following code:
// change default display name format
add_action('user_register', 'registration_save_displayname', 1000);
function registration_save_displayname($user_id) {
    if ( isset( $_POST['first_name'])){
		$pretty_name = $_POST['first_name'];
		wp_update_user( array ('ID' => $user_id, 'display_name'=> $pretty_name) ) ;
	}
}

/**
* Format WordPress User's "Display Name" to Full Name on Login
* ------------------------------------------------------------------------------
*/

add_action( 'wp_login', 'wpse_9326315_format_user_display_name_on_login' );

function wpse_9326315_format_user_display_name_on_login( $username ) {
    $user = get_user_by( 'login', $username );

    $first_name = get_user_meta( $user->ID, 'first_name', true );
    $last_name = get_user_meta( $user->ID, 'last_name', true );

    $full_name = trim( $first_name . ' ' . $last_name );

    if ( ! empty( $full_name ) && ( $user->data->display_name != $full_name ) ) {
        $userdata = array(
            'ID' => $user->ID,
            'display_name' => $full_name,
        );

        wp_update_user( $userdata );
    }
}

This code will change the number of related products displayed on the product page to 6.

  1. Click on the “Save Changes and Activate” button.

The number of related products displayed on the product page will be changed to 6.

Conclusion

Code snippets can be a powerful tool for adding custom functionality to your WordPress site. However, using a plugin like “Code Snippets” is important to manage your code snippets safely and effectively. This article discussed using the “Code Snippets” plugin to add code snippets to your WordPress site. We also provided examples of code snippets for hiding the admin bar, customizing the WordPress login, and modifying WooCommerce functionality. Using these examples, you can add custom functionality to your WordPress site using code snippets.

Frequently Asked Questions (FAQs)

  1. What are code snippets in WordPress?

    Code snippets are small pieces of code that can be added to a WordPress website to modify or add functionality.

  2. How do I use code snippets in WordPress?

    Code snippets can be added to WordPress using a plugin like Code Snippets or manually adding code to the functions.php file in the WordPress theme.

  3. What are some common uses for code snippets in WordPress?

    Common uses for code snippets in WordPress include adding custom functionality, modifying default WordPress behavior, and integrating with third-party services.

  4. Do I need coding knowledge to use code snippets in WordPress?

    Some coding knowledge is recommended to use code snippets in WordPress, as mistakes in code can cause errors or break website functionality.

  5. Can code snippets cause issues with WordPress themes or plugins?

    Yes, poorly written or conflicting code snippets can cause issues with WordPress themes or plugins, so it is important to test code snippets thoroughly before adding them to a live website.

  6. Are there any risks to using code snippets in WordPress?

    There is always some risk when adding code snippets to WordPress, as mistakes in code can cause website errors or security vulnerabilities.

  7. Can code snippets be used to improve website performance?

    Code snippets can improve website performance by optimizing code, caching content, and reducing server load.

  8. How do I find code snippets for WordPress?

    WordPress code snippets can be found through online forums, blogs, and resources and by working with experienced developers.

  9. Can I create my own code snippets for WordPress?

    Yes, experienced developers can create their own code snippets for WordPress to add custom functionality or modify existing features.

  10. Is it recommended to use code snippets for WordPress customization?

    Using caution and thorough testing when using code snippets for WordPress customization is recommended, as code mistakes can cause website functionality issues. Working with experienced developers or using a trusted plugin can help mitigate these risks.

Discover More

Start typing to see posts you are looking for.