One of the more common use cases for developing a custom plugin
is to take advantage of the WordPress Post API to create custom post types.
Let's say you're building an online bookstore and you want to store data about the books that are for sale.
The WordPress Core Post API allows you to register custom post types.
These custom post types extend the Core Post data type,
which is stored in the post table in the WordPress database.
An example of a custom post type is the page data type,
which is registered by WordPress Core.
Using the WordPress Post API, you can create a custom post type and store information about books.
When you register a custom post type correctly,
WordPress will automatically create a new admin menu item for your custom post type
and admin pages to manage the data.
Register a custom post type.
You can use the WordPress register post type function.
As you can see from the function reference page in the WordPress developer documentation,
this function takes two parameters, the name of the custom post type
and an array of arguments that define the custom post type.
Parameters are the names of variables that you define in a function declaration.
In this case, the parameters are the name, which is a string variable, and an array of arguments.
When you use the function, you pass in the values for these parameters.
These values are called arguments.
Here is an example of creating the arguments array and calling the register post type function,
passing in the relative arguments to create the custom post type for books.
Now let's look at how you could implement this code in a plugin.
Start, create a new plugin in the same way that you learned in the what is a plugin lesson.
Add a directory to the WP content plugins directory called bookstore.
Then create the new PHP file called bookstore.php inside that directory.
Inside the file, add the opening PHP tag
and include the plugin header at the top of the file,
as well as the abs path check to prevent direct access to the file.
Next, create a new function called bookstore register book post type
that will register the custom post type.
Inside that function, you can set up the arguments array.
Then you can call register post type, passing in the book post type and the arguments.
Notice the use of the bookstore prefix in the function name.
This is an example of prefixing function names to avoid conflicts with other plugins or themes.
You can read more about this as well as other methods to prevent function conflicts
in the plugin best practices page in the plugin developer handbook.
The last step is to trigger the bookstore register book post type function.
You will need to hook into the init action hook to do this.
If you skip the module on WordPress hooks,
hooks are a way to run code at specific points in a WordPress request lifecycle.
By hooking into the init action hook,
you run this code every time a WordPress request is initialized.
Now browse through your WordPress dashboard and activate the plugin.
You should see a new menu item called books in the admin menu
with the ability to add new books to your book custom post type.
If you browse to the post table in the WordPress database,
you should also see new rows with a post type column said to book containing your new books data.
Post type book.
It is important to note that registering too many custom post types can have a performance impact
on a WordPress site. This is because the custom post type registration process is executed on
every site request, even if the custom post type data is not being used on that post or page.
So be careful of overusing custom post types and consider when you should use them versus
creating custom tables to store custom data. You can learn more about creating and using custom
tables in the creating tables with plugins page in the plugin developer handbook. You will also
learn more about this in a future plugin developer learning pathway. To read more about custom post
types and how to register them, visit the post types page in the plugin developer handbook.
It's also a good idea to read the full register post type function reference page
in the WordPress developer documentation, as it contains a full list of all the
arguments that can be passed to the function and what they do.