How to Edit HTML in WordPress

Published on July 20, 2023 by Simon Wright

Do you wish you had more control over your WordPress website’s appearance and functionality? Well, you can because editing the HTML code in WordPress can give you the flexibility to deeply customize different areas of your site. So, whether you want to tweak the design of individual posts or pages, modify your theme’s source code, or add custom CSS, this article will guide you through the process. This step-by-step guide will help unleash your creativity by showing you various methods to edit HTML in individual WordPress posts and pages safely.

Ready to learn how to edit HTML in WordPress? Great, so let’s begin!

A. Editing WordPress HTML for Individual Posts/Pages

If you want to make specific changes to the HTML code of individual posts, pages, or other post types in WordPress, there are simple ways to access and edit the source code. Let’s explore how you can achieve this in the block and classic editors.

Editing HTML in the Gutenberg Block Editor

In the default WordPress content editor, Gutenberg, you have multiple options to work with HTML. If you only want to add your own HTML code to a piece of content, using the dedicated “Custom HTML” block is recommended. Here’s how:

  1. Add a new block by hitting the “+” icon.
  2. In the “Search for a block” field, type “Custom HTML” and select the Custom HTML block.
  3. Next, paste (or type) your desired HTML code into the block where it says “Write HTML…
  4. Finally, preview your changes and publish the post or page in the usual way.

However, there may be instances where you need to edit the HTML of a specific block in a page or post, for example, if you want to add a nofollow tag to a link. Here are two ways to do this:

    1. Click on the block’s options (the three vertical dots) and choose “Edit as HTML.”
    2. Edit the HTML code for the individual block. Once you have finished editing, click “Edit visually” to return to the block editor.

Alternatively, you can access the “Code editor” from the main tools and options dropdown (accessed by clicking the three dots located in the right-hand corner) to edit the HTML for the entire post. However, keep in mind that the full code editor requires working around and preserving the markup for blocks.

Once your HTML edits are complete, click “Exit code editor” to return to the block editor.

Editing HTML in the Classic Editor

If you’re still using the original classic WordPress editor (possibly using a plugin such as Classic Editor), you can still edit HTML for entire posts by following these steps:

  1. Open the post or page in the editor.
  2. Switch to the “Text” tab at the top right corner.
  3. Locate the HTML code you wish to edit and make the necessary changes.
  4. To preview the changes and revert to the visual editor, click on the “Visual” tab.

If you happen to be using the classic paragraph block provided by the Advanced Editor Tool (previously called TinyMCE) plugin, the steps to follow are slightly different:

  1. Go to the paragraph which you wish to edit the HTML for.
  2. Click on “Tools” and then select “<> Source code.”
  3. A popup window will appear containing the HTML for that paragraph. You can make your changes here and click “OK” when you are done.

B. Editing the Source Code in Your WordPress Theme

If you are considering editing your WordPress theme’s source code, there are some important considerations to remember. For example, making direct code edits to the parent theme can lead to issues because subsequent theme updates may overwrite the changes. Moreover, you may introduce conflicts with the theme and installed plugins.

To avoid this, let’s explore how you can edit your theme’s code safely.

Using a Child Theme for Code Edits

To directly edit the code in your theme’s files, it’s crucial to use a child theme rather than modifying the “parent” theme. This ensures that your code changes won’t be lost when you update the theme. Creating a child theme involves the following steps:

  1. Create a new folder for your child theme in the wp-content/themes/ directory.
  2. Add a style.css file in the child theme folder and define its details.
  3. Create a functions.php file in the child theme folder and enqueue the parent and child theme stylesheets.
  4. Customize the code in the child theme’s files instead of in the parent theme.

Using a child theme provides a safe and efficient way to make code edits without the risk of losing your changes during updates. For more details, the Developer Resources section of the WordPress documentation contains comprehensive details on creating child themes, plus there are plenty of other useful resources available on the internet, including YouTube tutorials.

Consider Using Plugins for Code Snippets

If you only need to add code snippets to your theme, such as tracking scripts or custom functions, using plugins can be a simpler and more manageable approach.

Plugins like Head, Footer and Post Injections or Code Snippets allow you to add snippets without directly editing your theme’s files. These plugins provide segregated interfaces to manage and organize your code snippets safely and effectively.

By using plugins, you can inject code snippets into specific sections like the <head> or the functions.php file of your theme. This approach ensures that your snippets remain intact even when updating your theme. The only downside is you are adding another plugin to your site, which can be an issue if you already have several others.

Using the In-Dashboard WordPress Code Editor

