You want to create a plugin, all you need to create a folder and then create a single file with one line of content. Navigate to the wp-content/plugins folder, and create a new folder named contact . Inside this folder, create a file name contact.php and then after you need to information of your plugin.

/*
Plugin Name: Contact Form
Description: Create simple contact form store data
Plugin URI: https://wptreasures.wordpress.com/
Author URI: https://wptreasures.wordpress.com/
Author: Pratik Mungalpara
Version: 1.0
*/

function contact_install()
{
global $wpdb;

$table_name = $wpdb->prefix . ‘contact’;

$sql = “CREATE TABLE IF NOT EXISTS ” . $table_name . ” (
id int(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
email VARCHAR(200) NOT NULL,
subject VARCHAR(200) NOT NULL,
message VARCHAR(200) NOT NULL,
PRIMARY KEY (id)
);”;
require_once(ABSPATH . ‘wp-admin/includes/upgrade.php’);
dbDelta($sql);
}
register_activation_hook(__FILE__, ‘contact_install’);

/**
* register_activation_hook implementation
* [OPTIONAL]
* additional implementation of register_activation_hook
* to insert some dummy data
*/
function contact_install_data()
{
global $wpdb;

$table_name = $wpdb->prefix . ‘contact’; // do not forget about tables prefix

$wpdb->insert($table_name, array(
‘name’ => ‘Alex’,
’email’ => ‘alex@example.com’,
‘subject’ => ‘test’,
‘message’ => ‘test message’
));
$wpdb->insert($table_name, array(
‘name’ => ‘Maria’,
’email’ => ‘maria@example.com’,
‘subject’ => ‘test’,
‘message’ => ‘test message’
));
}
register_activation_hook(__FILE__, ‘contact_install_data’);

if (!class_exists(‘WP_List_Table’)) {
require_once(ABSPATH . ‘wp-admin/includes/class-wp-list-table.php’);
}

class Contact_List_Table extends WP_List_Table
{
function __construct()
{
global $status, $page;

parent::__construct(array(
‘singular’ => ‘contact’,
‘plural’ => ‘contacts’,
));
}
function column_default($item, $column_name)
{
return $item[$column_name];
}

function column_name($item)
{
// links going to /admin.php?page=[your_plugin_page][&other_params]
// notice how we used $_REQUEST[‘page’], so action will be done on curren page
$actions = array(
‘edit’ => sprintf(‘<a href=”?page=contacts_form&id=%s”>%s</a>’, $item[‘id’], __(‘Edit’, ‘contact_form’)),
‘delete’ => sprintf(‘<a href=”?page=%s&action=delete&id=%s”>%s</a>’, $_REQUEST[‘page’], $item[‘id’], __(‘Delete’, ‘contact_form’)),
);

return sprintf(‘%s %s’,
$item[‘name’],
$this->row_actions($actions)
);
}
function column_cb($item)
{
return sprintf(
‘<input type=”checkbox” name=”id[]” value=”%s” />’,
$item[‘id’]
);
}
function get_columns()
{
$columns = array(
‘cb’ => ‘<input type=”checkbox” />’, //Render a checkbox instead of text
‘name’ => __(‘Name’, ‘contact_form’),
’email’ => __(‘E-Mail’, ‘contact_form’),
‘subject’ => __(‘Subject’, ‘contact_form’),
‘message’ => __(‘Message’, ‘contact_form’),
);
return $columns;
}
function get_sortable_columns()
{
$sortable_columns = array(
‘name’ => array(‘name’, true),
’email’ => array(’email’, false),
‘subject’ => array(‘subject’, false),
‘message’ => array(‘message’, false),
);
return $sortable_columns;
}
function get_bulk_actions()
{
$actions = array(
‘delete’ => ‘Delete’
);
return $actions;
}
function process_bulk_action()
{
global $wpdb;
$table_name = $wpdb->prefix . ‘contact’; // do not forget about tables prefix

if (‘delete’ === $this->current_action()) {
$ids = isset($_REQUEST[‘id’]) ? $_REQUEST[‘id’] : array();
if (is_array($ids)) $ids = implode(‘,’, $ids);

if (!empty($ids)) {
$wpdb->query(“DELETE FROM $table_name WHERE id IN($ids)”);
}
}
}
function prepare_items()
{
global $wpdb;
$table_name = $wpdb->prefix . ‘contact’; // do not forget about tables prefix

$per_page = 5; // constant, how much records will be shown per page

$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();

// here we configure table headers, defined in our methods
$this->_column_headers = array($columns, $hidden, $sortable);

// [OPTIONAL] process bulk action if any
$this->process_bulk_action();

$total_items = $wpdb->get_var(“SELECT COUNT(id) FROM $table_name”);

$paged = isset($_REQUEST[‘paged’]) ? max(0, intval($_REQUEST[‘paged’]) – 1) : 0;
$orderby = (isset($_REQUEST[‘orderby’]) && in_array($_REQUEST[‘orderby’], array_keys($this->get_sortable_columns()))) ? $_REQUEST[‘orderby’] : ‘name’;
$order = (isset($_REQUEST[‘order’]) && in_array($_REQUEST[‘order’], array(‘asc’, ‘desc’))) ? $_REQUEST[‘order’] : ‘asc’;

$this->items = $wpdb->get_results($wpdb->prepare(“SELECT * FROM $table_name ORDER BY $orderby $order LIMIT %d OFFSET %d”, $per_page, $paged), ARRAY_A);

$this->set_pagination_args(array(
‘total_items’ => $total_items, // total items defined above
‘per_page’ => $per_page, // per page constant defined at top of method
‘total_pages’ => ceil($total_items / $per_page) // calculate pages count
));
}
}

function contact_admin_menu()
{
add_menu_page(__(‘Contacts’, ‘contact_form’), __(‘Contacts’, ‘contact_form’), ‘activate_plugins’, ‘contacts’, ‘contacts_page_handler’);
add_submenu_page(‘contacts’, __(‘Contacts’, ‘contact_form’), __(‘Contacts’, ‘contact_form’), ‘activate_plugins’, ‘contacts’, ‘contacts_page_handler’);
// add new will be described in next part
add_submenu_page(‘contacts’, __(‘Add new’, ‘contact_form’), __(‘Add new’, ‘contact_form’), ‘activate_plugins’, ‘contacts_form’, ‘contacts_form_page_handler’);
}
add_action(‘admin_menu’, ‘contact_admin_menu’);

function contacts_page_handler()
{
global $wpdb;

$table = new Contact_List_Table();
$table->prepare_items();

$message = ”;
if (‘delete’ === $table->current_action()) {
$message = ‘

‘ . sprintf(__(‘Items deleted: %d’, ‘contact_form’), count($_REQUEST[‘id’])) . ‘

‘;
}
?>

<h2><?php _e(‘Contacts’, ‘contact_form’)?> <a class=”add-new-h2″
href=”<?php echo get_admin_url(get_current_blog_id(), ‘admin.php?page=contacts_form’);?>”><?php _e(‘Add new’, ‘contact_form’)?></a>
</h2>
<?php echo $message; ?>

<form id=”contacts-table” method=”GET”>
<input type=”hidden” name=”page” value=”<?php echo $_REQUEST[‘page’] ?>”/>
<?php $table->display() ?>
</form>

</div>
<?php
}

function contacts_form_page_handler()
{
global $wpdb;
$table_name = $wpdb->prefix . ‘contact’; // do not forget about tables prefix

$message = ”;
$notice = ”;

// this is default $item which will be used for new records
$default = array(
‘id’ => 0,
‘name’ => ”,
’email’ => ”,
‘subject’ => ”,
‘message’ => ”,
);

if (wp_verify_nonce($_REQUEST[‘nonce’], basename(__FILE__))) {

$item = shortcode_atts($default, $_REQUEST);
// validate data, and if all ok save item to database
// if id is zero insert otherwise update
$item_valid = contact_validate_person($item);
if ($item_valid === true) {
if ($item[‘id’] == 0) {
$result = $wpdb->insert($table_name, $item);
$item[‘id’] = $wpdb->insert_id;
if ($result) {
$message = __(‘Item was successfully saved’, ‘contact_form’);
} else {
$notice = __(‘There was an error while saving item’, ‘contact_form’);
}
} else {
$result = $wpdb->update($table_name, $item, array(‘id’ => $item[‘id’]));
if ($result) {
$message = __(‘Item was successfully updated’, ‘contact_form’);
} else {
$notice = __(‘There was an error while updating item’, ‘contact_form’);
}
}
} else {

$notice = $item_valid;
}
}
else {

$item = $default;
if (isset($_REQUEST[‘id’])) {
$item = $wpdb->get_row($wpdb->prepare(“SELECT * FROM $table_name WHERE id = %d”, $_REQUEST[‘id’]), ARRAY_A);
if (!$item) {
$item = $default;
$notice = __(‘Item not found’, ‘contact_form’);
}
}
}

// here we adding our custom meta box
add_meta_box(‘contacts_form_meta_box’, ‘Contact data’, ‘contacts_form_meta_box_handler’, ‘contact’, ‘normal’, ‘default’);

?>

<h2><?php _e(‘Contact’, ‘contact_form’)?> <a class=”add-new-h2″
href=”<?php echo get_admin_url(get_current_blog_id(), ‘admin.php?page=contacts’);?>”><?php _e(‘back to list’, ‘contact_form’)?></a>
</h2>

<?php if (!empty($notice)): ?>

<?php endif;?>
<?php if (!empty($message)): ?>

<?php endif;?>

<form id=”form” method=”POST”>
<input type=”hidden” name=”nonce” value=”<?php echo wp_create_nonce(basename(__FILE__))?>”/>
<?php /* NOTICE: here we storing id to determine will be item added or updated */ ?>
<input type=”hidden” name=”id” value=”<?php echo $item[‘id’] ?>”/>

” id=”submit” class=”button-primary” name=”submit”>

</div>
</div>
</form>
</div>
<?php
}

function contacts_form_meta_box_handler($item)
{
?>

<table cellspacing=”2″ cellpadding=”5″ style=”width: 100%;” class=”form-table”>
<tbody>
<tr class=”form-field”>
<th valign=”top” scope=”row”>
<label for=”name”><?php _e(‘Name’, ‘contact_form’)?></label>
</th>
<td>
<input id=”name” name=”name” type=”text” style=”width: 95%” value=”<?php echo esc_attr($item[‘name’])?>”
size=”50″ class=”code” placeholder=”<?php _e(‘Your name’, ‘contact_form’)?>” >
</td>
</tr>
<tr class=”form-field”>
<th valign=”top” scope=”row”>
<label for=”email”><?php _e(‘E-Mail’, ‘contact_form’)?></label>
</th>
<td>
<input id=”email” name=”email” type=”email” style=”width: 95%” value=”<?php echo esc_attr($item[’email’])?>”
size=”50″ class=”code” placeholder=”<?php _e(‘Your E-Mail’, ‘contact_form’)?>”>
</td>
</tr>
<tr class=”form-field”>
<th valign=”top” scope=”row”>
<label for=”subject”><?php _e(‘Subject’, ‘contact_form’)?></label>
</th>
<td>
<input id=”subject” name=”subject” type=”text” style=”width: 95%” value=”<?php echo esc_attr($item[‘subject’])?>”
size=”50″ class=”code” placeholder=”<?php _e(‘Your Subject’, ‘contact_form’)?>”>
</td>
</tr>
<tr class=”form-field”>
<th valign=”top” scope=”row”>
<label for=”message”><?php _e(‘Message’, ‘contact_form’)?></label>
</th>
<td>
<textarea id=”message” name=”message” type=”textarea” style=”width: 95%”
size=”50″ class=”code” placeholder=”<?php _e(‘Your Message’, ‘contact_form’)?>” ><?php echo esc_attr($item[‘message’])?></textarea>
</td>
</tr>
</tbody>
</table>
<?php
}

function contact_validate_person($item)
{
$messages = array();

if (empty($item[‘name’])) $messages[] = __(‘Name is required’, ‘contact_form’);
if (preg_match(“/[^A-Za-z’-]/”, $item[‘name’])) { $messages[] = __(‘Please enter a valid name’); }
if(empty($item[’email’]))
{
$messages[] = __(‘E-Mail is required’, ‘contact_form’);
}
else
{
if (!is_email($item[’email’])) $messages[] = __(‘E-Mail is in wrong format’, ‘contact_form’);
}
if (empty($item[‘subject’])) $messages[] = __(‘Subject is required’, ‘contact_form’);
if (empty($item[‘message’])) $messages[] = __(‘Message is required’, ‘contact_form’);

if (empty($messages)) return true;
return implode(‘<br />’, $messages);
}

function contact_insert_into_db()
{
global $wpdb;
// creates contact table in database if not exists
$table = $wpdb->prefix . “contact”;
$charset_collate = $wpdb->get_charset_collate();
$sql = “CREATE TABLE IF NOT EXISTS $table (
id int(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
email VARCHAR(200) NOT NULL,
subject VARCHAR(200) NOT NULL,
message VARCHAR(200) NOT NULL,
PRIMARY KEY (id)
);”;
require_once( ABSPATH . ‘wp-admin/includes/upgrade.php’ );
dbDelta( $sql );
// starts output buffering
ob_start();
?>
<form action=”#c_form” method=”post” id=”c_form”>
<input id=”visitor_name” name=”visitor_name” placeholder=”Your Name” type=”text” required >
<input id=”email” name=”email” placeholder=”Your Email” type=”email” required >
<input id=”subject” name=”subject” placeholder=”Your Subject” type=”text” required >
<textarea id=”message” name=”message” placeholder=”Your Message” required ></textarea>
<input id=”submit” name=”submit” value=”Send” type=”submit”>
</form>
<?php
$html = ob_get_clean();
// does the inserting, in case the form is filled and submitted
if ( isset( $_POST[“submit”] ) ) {
$table = $wpdb->prefix.”contact”;
$name = $_POST[“visitor_name”];
$email = $_POST[“email”];
$subject = $_POST[“subject”];
$message = $_POST[“message”];
$wpdb->insert(
$table,
array(
‘name’ => $name,
’email’ => $email,
‘subject’ => $subject,
‘message’ => $message
)
);
$html = “<p>Your message was successfully send.</p>”;
}
// outputs everything
return $html;

}

add_shortcode(‘contact-form’, ‘contact_insert_into_db’);

Advertisements