Pages

Wednesday, March 6, 2013

Add wordpress post from front-end with featured images and send mail [resolved]


/*Create Form*/

<form class="story" name="share_story" action="" method="POST"  id="story_form" enctype="multipart/form-data">
    <label>Name</label><br />
    <input type="text" class="flat_input_box" name="share_name" id="share_name" /><br />  
    <label>Email address</label><br />
    <input type="text" class="flat_input_box" name="share_email_address" id="email_address" /><br />  
    <label>Upload Image</label><br />
    <input type="file" name="story_image" class="flat_input_box" id="featured_image" /><br />
    <label>Story Title</label><br />
    <input type="text" class="flat_input_box" name="story_title" id="story_title" /><br />
    <textarea class="flat_input_box" name="story_content" id="story_content"></textarea><br />  
    <div class="right">
        <a href="#">Cancel</a>
     <input class="blue_btn" type="submit" value="Submit" onclick="return CheckStoryForm();" name="submit" />
    </div>
</form>

/*Create Form */

/*Javascript validation Starts*/

<script>
function CheckStoryForm(){
var result = true;
var emailcheck = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var emailval = document.share_story.email_address.value;
if(document.share_story.name.value==''){
alert("Please enter name");
return false;
}
if(document.share_story.email_address.value==''){
alert("Please enter email address");
return false;
}
else if(!emailcheck.test(emailval))
{
alert('Please enter valid email address');
return false;
}

var fup = document.getElementById('featured_image');
var fileName = fup.value;
if(fileName != ''){
var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
if(ext == "gif" || ext == "GIF" || ext == "JPEG" || ext == "jpeg" || ext == "jpg" || ext == "JPG" || ext == "png" || ext == "PNG")
{
return true;
}
else{
alert("Please upload image with jpg, png, gif, jpeg formate.");
fup.focus();
return false;
}
}
if(document.share_story.story_title.value==''){
alert("Please enter title of story");
return false;
}

return true;
}
</script>

/*Javascript validation Ends*/


