How to create a WordPress theme? 5 step guide using code

A WordPress theme allows you to have a consistent design for your pages and posts, changing the appearance of the entire website. That said, it could prevent you from having full control over its functionality and design.

Luckily, you can create a WordPress theme from scratch to fully customize your WordPress website however you want. Also, creating your own custom WordPress theme helps reduce code clutter in theme files and improve site performance.

However, you will need to have some basic coding knowledge to create your own theme, as it has to be written in PHP, HTML, CSS, and Javascript. Therefore, this tutorial will show you how to create a WordPress theme using HTML5 and CSS3.

What is a responsive design?

Devices often have different screens, each of which varies in color, functionality, and resolution. Some work in landscape mode, while others work in portrait mode. Also, many devices can toggle between these modes.

For this reason, it is paramount to create a custom WordPress theme that is responsive to various screen resolutions.

Responsive design is all about displaying your website in the best format for visitors based on their screen size. The page layout responds to the available space and the content is positioned correctly: nothing is cut off, overflowing and your website looks as clean as possible.

To measure the responsive alignment of your design, you should think of your website design as a series of grids rather than pixels (px). Then divide the width of the grids for each content section into percentages (%).

In the case of images, you can configure them with the maximum width: 100% of their real size and the height in automatic. The width and height of the image will automatically adjust when the screen size is reduced. So, if you put a 200px wide image inside a Div which occupies 100% of the screen, will remain at its actual width. Here is an example of good and bad Syntax to build a responsive layout:

/* GOOD WIDTH */ .inline-text-box { width: 50%; } /* BAD WIDTH */ .inline-text-box { width: 800px; } /* GOOD IMAGE */ img { max-width: 100%; height: auto; } /* BAD IMAGE */ img { width: 100%; height: auto; }

With this in mind, we are going to apply these responsive web design principles to create a WordPress theme with HTML5 and CSS3.

5 Steps to Create a Responsive WordPress Theme Using HTML5 and CSS3

is the latest version of the HTML web markup language that helps display content consistently on any computer, tablet, or mobile phone.

See also  What is a DKIM record? All you need to know

Additionally, HTML5 offers a viewport meta tag feature that controls the display of your website in each browser with the tag , inside tag >.

To create a fully responsive WordPress theme, you can also use the CSS3 media queries feature. Set specific rules and control how your site will behave on various screen sizes.

Consider installing WordPress locally when developing a theme. This is a great way to experiment with WordPress, without worrying about damaging your website. You also don’t need to have a domain name or web hosting until you want to get online.

Knowing this, let’s get into the steps to create a custom WordPress theme. We’ll apply both the meta viewport tag and the media queries as we build it.

1. Create and store template files

WordPress themes can usually be created using just two template files in the theme directory:

  • index.php: Prepare a template for the theme to display your content.
  • style.css: manages the visual aspect of the theme.

However, developing a WordPress theme will also require various support files to add more flexibility when it comes to customization. Those template files include:

  • header.php: contains any HTML that goes at the top of your pages, starting with >.
  • footer.php: contains the HTML that goes at the bottom of your website, including >.
  • functions.php: add functionality to the theme, from menus and colors to scripts and style sheets.
  • sidebar.php: generates sidebar items.
  • single.php: displays a single post from a particular page.
  • page.php: displays the content of a single page.

As a theme developer, you are free to customize each template file. WordPress will choose and generate templates based on the template hierarchy. It will browse it until it finds a matching file and display the page accordingly. With this in mind, create a new folder for the components inside your folder. topics of wordpress, before creating a WordPress theme from scratch.

pro tip

The folder name must not be the same as another theme. Also, if you plan to share your work, check that there is no theme with the same name on the WordPress.org theme.

Once you have a folder to store your theme, create the essential WordPress template files. Each PHP template file must use the correct name as defined in the WordPress documentation.

