Where and How HTML is Used in WordPress?
HTML (Hypertext Markup Language) is the backbone of any website, including those built with WordPress. It is essential for structuring content and elements on a web page. In WordPress, HTML is used in various places, both on the front end (what users see) and the back end (admin area). This guide will explore where and how HTML is used in WordPress, providing a comprehensive understanding for beginners and experienced users alike.
1. HTML in WordPress Themes
WordPress themes consist of a collection of template files written in PHP that generate HTML to display different parts of a website. These templates include:
- header.php: Contains the HTML code for the header section of your site, including the
<head>
tag and opening<body>
tag. - footer.php: Contains the HTML code for the footer section, closing
</body>
and</html>
tags. - index.php: The main template file that controls the overall structure of your site.
- single.php: Controls the layout for single posts.
- page.php: Controls the layout for pages.
- sidebar.php: Contains the HTML for the sidebar.
Custom HTML and Template Tags
In these template files, you can add custom HTML and use WordPress template tags to display dynamic content. For example, to display the site title in the header:
<!DOCTYPE html>
<html language_attributes(); >
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<title> bloginfo('name'); </title>
</head>
<body body_class(); >
<header>
<h1> bloginfo('name'); </h1>
<p> bloginfo('description'); </p>
</header>
2. HTML in WordPress Posts and Pages
The Block Editor (Gutenberg) allows users to create rich content using various blocks. Each block generates specific HTML elements. You can also use the Custom HTML block to add raw HTML directly to your posts or pages.
- Open the post or page in the Block Editor.
- Click the
+
button to add a new block. - Search for
Custom HTML
and select it. - Enter your HTML code in the provided text area.
Classic Editor
The Classic Editor provides a Visual editor and a Text editor. The Text editor allows you to add or edit the HTML code directly.
- Open the post or page in the Classic Editor.
- Click the
Text
tab to switch from the Visual editor to the HTML editor. - Enter or edit your HTML code as needed.
Know more: How to Upload an HTML Page to WordPress Without 404 Errors
3. HTML in Widgets
WordPress offers a Custom HTML widget that allows you to add HTML code to widget areas such as sidebars or footers.
- Go to
Appearance ⟶ Widgets
in the WordPress dashboard. - Drag the
Custom HTML
widget to the desired widget area.
Enter your HTML code in the widget’s content area. Click Save
.
4. HTML in Plugins
Many plugins provide shortcodes that generate HTML output. You can also create custom shortcodes that output HTML.
Add the following code to your theme’s functions.php
file or a custom plugin:
function custom_shortcode_function() {
return '<div class="custom-class">This is custom HTML output by a shortcode.</div>';
}
add_shortcode('custom_shortcode', 'custom_shortcode_function');
Add [custom_shortcode]
to any post, page, or widget area to display the custom HTML.
5. HTML in WordPress Customizer
You can add custom HTML, CSS, and JavaScript in the WordPress Customizer for additional customization.
- Go to
Appearance ⟶ Customize
. - Navigate to the
Additional CSS
section to add custom CSS.
For HTML and JavaScript, use the Additional HTML
option (if available) or incorporate it within your theme’s custom sections.
To Sum Up
HTML is a fundamental part of WordPress, used extensively in themes, posts, pages, widgets, plugins, and the customizer. Understanding where and how to use HTML in WordPress enables you to customize your website’s appearance and functionality effectively. Whether you are editing theme files, adding custom content in posts, or creating widgets, mastering HTML in WordPress opens up a wide range of possibilities for your site.