The goal of this application is to let people submit questions and get answers from various experts in the same field. First off, we need to create a method to add questions and answers. By default, WordPress allows us to create posts and submit comments to the posts. In this scenario, a post can be considered as the question and comments can be considered as the answers. Therefore, we have the capability of directly using normal post creation for building this interface.
However, I would like to choose a slightly different approach by using the custom post types plugin, which you can find at http://codex.wordpress.org/Post_Types#Custom_Post_Types, in order to keep the default functionality of posts and let the new functionality be implemented separately without affecting the existing ones. We will create a plugin to implement the necessary tasks for our application:
- First off, create a folder called wpwa-questions inside the /wp-content/plugins folder and add a new file called wpwa-questions.php.
- Next, we need to add the block comment to define our file as a plugin:
/*
Plugin Name: WPWA Questions
Plugin URI: -
Description: Question and Answer Interface using WordPress Custom Post Types and Comments
Version: 1.0
Author: Rakhitha Nimesh
Author URI: http://www.wpexpertdeveloper.com/
License: GPLv2 or later
Text Domain: wpwa-questions
*/
- Having created the main plugin file, we can now create the structure of our plugin with the necessary settings as shown in the following code section:
if( !class_exists( 'WPWA_Questions' ) ) {
class WPWA_Questions{
private static $instance;
public static function instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WPWA_Questions ) ) {
self::$instance = new WPWA_Questions();
self::$instance->setup_constants();
self::$instance->includes();
add_action('admin_enqueue_scripts',array(self::$instance,'load_admin_scripts'),9);
add_action('wp_enqueue_scripts',array(self::$instance,'load_scripts'),9);
}
return self::$instance;
}
public function setup_constants() {
if ( ! defined( 'WPWA_VERSION' ) ) {
define( 'WPWA_VERSION', '1.0' );
}
if ( ! defined( 'WPWA_PLUGIN_DIR' ) ) {
define('WPWA_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'WPWA_PLUGIN_URL' ) ) {
define( 'WPWA_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
}
public function load_scripts(){ }
public function load_admin_scripts(){ }
private function includes() { }
public function load_textdomain() { }
}
}
- First, we create a class called WPWA_Questions as the main class of the question-answer plugin. Then we define a variable to hold the instance of the class and use the instance function to generate an object from this class. This static function and the private instance variables make sure that we only have one instance of our plugin class. We have also included the necessary function calls and filters in this function.
- These functions are used to handle the most basic requirements of any WordPress plugin. Since they are common to all plugins, we keep these functions inside the main file. Let's look at the functionality of these functions:
- setup_constants: This function is used to define the constants of the application such as version, plugin directory path, and plugin directory URL
- load_scripts: This function is used to load all the plugin specific scripts and styles on the frontend of the application
- load_admin_scripts: This function is used to load all the plugin specific scripts and styles on the backend of the application
- includes: This function is used to load all the other files of the plugin
- load_text_domain: This function is used to configure the language settings of the plugin
- Next, we initialize the plugin by calling the following code after the class definition:
function WPWA_Questions() {
global $wpwa;
$wpwa = WPWA_Questions::instance();
}
WPWA_Questions();
- Having created the main plugin file, we can move into creating a custom post type called wpwa-question using the following code snippet. Include this code snippet in your WPWA_Questions class file of the plugin:
public function register_wp_questions() {
$labels = array(
'name' => __( 'Questions', 'wpwa_questions' ),
'singular_name' => __( 'Question',
'wpwa_questions'),
'add_new' => __( 'Add New', 'wpwa_questions'),
'add_new_item' => __( 'Add New Question',
'wpwa_questions'),
'edit_item' => __( 'Edit Questions',
'wpwa_questions'),
'new_item' => __( 'New Question',
'wpwa_questions'),
'view_item' => __( 'View Question',
'wpwa_questions'),
'search_items' => __( 'Search Questions',
'wpwa_questions'),
'not_found' => __( 'No Questions found',
'wpwa_questions'),
'not_found_in_trash' => __( 'No Questions found in
Trash', 'wpwa_questions'),
'parent_item_colon' => __( 'Parent Question:',
'wpwa_questions'),
'menu_name' => __( 'Questions', 'wpwa_questions'),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => __( 'Questions and Answers',
'wpwa_questions'),
'supports' => array( 'title', 'editor',
'comments' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type( 'wpwa_question', $args );
}
This is the most basic and default code for custom post type creation, and I assume that you are familiar with the syntax. We have enabled title, editor, and comments in the support section of the configuration. These fields will act as the roles of question title, question description, and answers. Other configurations contain the default values and hence explanations will be omitted. If you are not familiar, make sure to have a look at documentation on custom post creation at http://codex.wordpress.org/Function_Reference/register_post_type.
Beginner to intermediate level developers and designers tend to include the logic inside the functions.php file in the theme. This is considered a bad practice as it becomes extremely difficult to maintain because your application becomes larger. So, we will be using plugins to add functionality throughout this book and the drawbacks of the functions.php technique will be discussed in later chapters.
- Then, you have to add the following code inside the instance function of WPWA_Questions class to initialize the custom post type creation code:
add_action('init',
array(self::$instance,'register_wp_questions'));
- Once the code is included, you will get a new section on the admin area for creating questions. This section will be similar to the posts section inside the WordPress admin. Add a few questions and insert some comments using different users before we move into the next stage.
Before we go into the development of questions and answers, we need to make some configurations so that our plugin works without any issues. Let's look at the configuration process:
- First, we have to look at the comment-related settings inside Discussion Settings in the WordPress Settings section. Here, you can find a setting called Before a comment appears.
- Disable both checkboxes so that users can answer and get their answers displayed without the approval process. Depending on the complexity of application, you can decide whether to enable these checkboxes and change the implementation.
- The second setting we have to change is the Permalinks. Once we create a new custom post type and view it in a browser, it will redirect you to a 404 page not found page. Therefore, we have to go to the Permalinks section of WordPress Settings and update the Permalinks using the Save Changes button. This won't change the Permalinks. However, this will flush the rewrite rules so that we can use the new custom post type without 404 errors.
Now, we can start working with the answer-related features.