1. Custom Post Types and Meta Fields
Register Question Post Type
function register_question_post_type() {
register_post_type('question', array(
'labels' => array(
'name' => __('Questions'),
'singular_name' => __('Question'),
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'questions'),
'supports' => array('title', 'editor', 'comments'),
));
}
add_action('init', 'register_question_post_type');
Register Answer Post Type
function register_answer_post_type() {
register_post_type('answer', array(
'labels' => array(
'name' => __('Answers'),
'singular_name' => __('Answer'),
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'answers'),
'supports' => array('title', 'editor'),
));
}
add_action('init', 'register_answer_post_type');
2. Meta Fields for Answers
Add Meta Box for Answers
function add_answer_meta_box() {
add_meta_box('answer_meta_box', 'Answer Details', 'render_answer_meta_box', 'answer', 'normal', 'high');
}
add_action('add_meta_boxes', 'add_answer_meta_box');
function render_answer_meta_box($post) {
$user_id = get_post_meta($post->ID, 'user_id', true);
$question_id = get_post_meta($post->ID, 'question_id', true);
?>
<label for="user_id">User ID:</label>
<input type="text" id="user_id" name="user_id" value="<?php echo esc_attr($user_id); ?>" disabled><br>
<label for="question_id">Question ID:</label>
<input type="text" id="question_id" name="question_id" value="<?php echo esc_attr($question_id); ?>" disabled><br>
<?php
}
Save Answer Meta Box Data
function save_answer_meta_box_data($post_id) {
if (array_key_exists('user_id', $_POST)) {
update_post_meta($post_id, 'user_id', sanitize_text_field($_POST['user_id']));
}
if (array_key_exists('question_id', $_POST)) {
update_post_meta($post_id, 'question_id', sanitize_text_field($_POST['question_id']));
}
}
add_action('save_post', 'save_answer_meta_box_data');
Create Custom REST API Endpoints
Define custom REST API endpoints to manage questions and answers. Here’s a basic example:
function register_custom_endpoints() {
// Endpoint for creating a question
register_rest_route('custom/v1', '/questions', array(
'methods' => 'POST',
'callback' => 'create_question',
'permission_callback' => function () {
return current_user_can('publish_posts');
}
));
// Endpoint for updating a question
register_rest_route('custom/v1', '/questions/(?P<id>\d+)', array(
'methods' => 'PUT',
'callback' => 'update_question',
'permission_callback' => function () {
return current_user_can('edit_posts');
}
));
// Endpoint for deleting a question
register_rest_route('custom/v1', '/questions/(?P<id>\d+)', array(
'methods' => 'DELETE',
'callback' => 'delete_question',
'permission_callback' => function () {
return current_user_can('delete_posts');
}
));
// Endpoint for creating an answer
register_rest_route('custom/v1', '/answers', array(
'methods' => 'POST',
'callback' => 'create_answer',
'permission_callback' => function () {
return is_user_logged_in();
}
));
// Endpoint for updating an answer
register_rest_route('custom/v1', '/answers/(?P<id>\d+)', array(
'methods' => 'PUT',
'callback' => 'update_answer',
'permission_callback' => function () {
return current_user_can('edit_posts');
}
));
// Endpoint for deleting an answer
register_rest_route('custom/v1', '/answers/(?P<id>\d+)', array(
'methods' => 'DELETE',
'callback' => 'delete_answer',
'permission_callback' => function () {
return current_user_can('delete_posts');
}
));
}
add_action('rest_api_init', 'register_custom_endpoints');
Callback Methods for Questions
Create a Question (POST /custom/v1/questions)
function create_question(WP_REST_Request $request) {
$params = $request->get_json_params();
// Ensure all required fields are present
if (!isset($params['title'])) {
return new WP_REST_Response(array('message' => 'Missing required fields'), 400);
}
$new_question = array(
'post_title' => sanitize_text_field($params['title']),
'post_status' => 'publish', // Example: Set initial status to 'publish'
'post_type' => 'question'
);
$question_id = wp_insert_post($new_question);
if ($question_id) {
// Update meta fields if necessary
return new WP_REST_Response(array('message' => 'Question created successfully', 'question_id' => $question_id), 201);
}
return new WP_REST_Response(array('message' => 'Failed to create question'), 500);
}
Update a Question (PUT /custom/v1/questions/{id})
function update_question(WP_REST_Request $request) {
$params = $request->get_json_params();
$question_id = (int) $request['id'];
$updated_question = array(
'ID' => $question_id,
'post_title' => isset($params['title']) ? sanitize_text_field($params['title']) : '',
// You can update other post fields as needed
);
$result = wp_update_post($updated_question);
if ($result instanceof WP_Error) {
return new WP_REST_Response(array('message' => 'Failed to update question'), 500);
}
// Update meta fields if necessary
return new WP_REST_Response(array('message' => 'Question updated successfully', 'question_id' => $question_id), 200);
}
Delete a Question (DELETE /custom/v1/questions/{id})
function delete_question(WP_REST_Request $request) {
$question_id = (int) $request['id'];
$result = wp_delete_post($question_id, true); // Set true to force delete
if (!$result) {
return new WP_REST_Response(array('message' => 'Failed to delete question'), 500);
}
return new WP_REST_Response(array('message' => 'Question deleted successfully'), 200);
}
Callback Methods for Answers
Create an Answer (POST /custom/v1/answers)
function create_answer(WP_REST_Request $request) {
$params = $request->get_json_params();
// Ensure all required fields are present
if (!isset($params['question_id'], $params['content'])) {
return new WP_REST_Response(array('message' => 'Missing required fields'), 400);
}
$new_answer = array(
'post_title' => 'Answer for Question #' . $params['question_id'],
'post_content' => sanitize_textarea_field($params['content']),
'post_status' => 'publish', // Example: Set initial status to 'publish'
'post_type' => 'answer'
);
// Set author ID for the current logged-in user or any specific logic
$author_id = get_current_user_id();
if ($author_id) {
$new_answer['post_author'] = $author_id;
}
$answer_id = wp_insert_post($new_answer);
if ($answer_id) {
// Update meta fields if necessary, e.g., link to the question
update_post_meta($answer_id, 'question_id', sanitize_text_field($params['question_id']));
return new WP_REST_Response(array('message' => 'Answer created successfully', 'answer_id' => $answer_id), 201);
}
return new WP_REST_Response(array('message' => 'Failed to create answer'), 500);
}
Update an Answer (PUT /custom/v1/answers/{id})
function update_answer(WP_REST_Request $request) {
$params = $request->get_json_params();
$answer_id = (int) $request['id'];
$updated_answer = array(
'ID' => $answer_id,
'post_title' => isset($params['content']) ? sanitize_text_field($params['content']) : '',
// You can update other post fields as needed
);
$result = wp_update_post($updated_answer);
if ($result instanceof WP_Error) {
return new WP_REST_Response(array('message' => 'Failed to update answer'), 500);
}
// Update meta fields if necessary
return new WP_REST_Response(array('message' => 'Answer updated successfully', 'answer_id' => $answer_id), 200);
}
Delete an Answer (DELETE /custom/v1/answers/{id})
function delete_answer(WP_REST_Request $request) {
$answer_id = (int) $request['id'];
$result = wp_delete_post($answer_id, true); // Set true to force delete
if (!$result) {
return new WP_REST_Response(array('message' => 'Failed to delete answer'), 500);
}
return new WP_REST_Response(array('message' => 'Answer deleted successfully'), 200);
}