Advertisement
  1. Code
  2. Business

WordPress as a Knowledge Base

Scroll to top
Read Time: 6 min

At work we previously used KBPublisher to manage our knowledge base. It costed money, it was hard to style, code is encrypted with ion cube and so on, basically very hard to maintain. WordPress can do the same things and even better.

This tutorial will show you how to use custom taxonomies for knowledge base sections and custom posts for knowledge base articles.


Step 1 Administration

Knowledge base sections and articles need to be managed. WordPress makes this easy to do with custom taxonomies and custom post types.

Just register the new taxonomy and post type. Add the following into functions.php from your theme.

For more information on the ins and outs of this functionality, read our other custom post type and custom taxonomy articles.

1
2
function register_kb() {
3
	register_post_type( 'knowledgebase',
4
		array(
5
			'labels' => array(
6
				'name' => 'Knowledge Base',
7
				'menu_name' => 'Knowledge Base',
8
				'singular_name' => 'Article',
9
				'all_items' => 'All Articles'
10
			),
11
			'public' => true,
12
			'publicly_queryable' => true,
13
			'show_ui' => true,
14
			'show_in_menu' => true,
15
			'show_in_nav_menus' => true,
16
			'menu_position ' => 20,
17
			'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'comments', 'post-formats', 'revisions' ),
18
			'hierarchical' => false,
19
			'taxonomies' => array( 'section' ),
20
			'has_archive' => true,
21
			'rewrite' => array( 'slug' => 'knowledgebase', 'hierarchical' => true, 'with_front' => false )
22
		)
23
	);
24
	
25
	register_taxonomy( 'section', array( 'knowledgebase' ),
26
		array(
27
			'labels' => array(
28
				'name' => 'Sections',
29
				'menu_name' => 'Sections',
30
				'singular_name' => 'Section',
31
				'all_items' => 'All Sections'
32
			),
33
			'public' => true,
34
			'hierarchical' => true,
35
			'show_ui' => true,
36
			'rewrite' => array( 'slug' => 'knowledgebase-section', 'hierarchical' => true, 'with_front' => false ),
37
		)
38
	);
39
}
40
add_action( 'init', 'register_kb' );
41
42
function kb_rewrite_rules( $wp_rewrite ) {
43
	$new_rules = array( 'knowledgebase/(.*)/(.*)' => 'index.php?post_type=knowledgebase&section=' . $wp_rewrite->preg_index( 1 ) . '&knowledgebase=' . $wp_rewrite->preg_index( 2 ) );
44
	$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
45
}
46
add_action( 'generate_rewrite_rules', 'kb_rewrite_rules' );

This will run after WordPress has finished loading but before any headers are sent, registering the post type and taxonomy. Also, it will add the rewrite rules for the permalinks of the taxonomy and post type.

The register_post_type registers the custom post type, this is used for the KB articles. The register_taxonomy registers the custom taxonomy, this is used for the KB sections. The articles won't be hierarchical but the sections will be, so it gives the possibility to create a tree structure.

Also it would be nice to show the sections that an article is assigned to.

1
2
function kb_columns( $defaults ) {
3
	$defaults['section'] = 'Sections';
4
	return $defaults;
5
}
6
add_filter( 'manage_knowledgebase_posts_columns', 'kb_columns' );
7
8
function kb_custom_column( $column_name, $post_id ) {
9
	$taxonomy = $column_name;
10
	$post_type = get_post_type( $post_id );
11
	$terms = get_the_terms( $post_id, $taxonomy );
12
	if ( !empty( $terms ) ) {
13
		foreach ( $terms as $term ) {
14
			$post_terms[] = "<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " . esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . '</a>';
15
		}
16
		echo join( ', ', $post_terms );
17
	}
18
	else echo '<i>No terms.</i>';
19
}
20
add_action( 'manage_knowledgebase_posts_custom_column', 'kb_custom_column', 10, 2 );

Now add some sections and some articles so there is something to show.


Step 2 Showing the Sections

Add the following into functions.php from your theme.

