Create A Simple CRM in WordPress: Creating a Custom Post Type

If you're a freelance designer, developer, or a business owner, you know how difficult it is to keep a central store of prospective customer data.  Once you've accomplished that, it's sometimes difficult to keep that data updated - logging calls you've made to the prospect, emails you've sent, and so on.

Specifically, we'll explore registering a custom post type, creating custom post fields, displaying custom post fields in the WordPress Administration interface, searching our custom field data and turning the WordPress Administration interface into a lightweight CRM interface.

Why Not Use a CRM Plugin (Or a CRM System?)

While free and premium WordPress CRM plugins do exist (such as WP-CRM) and third party CRM services (such as Salesforce or Highrise), they are either too complex to start using quickly, or have features that may not be ideal for our particular workflow.

By rolling our own solution, we can build a lightweight, simple solution, that could be extended in the future based on your specific requirements. Plus, we’ll learn how to use some of WordPress API functions.

Creating the CRM Plugin

For this series, we’ll create a WordPress plugin and extend its functionality through each part in this series.

The source code will be provided in full at the end of each article, but until then, let’s create the plugin step-by-step.

First, in your local installation of WordPress, create a directory in wp-content/plugins called tuts-crm. From here, we’ll begin adding our source code.

The Plugin Header

Create a file called tuts-crm.php, inserting the following code:

As you’ll see, this is just a PHP comment. However, WordPress scans this to figure out:

  • the name of the plugin,
  • the author of the plugin,
  • the description of what the plugin does

In the WordPress dashboard, activate your new plugin by going to "Plugins" > "Installed Plugins," and then clicking "Activate" beside the CRM plugin:

Setting Up Our Plugin Class

With any WordPress web site having any possible number and combination of plugins, it’s possible that a function name we choose to use (such as crm()) conflicts with another plugin that uses the same.

Most plugins get around this issue by using a function_exists() check:

While this stops your WordPress site from grinding to a halt, it doesn’t get your plugin working if there is a conflict.

An alternate object-orientated approach is to wrap your functions in an object-oriented class. Let’s go ahead and do that by adding the following below the plugin header comment:

This creates our PHP class, along with a construct, which is called when we reach the line $wpTutsCRM = new WPTutsCRM;

Register Our Custom Post Type

Next, we need to register a Custom Post Type, which will store each contact in our CRM system.

From the Codex:

WordPress can hold and display many different types of content. A single item of such a content is generally called a post, although post is also a specific post type. Internally, all the post types are stored in the same place, in the wp_posts database table, but are differentiated by a column called post_type.

WordPress 3.0 gives you the capability to add your own custom post types and to use them in different ways.

Pages and Posts are two common WordPress Post Types. We can extend WordPress’ functionality by adding our own Custom Post Type. Let’s do this by registering an action to our Plugin’s constructor:

This tells WordPress to run the register_custom_post_type function inside our class when our plugin is initialised.

Next, add the register_custom_post_type function:

This registers a Custom Post Type called contact. register_post_type accepts the following array of arguments:

key Description
labels An array of labels describing this Post Type. These are used within the WordPress Administration.
has_archive Enables archives on the frontend web site.
public Whether the Post Type should be available in the WordPress Administration and frontend web site.
publicly_queryable Whether this Custom Post Type can be queried from the frontend web site.
capability_type Used to define the capabilities the User should have in order to add, edit and delete Posts for this Post Type. This could be an existing capability (such as post)
menu_icon Either the URL to the menu icon image, or a dashicons- class (3.8 or higher). Used in the WordPress Administration.
menu_position A number indicating where in the WordPress Administration menu to display this item. Lower number means it appears sooner.
query_var Sets the query_var for this post type. Defaults to the name of the Custom Post Type.
show_in_menu Whether to show this Custom Post Type in the WordPress Administration menu.
show_ui Whether to generate a UI in the WordPress Administration to allow adding, editing and deleting Posts for this Custom Post Type.
supports An array detailing the fields available to this Custom Post Type. Examples include title, editor, excerpt and page-attributes

Save your plugin, and then view your WordPress dashboard. You should see a new menu entry called Contacts:

Click on this entry, and you should see a familiar interface to the one you use to manage Posts and Pages:

Let’s go ahead and add our first contact. Click on "Add New," and then put "Joe Bloggs" in the "Title" field, before clicking "Publish."

Finally, we need to check the Custom Post Type saved. Click back on "Contacts" in the WordPress administration menu, and you should see our new contact:

Up Next…

In the next article, we’re going to add custom fields to our Contact Custom Post Type, allowing us to store more information about our prospects and clients.



Related Articles