If you decide to edit your theme’s code directly, creating a backup of your site before making any modifications is essential. After doing that, you can use the WordPress in-dashboard code editor to edit your theme’s code without leaving your WordPress admin area.

To access the in-dashboard code editor, follow these steps:

  1. Select “Appearance” from the WordPress dashboard main menu.
  2. Click on “Theme File Editor” from the dropdown menu. WordPress will display a warning message, reminding you to proceed with caution. If you are happy to proceed with changing your theme’s files, click “I understand.” You will now see the theme file editor screen.
  3. Make the necessary code edits in the code editor. You can use the sidebar on the right to navigate between the various theme files quickly.
  4. Save your changes by clicking “Update file” and test everything works as intended.

Using the in-dashboard code editor is convenient, but exercising caution while making edits is crucial. Remember to create backups and double-check your changes to avoid any unintended consequences.

Using SFTP to Edit Code in WordPress

An alternative method to edit your theme’s files is by using SFTP (SSH File Transfer Protocol). This approach allows you to use your preferred code editor and provides the advantage of immediate troubleshooting if something goes wrong.

To edit code via SFTP, follow these steps:

  1. Connect to your WordPress site via an SFTP client.
  2. Navigate to the theme’s folder (e.g., /wp-content/themes/name-of-child-theme).
  3. Find the file you want to edit, right-click on it, and choose your code editor’s file edit option.
  4. Make the necessary changes and save the file.
  5. Re-upload the modified file to your WordPress site.

Remember to download an original copy of the file before making any modifications. This way, you can easily revert to the original version if things don’t go according to plan.

C. Adding Custom CSS to WordPress

If you want to enhance the appearance of your WordPress site without touching the HTML or PHP code, adding custom CSS is a powerful solution. Custom CSS allows you to override the default styles and bring your own unique design ideas to life. Let’s explore different methods to add custom CSS to your WordPress site.

Using the WordPress Customizer

The WordPress Customizer is an intuitive interface that allows you to customize various aspects of your site, including adding custom CSS. This approach offers real-time previews of your changes, enabling you to fine-tune your CSS code. Here’s how to add custom CSS using the WordPress Customizer:

  1. Go to “Appearance” in your WordPress dashboard and click “Customize” to open the WordPress Customizer.
  2. Look for the “Additional CSS” option within the Customizer and click on it.
  3. Add your desired CSS code in the code editor – you will see the changes reflected in real-time. Once you are happy with the changes, click “Publish” to save them.

The WordPress Customizer is a simple, convenient, and relatively safe way to add custom CSS to your website, allowing you to experiment with different styles and instantly see the results.

Using the Simple CSS Plugin

If you prefer to manage your custom CSS outside of the WordPress Customizer, you can use a plugin such as Simple Custom CSS and JS. Such plugins offer a similar code editor within the WordPress Customizer, plus they allow you to add custom CSS to individual posts or pages via a meta box. Moreover, these plugins help streamline your CSS management and provide greater flexibility.

Creating a Custom CSS Stylesheet

Creating a custom CSS stylesheet is recommended for more extensive CSS modifications, such as where hundreds or thousands of lines of code are needed.

We do not recommend this approach unless you have considerable coding knowledge, as it involves creating a separate CSS file and enqueuing it in your theme. Using the wp_enqueue_scripts function will ensure your custom CSS loads on your site’s front end. While considerably more complex, this method does provide a cleaner and more organized way to manage large CSS files.

Summary

This article explored various methods to edit HTML in WordPress and add custom CSS to your website.

When it comes to editing HTML for individual posts or pages, both the default block editor (Gutenberg) and the classic editor offer easy ways to make changes. However, if you want to modify your WordPress theme’s source code, using a child theme is essential to safeguard your changes during theme updates. That said, using plugins for code snippets or the in-dashboard code editor can simplify the process and ensure easier management. Finally, if you want to enhance your site’s appearance with custom CSS, the WordPress Customizer, the Simple CSS plugin, or creating a custom CSS stylesheet provide flexible options to suit your needs.

The simple steps detailed in this article will allow you to edit HTML, PHP, and CSS in WordPress confidently. In the process, you will unlock endless new possibilities and create a WordPress website that truly reflects your unique style and vision.

On a final note, you can save time and costs, maximize your site’s performance, and unleash your creativity with WP Bolt’s WordPress Virtual Private Server (VPS) hosting. VPS hosting offers superior performance, reliability, and security compared to the shared hosting offered by most other providers. Moreover, it is highly affordable, with the Starter Server package costing just $15 per month, and all plans come with 500 email accounts, free migrations, and 24/7 technical support.

Stop leaving money on the table.

Speed up your WordPress site today by moving to WP Bolt.

Get Started