Consider that to make different layouts for your posts, pages, and other parts of your website, you have to create separate templates containing HTML5 and PHP, which are applied to each section.

See also  PrestaShop vs Shopify: Comparison of 2 Modern eCommerce Platforms

pro tip

If you want to use custom post types, you can create templates that only apply to posts of that type. However, if you want posts in a specific category to have different styles, use “if-then” statements within the loop.

Now, let’s go to the steps:

  1. Go to the section of File Manager in you . Then go to public_html -> wp-content -> themes folder.
  2. Create a new folder. Please note that the folder must have a unique, descriptive and short name. Do not use numbers or spaces.
  3. We will use “my theme” in this example.
  4. Let’s create the custom templates first, and we’ll focus on the content later. files include header.php, index.php, footer.php, functions.php, sidebar.php, single.php Y page.php

Now that you have all the PHP template files ready, let’s move on to configuring the CSS style sheet.

2. Configure the initial CSS style sheet

They control the visual design and layout of the website pages. Every WordPress theme needs a style sheet to display things like fonts and colors. Without a CSS file, WordPress will not recognize your custom theme as valid.

With this in mind, create a style sheet file in the same folder as the theme. Add the necessary information, including the theme name, version, description, and author to the top of the file style.css. WordPress will display this data in the admin control panel.

The information must be written as a multi-line CSS comment. Each header on its own line, starting with a keyword. Find the full list of header keywords for topics on the page.

Here’s an example:

/* Theme Name: My Theme Author: Author URI: https:///tutoriales/ Description: My first responsive HTML5 theme Version: 1.0 License: GNU General Public License v3 or later License URI: http: //www.gnu.org/licenses/gpl-3.0.html */

pro tip

The last two lines are necessary if you want to share your theme on WordPress.org. If you are going to use the theme for your own WordPress website, you do not need to include them.

At this point, your theme will already be visible in your WordPress dashboard. go to Appearance -> Themes and you will see My theme with a white and gray check box image.

However, you will get a blank home page if you activate the theme. This is because your file index.php is empty and no templates are available. Add a single rule to the CSS file that changes the background color of the page. Insert a blank line after the comment close */ on the tenth line. Then paste the following code below it:

See also  How to fix “The connection is not private” error

* { box-sizing: border-box; } body { background-color: #f9f9f9; font-family: Helvetica; }

Let’s understand this code by breaking it down.

The first entry is an important part of the responsive style. <*> or the asterisk symbol, matches each element of the HTML document. Sets that the final width and height of each element on the page should include the content, padding, and border. Therefore, this rule effectively adds the padding inside the box instead of outside it.

Meanwhile, the second element changes the background color to white and sets the default font to be used in the theme.

Please note that the above code only adds basic elements to your custom theme. We will return to the file style.css once we have configured the other files and the general structure of the website is ready.

3. Make the WordPress theme functional

Before the WordPress theme development process, we recommend adding some code snippets to the files. functions.php Y sidebar.php, to improve the flexibility of the theme. For example, adding areas with widgets or creating on specific pages.

functions.php

Each browser has different default settings for things like margins and page padding. Therefore, you should perform CSS normalization to ensure that your theme has the same style and behavior in all browsers.

To achieve this, include a CSS file called normalize.css in your file functions.php. This file will allow browsers to display your site consistently, regardless of those elements. To create one, insert the following code into functions.php:

This code will make WordPress choose the file normalize.css of the .

Next, activate the sidebar widgets by adding the following code after the above function:

// Register a new sidebar simply named ‘sidebar’ function add_widget_support() { register_sidebar( array( ‘name’ => ‘Sidebar’, ‘id’ => ‘sidebar’, ‘before_widget’ => ‘

‘, ‘ after_widget’ => ‘

‘, ‘before_title’ => ‘

‘, ‘after_title’ => ‘

‘, ) ); } // Hook the widget initiation and run…

Loading Facebook Comments ...
Loading Disqus Comments ...