Last active
September 19, 2018 16:45
-
-
Save ebi3102/7027a6dc81a7d713bd6a04f7f475ca25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Template Name: Insert Itinerary Front End Page Layout | |
*Itinerary is a Custome Post Tyape that named itineraries | |
* @author Ebrahim Moeini | |
* @since 1.0.0 | |
*below site help us to create this page | |
*https://tutsplus.com/authors/vinny-singh?_ga=2.94441311.216962670.1530158710-1100091547.1527395103 | |
*/ | |
?> | |
<?php | |
defined( 'ABSPATH' ) || exit; | |
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
require_once( ABSPATH . 'wp-admin/includes/file.php'); | |
require_once( ABSPATH . 'wp-admin/includes/media.php'); | |
//PHP Form Validation | |
$postTitleError = ''; | |
if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) { | |
if ( trim( $_POST['postTitle'] ) === '' ) { | |
$postTitleError = 'Please enter a title.'; | |
$hasError = true; | |
} | |
} | |
//end PHP Form Validation | |
//Featured Image Upload | |
$uploadDir = wp_upload_dir(); | |
$pName= $_POST['postTitle']; | |
$file = $_FILES['userFimg']; | |
$upload_overrides = array( 'test_form' => false); | |
$moveFile = wp_handle_upload($file , $upload_overrides);//wordpress function sanitizing file names, checking extensions for mime type, and moving the file to the appropriate directory within the uploads directory. | |
$uploadFile = $uploadDir['path'] . '/' . basename($file['name']); | |
move_uploaded_file($file['tmp_name'] , $uploadFile) ; | |
$fileName = basename( $uploadFile ); | |
$wp_filetype = wp_check_filetype(basename($fileName), null ); | |
$attachment = array( | |
'guid' => $uploadFile, | |
'post_mime_type' => $wp_filetype['type'], | |
'post_title' => $fileName, | |
'post_content' => '', | |
'post_status' => 'inherit', | |
'post_type' => 'attachment', | |
'menu_order' => '0' | |
); | |
//submitting ittinerary | |
$post_information = array( | |
'post_title' => wp_strip_all_tags($_POST['postTitle']), | |
'post_content' => $_POST['postContent'], | |
'post_type' => 'itineraries', | |
'post_status' => 'publish', | |
'post_author' => $user_id, | |
// 'post_category' => | |
); | |
$attach_id = wp_insert_attachment( $attachment, $uploadFile);//create post for attachment in wp_posts table into DB | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $uploadFile ); //This function generates metadata for an image attachment. It also creates a thumbnail and other intermediate sizes of the image attachment based on the sizes defined on the Settings_Media_Screen. | |
$res1= wp_update_attachment_metadata( $attach_id, $attach_data ); | |
$post_id = wp_insert_post( $post_information ); | |
set_post_thumbnail( $post_id, $attach_id );//create a row in wp_post_meta table into DB that its post_id is "$post_id" its meta_key is "_thumbnail_id" and its meta_value is "$attach_id" | |
// if post created redirct to the home page | |
if ( $post_id) { | |
update_post_meta( $post_id, 'first-name',esc_attr(strip_tags($_POST['first-name']))); | |
update_post_meta( $post_id, 'last-name',esc_attr(strip_tags($_POST['last-name']))); | |
wp_redirect( home_url() ); | |
exit; | |
} | |
?> | |
<?php | |
get_header(); | |
?> | |
<?php | |
//if user not login coulde not insert post | |
global $user_login; | |
if(! $user_login){ | |
echo "You are Not Log in" ; | |
}else { | |
?> | |
<div class="row"> | |
<!-- if form has error --> | |
<?php if ( $postTitleError != '' ) { ?> | |
<span class="error"><?php echo $postTitleError; ?></span> | |
<div class="clearfix"></div><br> | |
<?php } ?> | |
<form action="" id="primaryPostForm" method="POST" enctype="multipart/form-data"> | |
<aside class="col-9 left-side"> | |
<fieldset> | |
<label for="postTitle"><?php _e('Post Title:', 'framework') ?></label> | |
<input type="text" name="postTitle" id="postTitle" value="<?php if ( isset( $_POST['postTitle'] ) ) echo $_POST['postTitle']; ?>" class="required" /> | |
</fieldset> | |
<fieldset> | |
<label for="postContent"><?php _e('Post Content:', 'framework') ?></label> | |
<textarea name="postContent" id="postContent" rows="8" cols="30" class="required"><?php if ( isset( $_POST['postContent'] ) ) { if ( function_exists( 'stripslashes' ) ) { echo stripslashes( $_POST['postContent'] ); } else { echo $_POST['postContent']; } } ?></textarea> | |
</fieldset> | |
</aside><!-- End of left-side class --> | |
<aside class="col-3 right-side"></aside> | |
<fieldset> | |
<figure class="insert-post-thumbnail"> | |
<img src="<?php echo $uploadFile ; ?>"><!-- need to ajax --> | |
</figure> | |
<label for="user-image-featured">Featured Image:</label> | |
<input type="file" name="userFimg" id="user-image-featured"> | |
</fieldset> | |
<fieldset> | |
<?php wp_list_categories(); ?> | |
</fieldset> | |
<fieldset> | |
<label for="first-name"><?php _e('First Name:', 'framework') ?></label> | |
<input type="text" name="first-name" id="first-name" value="<?php if(isset($_POST['first-name'])) echo $_POST['first-name'];?>" class="required" required /> | |
</fieldset> | |
<fieldset> | |
<label for="last-name"><?php _e('Last Name: ', 'framework') ?></label> | |
<input type="text" name="last-name" id="last-name" value="<?php if(isset($_POST['last-name'])) echo $_POST['last-name'];?>" class="required" /> | |
</fieldset> | |
</aside><!-- End of right-side class --> | |
<!-- for validate the form by wordpress --> | |
<div class="clear-both"></div> | |
<?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?> | |
<div class="col-12"> | |
<fieldset> | |
<input type="submit" name="submitted" id="submitted" value="<?php _e('Add Post', 'framework') ?>" /> | |
</fieldset> | |
</div> | |
</form> | |
<?php } ?> | |
</div><!-- end of row class --> | |
<?php | |
get_footer(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment