Advertisement
  1. Code
  2. WordPress

Creating Your WordPress Knowledge Base's Structure

Scroll to top
Read Time: 6 min

By this stage you should already have a plan for your knowledge base's structure and any post types and taxonomies you'll need to use to store your data.

The next step is to create the structure for your data. 

In this tutorial I'll take you through:

  • registering taxonomies for your data
  • removing taxonomies you don't need

What You'll Need

To follow this tutorial, you'll need

  • a development installation of WordPress
  • a text editor
  • a starting theme

1. Creating a Starting Theme

As my starting theme I'm going to use a theme I developed for an earlier tutorial on building a WordPress theme. The theme is included in the code bundle for this tutorial. If you prefer you can use your own theme or one that you've downloaded from the theme repository.

Before you get started, if you're using a theme you've downloaded, you'll need to rename its folder and edit the stylesheet to reflect the new use of the theme.

So my stylesheet now has the following at the beginning:

1
/*

2
Theme Name: Creating a Knowledge Base in WordPress - starting theme

3
Theme URI: https://rachelmccollin.co.uk

4
Author: Rachel McCollin

5
Author URI: http://rachelmccollin.co.uk

6
Description: The theme to accompany the wptutsplus series on creating a knowledge base using WordPress. This is the starting theme.

7
Version: 1.0

8
License: GNU General Public License v2 or later

9
License URI: http://www.gnu.org/licenses/gpl-2.0.html

10
*/

2. Registering Post Types and Taxonomies

If your knowledge base makes use of custom post types, then you'll need to register those. You do this by adding the register_post_type() function to your theme's functions.php file. 

My knowledge base will just be using posts with custom taxonomies, so I need to use the register_taxonomy() function.

Open your functions.php file. If you're using my starter theme, it already has some functions in it, which add theme support for featured images and register some widgets. Below the last function, add the following to register the Content Type taxonomy:

1
function tutsplus_taxonomies() {
2
    // Content type taxonomy

3
	$labels = array(
4
		'name'              => __( 'Content Types', 'tutsplus' ),
5
		'singular_name'     => __( 'Content Type', 'tutsplus' ),
6
		'search_items'      => __( 'Search Content Types', 'tutsplus' ),
7
		'all_items'         => __( 'All Content Types', 'tutsplus' ),
8
		'edit_item'         => __( 'Edit Content Type', 'tutsplus' ),
9
		'update_item'       => __( 'Update Content Type', 'tutsplus' ),
10
		'add_new_item'      => __( 'Add New Content Type', 'tutsplus' ),
11
		'new_item_name'     => __( 'New Content Type Name', 'tutsplus' ),
12
		'menu_name'         => __( 'Content Types', 'tutsplus' ),
13
	);
14
15
	$args = array(
16
		'hierarchical'      => true,
17
		'labels'            => $labels,
18
		'show_ui'           => true,
19
		'show_admin_column' => true,
20
		'query_var'         => true,
21
		'rewrite'           => array( 'slug' => 'content-types' ),
22
	);
23
	
24
	register_taxonomy( 'contenttype', array( 'post' ), $args );
25
	
26
}
27
28
add_action( 'init', 'tutsplus_taxonomies' );

You'll now have an extra taxonomy showing up in the admin for posts:

extra taxonomy showing up in the adminextra taxonomy showing up in the adminextra taxonomy showing up in the admin

Next, you need to add any additional taxonomies. I'm using two more: User Topics and Developer Topics. Inside the tutsplus_taxonomies() function you already created, register each of these using register_taxonomy(). Your entire function will now look like this:

