Advertisement
  1. Code
  2. WordPress
  3. Plugin Development

Random Quote Plugin With Custom Post Type

Scroll to top
Read Time: 5 min

This is the second part of creating a random quote plugin, but this time with custom post types.


In this second version of the random quote plugin we move a bit further and use some more tools and functions. Custom post types are available since WordPress v3.0. They allow developers to add different types of content to their plug-ins and themes and with that you can easily extend WordPress' CMS functionality.

You can find the first version of the plugin here. If you are just starting out with plugin development or want a quick refresh it is recommended to read that first.


1. Basic Data

This is the data needed for every plugin. Simple things like author, plugin name, description and so on.

1
2
<?php
3
/*

4
Plugin Name: Adam's Random Quote

5
Version: 2.0

6
Plugin URI: https://code.tutsplus.com

7
Description: Loads a Random Quote from custom post types

8
Author: Adam Burucs

9
Author URI: https://code.tutsplus.com

10
*/
11
?>

2. Register Custom Post Type

Custom post types in WordPressCustom post types in WordPressCustom post types in WordPress

Before we can use the random quote custom post type we have to set it. In the labels array the singular and plural form of the name is needed. The public switch set to true allows the users to use the post type on the admin. The has_archive sets the option to use archive of post types.

1
2
<?php
3
add_action( 'init', 'random_quote' );
4
function random_quote() {
5
	register_post_type( 'random_quote',
6
		array(
7
			'labels' => array(
8
				'name' => __( 'Random Quotes' ),
9
				'singular_name' => __( 'Random Quote' )
10
			),
11
			'public' => true,
12
			'has_archive' => true,
13
		)
14
	);
15
}
16
?>

3. Creating Admin Interface

With project_edit_columns and project_custom_columns function we can create a modified admin interface for the custom post type using the Person and Quote fields (overriding the title and description). Both of these two functions are needed to get the job done.

1
2
<?php
3
add_filter("manage_edit-random_quote_columns", "project_edit_columns");
4
5
function project_edit_columns($columns) {
6
	$columns = array(
7
		"cb" => "<input type=\"checkbox\" />",
8
		"title" => "Person",
9
		"description" => "Quote",
10
	);
11
12
	return $columns;
13
}
14
15
add_action("manage_posts_custom_column",  "project_custom_columns");
16
17
function project_custom_columns($column) {
18
	global $post;
19
	switch ($column) {
20
		case "description":
21
			the_excerpt();
22
			break;
23
	}
24
}
25
?>

This is a picture of the final plugin.

Listing of custom posts (quotes)Listing of custom posts (quotes)Listing of custom posts (quotes)

4. Get One Random Quote From the Database

Get data from the WP DBGet data from the WP DBGet data from the WP DB

With the WP_Query class we can get one random element from the custom posts. Because we retrieve only one element we don't need a standard loop. Setting these three arguments are mandatory. The $quo variable helps in creating a string based on the quote and its author which can generate a sample like this:

"I never think of the future. It comes soon enough."
~ Albert Einstein

1
2
<?php
3
function ab_arq_generate() {
4
	// Retrieve one random quote

5
	$args = array(
6
		'post_type' => 'random_quote',
7
		'posts_per_page' => 1,
8
		'orderby' => 'rand'
9
	);
10
	$query = new WP_Query( $args );
11
12
	// Build output string

13
	$quo = '';
14
	$quo .= $query->post->post_title;
15
	$quo .= ' said "';
16
	$quo .= $query->post->post_content;
17
	$quo .= '"';
18
19
	return $quo;
20
}
21
?>

5. Assigning the Quote to the Blog Description Element

To attach the generated quote to its place we use a helper function, and after that we override the default filter (bloginfo).

1
2
<?php
3
function ab_arq_change_bloginfo( $text, $show ) {
4
	if( 'description' == $show ) {
5
		$text = ab_arq_generate();
6
	}
7
	return $text;
8
}
9
10
add_filter( 'bloginfo', 'ab_arq_change_bloginfo', 10, 2 );
11
?>

6. Final Code

Here is what we have done, just one file.

1
2
<?php
3
/*

4
Plugin Name: Adam's Random Quote

5
Version: 2.0

6
Plugin URI: https://burucs.com

7
Description: Loads a Random Quote from custom post types

8
Author: Adam Burucs

9
Author URI: http://burucs.com

10
*/
11
12
// Register custom post type

13
add_action( 'init', 'ab_arq_random_quote' );
14
function ab_arq_random_quote() {
15
	register_post_type( 'random_quote',
16
		array(
17
			'labels' => array(
18
				'name' => __( 'Random Quotes' ),
19
				'singular_name' => __( 'Random Quote' )
20
			),
21
			'public' => true,
22
			'has_archive' => true,
23
		)
24
	);
25
}
26
27
// Create admin interface

28
29
add_filter("manage_edit-random_quote_columns", "ab_arq_project_edit_columns");
30
31
function ab_arq_project_edit_columns($columns) {
32
	$columns = array(
33
		"cb" => "<input type=\"checkbox\" />",
34
		"title" => "Person",
35
		"description" => "Quote",
36
	);
37
38
	return $columns;
39
}
40
41
add_action("manage_posts_custom_column",  "ab_arq_project_custom_columns");
42
43
function ab_arq_project_custom_columns($column) {
44
	global $post;
45
	switch ($column) {
46
		case "description":
47
			the_excerpt();
48
			break;
49
	}
50
}
51
52
// Main function to get quotes

53
function ab_arq_generate() {
54
	// Retrieve one random quote

55
	$args = array(
56
		'post_type' => 'random_quote',
57
		'posts_per_page' => 1,
58
		'orderby' => 'rand'
59
	);
60
	$query = new WP_Query( $args );
61
62
	// Build output string

63
	$quo = '';
64
	$quo .= $query->post->post_title;
65
	$quo .= ' said "';
66
	$quo .= $query->post->post_content;
67
	$quo .= '"';
68
69
	return $quo;
70
}
71
72
// Helper function

73
function ab_arq_change_bloginfo( $text, $show ) {
74
	if( 'description' == $show ) {
75
		$text = ab_arq_generate();
76
	}
77
	return $text;
78
}
79
80
// Override default filter with the new quote generator

81
add_filter( 'bloginfo', 'ab_arq_change_bloginfo', 10, 2 );
82
?>

7. Summary

In just a few additional steps we created a lot more flexible storing system with the use of custom posts, however please note that if you deactivate or delete the plugin, the quotes (custom posts) will remain in the WordPress database. If you want these to be deleted you have to extend this plugin accordingly.

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.