How to Modify Viewport Meta Tag in WordPress & Divi for Accessibility

How to Modify Viewport Meta Tag in WordPress & Divi for Accessibility

Share
Google warns in accessibility for viewport meta tag on Google pagespeed report

Google Chrome Developer Best Practice Audit Link: https://developer.chrome.com/docs/lighthouse/best-practices/viewport

Introduction:

Ensuring your WordPress site is accessible is essential for reaching a broad audience, including users with visual impairments. One crucial aspect of accessibility is the viewport meta tag, which controls how your site is displayed on different devices. In this guide, we’ll show you how to modify the viewport meta tag in the Divi theme according to Google’s accessibility standards, ensuring your site is both responsive and user-friendly.

Why You Need a Child Theme:

Before you start making changes to your Divi theme, it’s important to use a child theme. A child theme allows you to customize your site without altering the parent theme’s core files. This way, your changes won’t be lost when Divi is updated.

How to Create a Child Theme:

1. Create a New Folder:

  • Navigate to wp-content/themes/ in your WordPress directory.
  • Create a new folder, e.g., divi-child.

2. Create a style.css File:

  • In the divi-child folder, create a style.css file.
  • Add the following code:
/*
Theme Name: Divi Child
Template: Divi
*/

3. Create a functions.php File:

  • In the same divi-child folder, create a functions.php file.
  • Add the following code to enqueue the parent theme’s styles:phpCopy code
<?php
function divi_child_enqueue_styles() {
    wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');
}
add_action('wp_enqueue_scripts', 'divi_child_enqueue_styles');

4. Activate the Child Theme:

  • Go to your WordPress dashboard, navigate to Appearance > Themes, and activate the Divi Child theme.

Finding and Modifying the Viewport Meta Tag in Divi:

The viewport meta tag in Divi is usually added via the functions.php file. Here’s how to find and modify it.

Step 1: Locate the Viewport Meta Tag in Divi

  1. Access the Theme Editor:
    • In your WordPress dashboard, go to Appearance > Theme File Editor.
    • Select the functions.php file from the Divi theme.
  2. Find the Viewport Meta Tag Function:
    • Look for the following function:
function et_add_viewport_meta(){
    echo '<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />';
}
add_action('wp_head', 'et_add_viewport_meta');

Step 2: Remove and Replace the Viewport Meta Tag

Step 2: Remove and Replace the Viewport Meta Tag

1. Remove the Existing Function:

  • In your child theme’s functions.php file, add the following code to remove the existing viewport function:
function remove_divi_viewport_meta() {
    remove_action('wp_head', 'et_add_viewport_meta');
}
add_action('init', 'remove_divi_viewport_meta');

2. Add a New Viewport Meta Tag:

  • Next, add the new, optimized viewport meta tag:
function custom_add_viewport_meta(){
    echo '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
}
add_action('wp_head', 'custom_add_viewport_meta');
    Fixing the viewport meta tag using Google's guidelines
    Green message after fixing the initial-scale problem with Google

    FAQs:

    Q: Why is the viewport meta tag important?

    A: The viewport meta tag controls how your website is displayed on different devices, ensuring it’s responsive and accessible. An improperly configured tag can prevent users from zooming in, which is crucial for those with visual impairments.

    Q: What is a child theme, and why should I use one?

    A: A child theme allows you to customize your WordPress site without affecting the parent theme. This ensures that your customizations remain intact even after updates.

    Q: How do I know if my viewport meta tag is optimized?

    A: You can test your website’s accessibility using tools like Google Lighthouse or WAVE. These tools will identify if your viewport meta tag is causing issues.

    Q: What happens if I don’t remove the old viewport meta tag?

    A: If you don’t remove the old tag, both the old and new tags might conflict, leading to unpredictable behavior on different devices.

    Last Words:

    Optimizing the viewport meta tag in Divi is a simple yet effective way to improve your website’s accessibility. By following this guide, you’ve learned how to safely implement these changes using a child theme, ensuring your site is both user-friendly and future-proof.

    Remember, accessibility isn’t just a best practice—it’s essential for creating an inclusive web. If you have any questions or need further assistance, feel free to reach out or leave a comment below!

    Learn More About Divi