Building a WordPress Theme From Scratch
This is a finished example of a WordPress app featuring a custom built theme where the theme is built using JS and CSS from the Bootstrap v4. Instructions provided are specific to using Mac ecosystem. I tried to provide relevant links to explanations for Windows users regarding WAMP vs MAMP, but some setup steps may differ. However, all PHP, HTML, CSS, JS code for creating the theme is platform independent and will be the same for both Mac and Windows users.
Setup Local Server and Install WordPress
- Download and install a code editor (if necessary) such as Atom.
- Download an app to spin up a local PHP Apache Server such as MAMP for Mac or WAMP for Windows.
- Download Wordpress from wordpress.org
- Open MAMP (or WAMP) App and click Start Servers. Please note: The following steps assume MAMP on Mac.
- When your browser opens under MySQL heading click the phpMyAdmin link.
- After logging into phpMyAdmin in the left hand navigation panel click new and under Create Database, name your database something, I called mine
wp_dev_db(make sure there are no spaces in the name you choose). Then click Create. Then in the top navigation click Start. Let's leave this browser tab open and refer to the MySQL database details from here later on. - Move into the downloaded WordPress folder using Terminal $
cd ~/Downloads/wordpressor optionally you can rename the folder first if you like, I renamed mine building-a-wordpress-theme-from-scratch and moved it to my Development folder, I then ran the command $cd ~/Development/building-a-wordpress-theme-from-scratch. - Rename wp-config-sample.php to wp-config.php with $
mv wp-config-sample.php wp-config.php. - Open the site in your code editor I'm using Atom with the command $
atom . - Open wp-config.php and set the database details to point to your local server the following are the instructions for MAMP on Mac directions for Windows may vary at this step so if your own windows instead refer to the instructions for setting up and connecting to databases on WAMP. Here are the MAMP specific code using the details from the MAMP page we left open in our browser:
// ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define('DB_NAME', 'wp_dev_db');
/* MySQL database username / define('DB_USER', 'root');
/* MySQL database password / define('DB_PASSWORD', 'root');
/* MySQL hostname / define('DB_HOST', 'localhost:8889');
/* Database Charset to use in creating database tables. / define('DB_CHARSET', 'utf8');
/* The Database Collate type. Don't change this if in doubt. / define('DB_COLLATE', '');
11. Then visit [https://api.wordpress.org/secret-key/1.1/salt/](https://api.wordpress.org/secret-key/1.1/salt/) and copy the generated keys and paste them into your __wp-config.php__ file under the section labeled Authentication Unique Keys and Salts.
12. Also in __wp-config.php__ set debug from false to true `define('WP_DEBUG', true);`.
13. Then back to MAMP (WAMP instructions instead see their docs or forum) App click Stop Servers. Then click Preferences.
14. Click Web Server tab and set the document root to point to the folder for your WP (WordPress) Site. On Mac in MAMP you click the folder icon with three dots on it to browse for the root folder. It is important that it is set to the folder that contains the wp-config.php file. than click OK. Then click to Start Servers again.
15. In your browser set the URL to `http://localhost` if everything was done correctly this should forward to `http://localhost/wp-admin/install.php`.
16. Select your desired install language and click Continue.
17. On the next page set a Site Title I choose `WP Theme From Scratch`. Also set a username typically this is `admin`. Then set a password you will remember. Also set an admin email. Then click Install WordPress button.
18. On the next page click Log In and use your credentials to login to your site. If all goes well you should see the admin dashboard for your site.
19. Run any updates it suggests under Dashboard > updates
## Creating A Custom Theme
1. In Terminal (or in code editor) head to the theme folder at __wp-content/themes/__ $ `cd wp-content/themes`.
2. Create a new theme folder using Terminal $ `mkdir custom_theme` or via Atom by right clicking on the themes folder and select create new folder. Note: you can name your theme folder anything you like I choose custom_theme as a generic name for this example.
3. Inside __custom_theme__ create a __css__ folder $ `mkdir custom_theme/css` and also create a _js__ folder $ `mkdir custom_theme/js`.
4. Move into your custom theme folder $ `cd custom_theme` and create three files __index.php__ and __style.css__ and __js/app.js__$ `touch index.php style.css js/app.js`. Note that we did not save style.css inside the css folder, although we will be putting Bootstrap css in our css folder later on, WordPress requires a style.css file to be located inside the root folder in order to gather theme details.
5. In __style.css__ in your code editor add the following code:
```css
/*
Theme Name: Custom Theme
Theme URI: https://github.com/jongrover/building-a-wordpress-theme-from-scratch
Author: Jonathan Grover
Author URI: https://jonathangrover.com
Description: Example Theme from Github Repo
Version: 0.0.1
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: bootstrap
Text Domain: jongcustomtheme
This theme, like WordPress, is licensed under the GPL.
Use it to make something cool, have fun, and share what you've learned with others.
*/
h1 {
color: red;
}
Note: You can change any of the settings in the CSS comment at the top as you wish these will eventually appear in your admin dashboard view under Appearance>Themes as details for each theme.
-
In js/app.js in your code editor add the following code:
console.log('Hello from your theme!'); -
In index.php in your code editor put the code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title><?php echo get_bloginfo('name'); ?></title> <link rel="stylesheet" href="<?php echo get_bloginfo('template_directory'); ?>/style.css"> </head> <body> <h1><?php echo get_bloginfo('name'); ?></h1> <script src="<?php echo get_bloginfo('template_directory'); ?>/js/app.js"></script> </body> </html>Note that in our title element we put
<title><?php echo get_bloginfo('name'); ?></title>this uses the get_bloginfo() function to print the name of our site that we first set when we installed WordPress. We also printed this site name in the heading inside our page body. We used this function again to link to our CSS and JS files by making use of<?php echo get_bloginfo('template_directory'); ?>which was essential for printing the correct file path to our assets. For full details of what can be done with this function take a look at the docs here. -
Now head back to the admin dashboard in your browser and click Appearance>Themes
-
Under Custom Theme (or whatever your theme name you set was) click Activate.
-
Then visit
localhostin your browser to see your site. It should say your site title in an h1 element and the loaded CSS should style the text color as red. Additionally if you view the JavaScript console using the Developer Tools you should see it print "Hello from your theme!". -
Now that our theme is working let's delete the previous theme folders (you don't have to do this, but I won't be using the default themes any longer and they are just taking up space.) I'll get rid of twentyseventeen, twentysixteen, and twentyfifteen.
Adding Bootstrap
- Start by downloading Bootstrap.
- Next download jQuery a Bootstrap dependency.
- Also download Popper another Bootstrap dependency.
- After all downloads finish unzip them and move all CSS into your projects css folder and move all JS files into your js folder.
- Now let's link to these new files for Bootstrap in our index.php file in the root of our theme folder __custom_theme/index.php. Here We'll link to the files the same way we linked to our style.css and app.js__.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title><?php echo get_bloginfo('name'); ?></title> <link rel="stylesheet" href="<?php echo get_bloginfo('template_directory'); ?>/css/bootstrap.min.css"> <link rel="stylesheet" href="<?php echo get_bloginfo('template_directory'); ?>/style.css"> </head> <body> <h1><?php echo get_bloginfo('name'); ?></h1> <script src="<?php echo get_bloginfo('template_directory'); ?>/js/jquery-3.2.1.slim.min.js"></script> <script src="<?php echo get_bloginfo('template_directory'); ?>/js/popper.min.js"></script> <script src="<?php echo get_bloginfo('template_directory'); ?>/js/bootstrap.min.js"></script> <script src="<?php echo get_bloginfo('template_directory'); ?>/js/app.js"></script> </body> </html> - Save all files and head to localhost in your browser, open the Network tab in the Developer Tools and refresh the page. You should see Bootstrap CSS, JS, jQuery, and Popper are all correctly loading.
Dividing Page Layout
- Let's start by building our layout in __custom_theme/index.php__ and later we'll break it into separate PHP files. Here is my layout code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title><?php echo get_bloginfo('name'); ?></title> <link rel="stylesheet" href="<?php echo get_bloginfo('template_directory'); ?>/css/bootstrap.min.css"> <link rel="stylesheet" href="<?php echo get_bloginfo('template_directory'); ?>/style.css"> </head> <body>
<section class="col-md-9">
<div class="jumbotron">
<h1 class="display-3">Welcome</h1>
<p class="lead">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<hr class="my-4">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</section>
<aside class="col-md-3">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
</aside>
</div><!-- .row -->
</div><!-- .container -->