To create a modern WordPress custom theme with a widget, follow these steps:
- Install WordPress and set up a local development environment.
- Create a new folder in the “wp-content/themes” directory and give it a descriptive name (e.g. “my-custom-theme“).
- In the new folder, create a new file called “style.css” and add a comment block at the top with your theme’s name, author, and other information.
- Create a new file called “functions.php” and add the following code to register your widget area:
// Register widget area
function my_custom_theme_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'Sidebar', 'my-custom-theme' ),
'id' => 'sidebar-1',
'description' => esc_html__( 'Add widgets here.', 'my-custom-theme' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'my_custom_theme_widgets_init' );
- Create a new file called “header.php” and add the code for your theme’s header. This should include the opening <html>, <head>, and <body> tags, as well as any other elements you want to include in the header (e.g. navigation menu, logo, etc.).
- Create a new file called “footer.php” and add the code for your theme’s footer. This should include the closing </body> and </html> tags, as well as any other elements you want to include in the footer (e.g. copyright notice, social media links, etc.).
- Create a new file called “index.php” and add the code for your theme’s home page. This should include the header and footer files, as well as any other content you want to display on the home page (e.g. blog posts, featured images, etc.).
- To display the widget area in your theme, add the following code to the relevant template file (e.g. “index.php”, “single.php”, etc.):
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<div id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</div>
<?php endif; ?>
- Add any additional template files and custom styles as needed to complete your theme.
- Activate your theme in the WordPress dashboard and start adding widgets to your widget area.
- Designing a WordPress theme
- Building a WordPress theme from scratch
- Customizing a WordPress theme
- Creating a child theme in WordPress
- Modifying a WordPress theme’s code
- Developing a WordPress theme with CSS and PHP
- Designing a responsive WordPress theme
- Creating a custom homepage in WordPress
- Building a custom navigation menu in WordPress
- Adding custom functions to a WordPress theme
See More posts:
How To create custom theme in wordpress
Did you know the best PHP objects with examples?
How do I start learning about bug finding and start my journey of bug bounty programs?