SSL Certification and HTTPS

I have to sing the praises of digital ocean and certbot. I wanted to serve my wordpress site hosted at digitalocean.com on HTTPS. Because it is WordPress it runs on an Apache server and through Digitalocean’s easy one click app creation the cloud server is running Ubuntu 18.04.

Where it gets really awesome is that when I connected to my server via SSH and then looked at the instructions on the “Certbot” webpage for obtaining an SSL certificate for Apache on Ubuntu 18.04 it was the smoothest process I could have imagined. It was only a few commands on the command line and Certbot had not only installed an SSL cert on my server but it even asked me if I wanted my Apache setting changed so that traffic was automatically served on HTTPS, and it was as easy as answering yes. Wow, that’s a really great system. Oh yeah, did I mention it’s free? Well the certificate part at least, but Digital ocean is very reasonable, only $5 a month for starters.

PHP Include

When working with WordPress a great way to modularize your code is to use PHP include. There is a built-in WordPress function called get_template_part(). However, PHP variables do not pass between the child template part and the parent template the function is called in. If you use PHP’s include then you can share variables. For example:

Parent Template

<?php
include 'template-part.php';
echo $example-variable; // this will print out
?>

template-part.php

<?php
$example-variable = 'hello world!';
?>

CORS Headers

If you have tried to use the WordPress API and run into the issue that clients cannot access your API because of the error message “no access-allow-origin headers” AKA CORS Headers, the solution follows.

This is a very simple fix, wordpress has a built in function that will send these headers for you. Just add the following to your functions.php

add_action( 'rest_api_init', 'rest_send_cors_headers');
The function is documented here.

WordPress Loops

Custom Loop

$args = array(
    'posts_per_page' => '-1',
    'post_type' => 'form',
);

$myForms = new WP_Query($args);
while ($myForms->have_posts()){
    $myForms->the_post();

    the_title();
    the_content();
}


Standard loop

if (have_posts()) {
    while (have_posts()) {
        echo '<br>';
        the_title();
        echo '<br>';
        the_post();
        echo '<br>';
        the_time('F j, y');
        echo 'at: ';
        the_time('g:i a');
        echo '<br>';
        the_content();
        echo '<br>';
    }
} else {
    // if no posts
    echo 'there are no posts';
}

Template Hierarchy

WordPress uses temlates to display posts, pages and custom post types. It will decide what template to use by matching up the .php file name with a variety of post attributes. You may need to use flush_rewrite_rules() before a new template will properly register. The templates lower down on the list are less specific, worpress will fall back on these if it doesn’t find a more specific template first.

  • category-[category].php ex: if “news” then category-news.php
  • single-[post-type].php
  • tag[slug].php
  • taxonomy-[taxonomy].php
  • page-[slug].php
  • date.php
  • search.php
  • 404.php
  • home.php
  • front-page.php
  • single.php
  • page.php
  • index.php

WordPress Child Theme

This is what the top of a style.css file for a child theme should look like. This will tell WordPress to look for all files in the child directory first, like functions.php and if they aren’t found there it will look in the parent theme directory.

/*
Theme Name: A twentyseventeen child
Descrition: sample child theme
Author: Josh Chatfield
Template: twentyseventeen
Version: 1.0
*/

@import url('../twentyseventeen/style.css');

WordPress functions.php

<?php

//bundles enqueueing of script and style into one function
function add_josh_styles_and_scripts() {

 wp_enqueue_style( 'josh.css', get_stylesheet_directory_uri() . 
 '/css/josh.css');

 wp_enqueue_script( 'josh.js', get_template_directory_uri() . 
 '/js/josh.js', array(),'1.0', true);
}

// calls above function during the enqueue hook
add_action( 'wp_enqueue_scripts', 'add_josh_styles_and_scripts' );

// bundles enabling and registering menus
function setup_josh_theme() {
    add_theme_support('menus');
    register_nav_menu( 'primary', 'primary menu' );
    register_nav_menu( 'sidebar', 'sidebar menu' );
}

// calls the above function during the init hook
add_action('init','setup_josh_theme');

?>