Custom Post Type for Storing Lecture Notes
Learn to create a lecture note feature for the teachers on our website.
In this lesson, we will create a lecture notes feature. We will provide this feature to teachers to let them create lecture notes which will be saved in their accounts. We will talk more about creating user accounts in the next chapter of the course.
The first step is to create a custom post type for lecture notes. Then, we will create a page to display the lecture note posts. This page will have a form where users can enter the title and contents of the lecture note with a save button to save the lecture. Below the form, the user will be shown his/her past lecture notes. The user will also have the option of editing and deleting previously created lecture notes. In the upcoming lessons, we will learn how to send the create, edit and delete requests to the REST API and show the changes in real time using JavaScript.
This is how we want the page to look like:
Step 1: Custom post type for lecture notes
We will create a new custom post type to store lecture notes. Open the mu-plugins folder (inside the wp-content folder). It has a file school-custom-post-types.php
which contains the code for the different custom post types that we have created so far.
The school_custom_post_types
function hooks on to the init
hook to register a custom post type.
<?phpfunction school_custom_post_types(){register_post_type($name, $args);}add_action('init', 'school_custom_post_types');
As a first step, we will create the array of arguments for the lecture post type.
<?php$lectureArgs = array('public' => false,'show_ui' => true,'show_in_rest' => true,'labels' => array('name' => 'Lectures','singular_name' => 'Lecture','add_new_item' => 'Add New Lecture','edit_item' => 'Edit Lecture','view_item' => 'View Lecture','all_items' => 'All Lectures'),'menu_icon' => 'dashicons-welcome-write-blog','supports' => array( 'title', 'editor' ));
Since the lecture posts are specific to a user, we have set
public
tofalse
. This will make the posts private and they will not show up in public queries.The
show_ui
attribute set totrue
makes the lecture post type appear in the admin dashboard. In the absence of this line, the Lectures option will be ...