1
2
function kb_sections( $sections = array(), $active_section = null ) {
3
	$taxonomy = 'section';
4
	$link_class = '';
5
	if ( empty( $sections ) ) {
6
		$link_class = 'root';
7
		$sections = get_terms( $taxonomy, array( 'parent' => 0, 'hide_empty' => 0 ) );
8
		$active_section = kb_active_section();
9
		echo '<ul id="kb-sections" class="unstyled">';
10
	}
11
	if ( empty( $active_section ) ) {
12
		$active_section = '';
13
	}
14
	foreach ( $sections as $section ) {
15
		$toggle = '';
16
		$section_children = get_terms( $taxonomy, array( 'parent' => $section->term_id, 'hide_empty' => 0 ) );
17
		if ( !empty( $section_children ) && $link_class != 'root' ) {
18
			$toggle = '<i class="toggle"></i>';
19
		}
20
		echo '<li class="' . ( $section->term_id == $active_section ? 'active' : '' ) . '">';
21
		echo '<a  href="' . get_term_link( $section, $taxonomy ) . '" class="' . $link_class . '" rel="' . $section->slug . '">' . $toggle . $section->name . '</a>';
22
		
23
		if ( !empty( $section_children ) ) {
24
			echo '<ul id="' . $section->slug . '" class="children">';
25
			kb_sections( $section_children, $active_section );
26
		}
27
		echo "</li>";
28
	}
29
	echo "</ul>";
30
}
31
32
function kb_active_section() {
33
	$taxonomy = 'section';
34
	$current_section = '';
35
	if ( is_single() ) {
36
		$sections = explode( '/', get_query_var( $taxonomy ) );
37
		$section_slug = end( $sections );
38
		if ( $section_slug != '' ) {
39
			$term = get_term_by( 'slug', $section_slug, $taxonomy );
40
		} else {
41
			$terms = wp_get_post_terms( get_the_ID(), $taxonomy );
42
			$term = $terms[0];
43
		}
44
		$current_section = $term->term_id;
45
	} else {
46
		$term = get_term_by( 'slug', get_query_var( $taxonomy ), get_query_var( 'taxonomy' ) );
47
		$current_section = $term->term_id;
48
	}
49
	return $current_section;
50
}

In the theme folder create a file called sidebar-sections.php where your kb_sections function will be called outputting an unordered and nested list of sections.

1
2
<?php kb_sections(); ?>

Like this, the KB sections can be shown everywhere as desired by including the sidebar.

1
2
<?php get_sidebar( 'sections' ); ?>

Step 3 Showing the Articles

Add the following into functions.php from your theme.

1
2
function kb_article_permalink( $article_id, $section_id ) {
3
	$taxonomy = 'section';
4
	$article = get_post( $article_id );
5
	$section = get_term( $section_id, $taxonomy );
6
	$section_ancestors = get_ancestors( $section->term_id, $taxonomy );
7
	krsort( $section_ancestors );
8
	$permalink = '<a href="/knowledgebase/';
9
	foreach ($section_ancestors as $ancestor):
10
		$section_ancestor = get_term( $ancestor, $taxonomy );
11
		$permalink.= $section_ancestor->slug . '/';
12
	endforeach;
13
	$permalink.= $section->slug . '/' . $article->post_name . '/" >' . $article->post_title . '</a>';
14
	return $permalink;
15
}

Note: this method is necessary because an article can be linked to multiple sections.

This will generate a hierarchical permalink structure.

Like: /knowledgebase/section-slug/sub-section-slug/another-sub-section-slug/article-slug

In the theme folder then create these files: archive-knowledgebase.php, single-knowledgebase.php, content-knowledgebase.php, taxonomy-section.php.

In archive-knowledgebase.php add the following to show the sections and recent articles.

1
2
<?php
3
get_header();
4
get_sidebar( 'sections' );
5
while ( have_posts() ) : the_post();
6
	$terms = wp_get_post_terms( $post->ID, 'section' );
7
	$term = $terms[0];
8
	echo kb_article_permalink( $post->ID, $term->term_id );
9
endwhile;
10
get_footer();
11
?>

In single-knowledgebase.php add the following.

1
2
<?php
3
get_header();
4
while ( have_posts() ) : the_post();
5
	get_template_part( 'content', 'knowledgebase' );
6
endwhile;
7
get_footer();
8
?>

In content-knowledgebase.php add the following.

1
2
<?php
3
get_sidebar( 'sections' );
4
the_title();
5
the_content();
6
?>

In taxonomy-section.php add the following to show a list of articles from a section.

1
2
<?php
3
global $wp_query;
4
$args = array_merge( $wp_query->query, array( 'posts_per_page' => 100 ) );
5
query_posts( $args );
6
$term = get_term_by( 'slug', get_query_var( 'term' ), 'section' );
7
8
get_header();
9
get_sidebar( 'sections' );
10
while ( have_posts() ) : the_post();
11
	echo kb_article_permalink( $post->ID, $term->term_id );
12
endwhile;
13
get_footer();
14
?>

This can be structured and styled as desired.


Example

A real life example of how this works and how it can be used: syneto.net


References

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.