Adding a custom post type to Wordpress using NanoSoup/Nemesis

AuthorJez R. Emery
Date
31. August 2021
Time1 min read

Nemesis is a simple plugin provided by the NanoSoup group which aids in the rapid setup and customisation of custom post types within Wordpress without the need for much Wordpress specific knowledge.

Setting up the plugin

Create a new folder in the Wordpress plugin directory.

$ mkdir location-post-type

Enter the directory.

$ cd location-post-type

The install Nemesis as a dependency using composer

$ composer install nanosoup/nemesis

Create your root plugin file location-post-type.php

You'll need to make this file contain the following.

<?php
/*
Plugin Name: Location Post Types
Plugin URI: https://www.hiohzo.com/
Description: Custom post type for Locations
Version: 1.0.0
Author: Jez Emery (Hiohzo)
Author URI: https://www.hiohzo.com/
*/

use NanoSoup\Nemesis\PostTypes\PostTypes;

require_once __DIR__ . '/vendor/autoload.php';

Once this is done, the barebones of the plugin are set up.

Configuring your custom post type

Nemesis allows for a LOT of customisation, however in this article we'll cover the basics of setting up a simple post type.

Edit your location-post-type.php file to add the following.

function create_posttype_locations()
{
    if (!function_exists('register_post_type')) { return false; }

    (new PostTypes())->registerPostType('location', 'Location', 'Locations', 'location', [
        'hierarchical' => true,
        'menu_icon' => 'dashicons-location'
    ]);
}
add_action('init', 'create_posttype_locations');

You'll likely notice at this point, if you enable the plugins, the URLs do not currently work as expected. You will probably experience them all 404ing. This is because you need to flush permalinks in Wordpress. You can do this via the Admin CMS, however a better idea is to flush them when you enable the plugin.

This can be done with the following.

function location_post_type_activate()
{
    flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'location_post_type_activate');