Developer Snippet Diary

How to create a basic WordPress theme programmatically using PHP.

Creating a basic WordPress theme programmatically involves several steps. I'll guide you through the process with examples and code. Remember, WordPress themes are essentially a collection of PHP, HTML, CSS, and optionally JavaScript files that define the appearance and layout of your website.

1.Setting Up the Theme Directory
First, create a new directory in your WordPress installation's wp-content/themes folder. Name it as you like, for example, mytheme.

2.The Style.css File
Create a style.css file in your theme directory. This file must contain the theme header which tells WordPress about your theme:

/*
Theme Name: My Theme
Author: Your Name
Description: A description of your theme.
Version: 1.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: my-theme
*/

3.The index.php File
This is the main file of your WordPress theme. Create an index.php file in your theme directory:

<?php get_header(); ?>
<h1>Welcome to My WordPress Theme</h1>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>
<?php endwhile; endif; ?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

4. The functions.php File
This file acts as a plugin and is used to add features and functionality to a WordPress theme. Create a functions.php file in your theme directory:

<?php
function mytheme_enqueue_styles() {
    wp_enqueue_style('main-css', get_stylesheet_uri());
}
add_action('wp_enqueue_scripts', 'mytheme_enqueue_styles');
?>

5. The header.php and footer.php Files
Create a header.php file:

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>

6.footer.php

    <?php wp_footer(); ?>
</body>
</html>

Optional Files
sidebar.php: For sidebars.
header.php
footer.php
single.php: For single posts.
page.php: For single pages.
archive.php: For archive pages.

7. Activate theme

OR
YOU CAN DOWNLOAD The Underscores is a starter theme, you download it from here and do whatever you want. It gives you almost anything which is needed for a good base theme.
https://underscores.me/

Posted by: R GONDAL
Email: rizikmw@gmail.com