/*Post Submission Starts*/
if(isset($_POST['submit'])){

$name = $_POST['share_name'];
$email_address = $_POST['share_email_address'];
$story_title = $_POST['story_title'];
$story_content = $_POST['story_content'];
$admin_email = get_settings('admin_email');

/*custom post type*/
$new_post = array(
'post_title' => $story_title,
'post_content' => $story_content,
'post_status' => 'private',           // Choose: publish, preview, future, draft, etc.
'post_type' => 'share_your_story'  //'post',page' or use a custom post type if you want to
);
$pid = wp_insert_post($new_post);
/*custom post type*/

/*create and upload featured image with multiple resolution*/
$MyImage = '';
if(!empty($_FILES['story_image']['name'])){

include("simpleImage.php");  // please check http://ewebsurf.blogspot.in/2013/03/simpleimagephp.html from here you can copy the code of this file

$uploaddir = wp_upload_dir(); // get wordpress upload directory
$myDirPath = $uploaddir['path'];
$myDirUrl = $uploaddir['url'];

$MyImage = rand(0,5000).$_FILES['story_image']['name'];
$image_path = $myDirPath.'/'.$MyImage;
copy($_FILES['story_image']['tmp_name'],$image_path);

list($width, $height, $type, $attr) = getimagesize($myDirUrl.'/'.$MyImage); // get image property

$image = new SimpleImage();

$large_width = get_option('large_size_w');    // get large image resolution set as admin panel
$large_height = get_option('large_size_h');
if($width > $large_width || $height > $large_height){

$dimensions = fw_get_dimension_new($image_path, $large_width, $large_height);
$largeimage = $image_path;
$info = pathinfo($largeimage);
$image_name =  basename($largeimage,'.'.$info['extension']);
$ext = end(explode('.', $largeimage));
$newlargeimg = $myDirPath.'/'.$image_name.'-'.$dimensions['width'].'x'.$dimensions['height'].'.'.$ext;
copy($image_path,$newlargeimg);

            $image->load($newlargeimg);
            $image->resize($dimensions['width'],$dimensions['height']);
            $image->save($newlargeimg);
}

$medium_width = get_option('medium_size_w');
$medium_height = get_option('medium_size_h');

if($width > $medium_width || $height > $medium_height){

$dimensions = fw_get_dimension_new($image_path, $medium_width, $medium_height);
$mediumimage = $image_path;
$info = pathinfo($mediumimage);
$image_name =  basename($mediumimage,'.'.$info['extension']);
$ext = end(explode('.', $mediumimage));
$newmediumimg = $myDirPath.'/'.$image_name.'-'.$dimensions['width'].'x'.$dimensions['height'].'.'.$ext;
copy($image_path,$newmediumimg);

            $image->load($newmediumimg);
            $image->resize($dimensions['width'],$dimensions['height']);
            $image->save($newmediumimg);
}

$thumb_width = get_option('thumbnail_size_w');
$thumb_height = get_option('thumbnail_size_h');

if($width > $thumb_width || $height > $thumb_height){

$dimensions = fw_get_dimension_new($image_path, $thumb_width, $thumb_height);
$thumbimage = $image_path;
$info = pathinfo($thumbimage);
$image_name =  basename($thumbimage,'.'.$info['extension']);
$ext = end(explode('.', $thumbimage));
           
$ImageCrop = get_option('thumbnail_crop');
if($ImageCrop == 1){
$newthumbimg = $myDirPath.'/'.$image_name.'-150x150.'.$ext;
copy($image_path,$newthumbimg);

$image->load($newthumbimg);
$image->resize($thumb_width,$thumb_height);
} else {
$newthumbimg = $myDirPath.'/'.$image_name.'-'.$dimensions['width'].'x'.$dimensions['height'].'.'.$ext;
copy($image_path,$newthumbimg);

$image->load($newthumbimg);
$image->resize($dimensions['width'],$dimensions['height']);
}
            $image->save($newthumbimg);
}


$file = $MyImage;
$uploadfile = $myDirPath.'/' . basename( $file );
$filename = basename( $uploadfile );
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
'post_content' => '',
'post_status' => 'inherit',
'post_parent' => $pid
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );

require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $uploadfile );
wp_update_attachment_metadata( $attach_id,  $attach_data );

set_post_thumbnail( $pid, $attach_id );

}

$to = "Your Email Address";
$subject = " New Story ";
$headers = "From: $name <$email_address>\r\n";
$headers .= "MIME-Version: 1.0\r\n"."Content-Type: multipart/mixed; boundary=\"1a2a3a\"";

$message .= "If you can see this MIME than your client doesn't accept MIME types!\r\n"."--1a2a3a\r\n";

$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= '<table>';
$message .= '<tr><td><b>Name</b></td><td>'.$name.'</td></tr>';
$message .= '<tr><td><b>Email</b></td><td>'.$email_address.'</td></tr>';
$message .= '<tr><td><b>Story Title</b></td><td>'.$story_title.'</td></tr>';
$message .= '<tr><td colspan="2"><b>Content</b></td></tr>';
$message .= '<tr><td colspan="2">'.nl2br($story_content).'</td></tr>';
$message .= '</table>';
$message .= "\r\n"."--1a2a3a\r\n";

if($image != ''){
$file = file_get_contents($image_path);

$message .= "Content-Type: image/jpg; name=\"featuredimage.jpg\"\r\n"
  ."Content-Transfer-Encoding: base64\r\n"
  ."Content-disposition: attachment; file=\"$MyImage\"\r\n"
  ."\r\n"
  .chunk_split(base64_encode($file))
  ."--1a2a3a--";
}

if (mail($to, $subject, $message, $headers)) {
echo '<script>document.location="'.get_permalink().'/?msg=1";</script>';
} else {
echo '<script>document.location="'.get_permalink().'/?msg=2";</script>';
}


}

/*Post Submission Ends*/



No comments: