How to show cutom plugin data inside wordpress dashboard?
How to show cutom plugin data inside wordpress dashboard?
I am new to wordpress plugin development and I have a created a simple contact form plugin which takes name, email, and message as input, I just want to show the data saved in the form in the wordpress dashboard.
Here's a brief overview of what I've done so far:
1- Created a contact form using the add_shortcode function.
2- Handled form submissions and saved the data using wp_insert_post.
3- Set up a custom post type for form submissions and customized the dashboard view.
Here's the code:
<?php
if(!class_exists('DemoPlugin'))
{
class DemoPlugin
{
public function __construct()
{
//Create custom post type
add_action('init', [$this, 'create_custom_post_type']);
// add_action('init', [$this, 'list_all_shortcodes']);
//Add Shortcode
add_shortcode('demo_plugin_contact', [$this, 'demo_plugin_contact']);
//Register REST API
add_action('rest_api_init', [$this, 'register_rest_api']);
}
public function create_custom_post_type()
{
$args = [
'public' => true,
'has_archive' => true,
'supports' => ['title', 'editor', 'custom-fields'],
'exclude_from_search' => true,
'publicly_queryable' => false,
'capability' => 'manage_options',
'labels' => [
'name' => 'Contact Form(Demo-Plugin)',
'singular_name' => 'Contact Form Entry',
'menu_icon' => 'dashicons-media-document',
]
];
register_post_type('demo-plugin', $args);
}
public function register_rest_api()
{
register_rest_route('demo-plugin-form/v1', 'send-email', [
'methods' => 'POST',
'callback' => [$this, 'handle_contact_form']
]);
}
public function handle_contact_form($data)
{
$headers = $data->get_headers();
$params = $data->get_params();
$nonce = $headers['x_wp_nonce'][0];
if(!wp_verify_nonce($nonce, 'wp_rest'))
{
return new WP_REST_Response('Message not sent', 422);
}
$name = sanitize_text_field($params['name']);
$email = sanitize_email($params['email']);
$message = sanitize_text_field($params['message']);
$post_id = wp_insert_post([
'post_type' => 'demo-plugin',
'post_title' => "Contact Enquiry from $email",
'post_content' => "$email",
'post_status' => 'publish',
]);
if($post_id)
{
return new WP_REST_Response('Form Submitted successfully', 200);
}
}
}
}
new DemoPlugin();
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand