Category: WordPress

Creating a Child Theme in WordPress

When you are working with theme customization in WordPress, it is a good idea to create a child theme of the theme you want to start with. A child theme will look and feel like its parent theme, but will allow changes that you make to be retained when the parent theme is updated.

The first step to creating a child theme is to navigate to your themes directory located at /wp-content/themes. Once there, create a new directory for your child theme. You can call it whatever you want, but it is common to name it after the parent theme and append -child to the directory name.

Once your directory is created, you need to create a stylesheet in the directory and name it style.css. This stylesheet should contain the header comment below at the top of the file:

/*
 Theme Name:   Twenty Nineteen Child
 Theme URI:    http://example.com/twenty-nineteen-child/
 Description:  Twenty Nineteen Child Theme
 Author:       Your Name
 Author URI:   http://example.com
 Template:     twentynineteen
 Version:      1.0.0
 License:      GNU General Public License v2 or later
 License URI:  http://www.gnu.org/licenses/gpl-2.0.html
 Tags: one-column, flexible-header, accessibility-ready, custom 
 colors, custom-menu,   custom-logo, editor-style, featured-images,         
 footer-widgets, rtl-language-support, sticky-post, threaded- 
 comments, translation-ready
 Text Domain:  twentynineteenchild
*/

It’s important that the Template matches the directory name of your parent theme. Other things to watch out for are to make sure that you use the correct theme tags. Allowable theme tags are specified by WordPress.

The style.css theme is the only file that is required for the child theme, but you will need to add a functions.php file in order to enqueue styles correctly. The contents of that file should look something like this:

<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
 
    $parent_style = 'parent-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.
 
    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( $parent_style ),
        wp_get_theme()->get('Version')
    );
}

Once those files are in place you can activate your child theme in your sites Administration Screen.

Using a Child Theme

Once your child theme is in place, you can copy over any template files you want to modify and make the changes within your child theme directory where they won’t be affected by updates to the parent. Additionally you can add CSS and PHP functions to the style.css file and functions.php within your child theme.


Installing WordPress with WP-CLI

I’ve been wanting to get a blog set up for a while now to start documenting my progress as I learn about Web development. Mission accomplished? Here’s how I did it.

My first step was choosing a domain name. Always tricky. I settled on adventuresinwebdev.com and registered it. I don’t use my hosting provider as a registrar so I had to point the new domain to my providers DNS servers.

Once all that was taken care of, I logged into my web server via SSH and got to work installing WordPress. My provide provides a handy GUI tool for WordPress installations, but I generally prefer to use the CLI when I can. My hosting provider has WP-CLI set up on my server so the installation process is super easy.

I navigated to the folder where I wanted to install WordPress and entered the following command to download the WordPress core files:

wp core download

Then I logged in to the cPanel for my server and set up a new MySQL database for the WordPress installation.

Once the database was configured, I moved on to setting up the WordPress config file. Once again, WP-CLI makes this simple with the following command:

wp config create --url=domainNameGoesHere --dbhost=localhost
--dbname=databaseNameGoeshere --dbuser=databaseUserName
--dbpass=userPassword

If you are following along, you’ll want to insert your database information in the relevant fields.

The last step is to execute the WordPress installation.

wp core install --url=yourDomainName --title="Your site title" 
--admin_name=yourAdminUserName --admin_password=yourAdminPassword  
--admin_email=yourAdminEmailAddress 

Again, you’ll need to insert your information in the appropriate fields.

That’s it! WordPress has been installed. Congrats!