Wednesday, October 10, 2018

Front End Post Submission Form in WordPress


The process for creating a front-end submission form for posts, pages, or custom post types is actually quite simple, even though it is often thought of as being very difficult. There are just two major components to a front-end submission form:
  1. An HTML form for the data to be entered into
  2. A function that listens for the form submission and then processes the submitted data
The code written in the video, which consists of just two functions, is less than 80 lines and contains everything needed for a pretty decent front-end submission form, even if it is pretty basic. It introduces everything you need to know for getting started with building these kinds of forms and opens up the door to countless possibilities.
<?php
function create_post_form() {
ob_start();
if(isset($_GET['post'])) {
switch($_GET['post']) {
case 'successfull':
echo '<p class="success">' . __('Job created', 'gocloud') . '</p>';
break;
case 'failed' :
echo '<p class="error">' . __('Please fill in all the info', 'gocloud') . '</p>';
break;
}
}
?>
<form id="pippin_create_post" action="" method="POST">
<fieldset>
<input name="job_title" id="job_title" type="text"/>
<label for="job_title">Job Title</label>
</fieldset>
<fieldset>
<input name="user_name" id="user_name" type="text"/>
<label for="user_name">Your Name</label>
</fieldset>
<fieldset>
<input name="user_email" id="user_email" type="email"/>
<label for="user_email">Your Email</label>
</fieldset>
<fieldset>
<label for="job_desc">Job Description</label>
<textarea name="job_desc" id="job_desc"></textarea>
</fieldset>
<fieldset>
<input name="inquiry_email" id="inquiry_email" type="email"/>
<label for="inquiry_email">Inquiry Email</label>
</fieldset>
<fieldset>
<?php wp_nonce_field('jobs_nonce', 'jobs_nonce_field'); ?>
<input type="submit" name="job_submit" value="<?php _e('Submit Job Posting', 'gocloud'); ?>"/>
</fieldset>
</form>
<?php
return ob_get_clean();
}
add_shortcode('post_form', 'create_post_form');

function process_post_creation() {
if(isset($_POST['jobs_nonce_field']) && wp_verify_nonce($_POST['jobs_nonce_field'], 'jobs_nonce')) {

if(strlen(trim($_POST['job_title'])) < 1 || strlen(trim($_POST['job_desc'])) < 1) {
$redirect = add_query_arg('post', 'failed', home_url($_POST['_wp_http_referer']));
} else {
$job_info = array(
'post_title' => esc_attr(strip_tags($_POST['job_title'])),
'post_type' => 'jobs',
'post_content' => esc_attr(strip_tags($_POST['job_desc'])),
'post_status' => 'pending'
);
$job_id = wp_insert_post($job_info);

if($job_id) {
update_post_meta($job_id, 'ecpt_postedby', esc_attr(strip_tags($_POST['user_name'])));
update_post_meta($job_id, 'ecpt_posteremail', esc_attr(strip_tags($_POST['user_email'])));
update_post_meta($job_id, 'ecpt_contactemail', esc_attr(strip_tags($_POST['inquiry_email'])));
$redirect = add_query_arg('post', 'successfull', home_url($_POST['_wp_http_referer']));
}
}
wp_redirect($redirect); exit;
}
}
add_action('init', 'process_post_creation');
?>

Note: This article is taken from (https://pippinsplugins.com/custom-front-end-post-submission-form/)

How to backup and download Database using PHP

< ?php $mysqlUserName = 'databaseusername' ; $mysqlPassword = 'databasepassword' ; $mysqlHostNa...