1
function tutsplus_taxonomies() {
2
    // Content type taxonomy

3
	$labels = array(
4
		'name'              => __( 'Content Types', 'tutsplus' ),
5
		'singular_name'     => __( 'Content Type', 'tutsplus' ),
6
		'search_items'      => __( 'Search Content Types', 'tutsplus' ),
7
		'all_items'         => __( 'All Content Types', 'tutsplus' ),
8
		'edit_item'         => __( 'Edit Content Type', 'tutsplus' ),
9
		'update_item'       => __( 'Update Content Type', 'tutsplus' ),
10
		'add_new_item'      => __( 'Add New Content Type', 'tutsplus' ),
11
		'new_item_name'     => __( 'New Content Type Name', 'tutsplus' ),
12
		'menu_name'         => __( 'Content Types', 'tutsplus' ),
13
	);
14
15
	$args = array(
16
		'hierarchical'      => true,
17
		'labels'            => $labels,
18
		'show_ui'           => true,
19
		'show_admin_column' => true,
20
		'query_var'         => true,
21
		'rewrite'           => array( 'slug' => 'content-types' ),
22
	);
23
	
24
	register_taxonomy( 'contenttype', array( 'post' ), $args );
25
	
26
	// User topic taxonomy

27
	$labels = array(
28
		'name'              => __( 'User Topics', 'tutsplus' ),
29
		'singular_name'     => __( 'User Topic', 'tutsplus' ),
30
		'search_items'      => __( 'Search User Topics', 'tutsplus' ),
31
		'all_items'         => __( 'All User Topics', 'tutsplus' ),
32
		'edit_item'         => __( 'Edit User Topic', 'tutsplus' ),
33
		'update_item'       => __( 'Update User Topic', 'tutsplus' ),
34
		'add_new_item'      => __( 'Add New User Topic', 'tutsplus' ),
35
		'new_item_name'     => __( 'New User Topic Name', 'tutsplus' ),
36
		'menu_name'         => __( 'User Topics', 'tutsplus' ),
37
	);
38
39
	$args = array(
40
		'hierarchical'      => true,
41
		'labels'            => $labels,
42
		'show_ui'           => true,
43
		'show_admin_column' => true,
44
		'query_var'         => true,
45
		'rewrite'           => array( 'slug' => 'user-topics' ),
46
	);
47
48
	register_taxonomy( 'usertopic', array( 'post' ), $args );
49
	
50
	// Developer topic taxonomy

51
	$labels = array(
52
		'name'              => __( 'Developer Topics', 'tutsplus' ),
53
		'singular_name'     => __( 'Developer Topic', 'tutsplus' ),
54
		'search_items'      => __( 'Search Developer Topics', 'tutsplus' ),
55
		'all_items'         => __( 'All Developer Topics', 'tutsplus' ),
56
		'edit_item'         => __( 'Edit Developer Topic', 'tutsplus' ),
57
		'update_item'       => __( 'Update Developer Topic', 'tutsplus' ),
58
		'add_new_item'      => __( 'Add New Developer Topic', 'tutsplus' ),
59
		'new_item_name'     => __( 'New Developer Topic Name', 'tutsplus' ),
60
		'menu_name'         => __( 'Developer Topics', 'tutsplus' ),
61
	);
62
63
	$args = array(
64
		'hierarchical'      => true,
65
		'labels'            => $labels,
66
		'show_ui'           => true,
67
		'show_admin_column' => true,
68
		'query_var'         => true,
69
		'rewrite'           => array( 'slug' => 'developer-topics' ),
70
	);
71
72
	register_taxonomy( 'developertopic', array( 'post' ), $args );
73
74
}
75
76
add_action( 'init', 'tutsplus_taxonomies' );

You now have three taxonomies for use with posts:

three taxonomies for use with poststhree taxonomies for use with poststhree taxonomies for use with posts

3. Removing Unwanted Taxonomies

As my knowledge base won't be using categories, I want to remove those from the admin so that people inputting data don't accidentally use them when they should be using my taxonomies.

The way you do this is a little counterintuitive, as there isn't a function for deregistering taxonomies. Instead, you run the register_taxonomy() function with no parameters except the taxonomy name.

To remove categories, add the following to your functions.php file:

1
function tutsplus_remove_taxonomies() {
2
3
    register_taxonomy( 'category', '' );
4
	
5
}
6
add_action( 'init', 'tutsplus_remove_taxonomies', 0 );

When firing this via the init hook, using a priority of 0 ensures that this function runs after the core WordPress function setting up categories in the first place.

If you wanted to remove tags as well, you'd just add the following inside the tutsplus_rremove_taxonomies() function:

1
register_taxonomy( 'post_tag', '' );

You'll now see that just tags and my custom taxonomies are available in the dashboard:

Categories removed in dashboardCategories removed in dashboardCategories removed in dashboard

Summary

You now have the structure in place to be able to start adding data to your knowledge base. The next stage is to create the front-end, which I'll cover in the next tutorial. In that tutorial I'll show you how to create any custom template files, add hooks, and create the context-specific sidebars.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.