Thursday, February 7, 2013

To create custom post from the front end with Feature image

  I f you want to create the custom post from the front with   feature  image end need simple wordpress function  wp_insert_posts();

$new_post = array('post_title'=>$title ,'post_content'  =>   $description,'post_category' =>   array($_POST['cat']),'post_status'=>'publish','post_type'=>  'post');
echo  $pid = wp_insert_post($new_post);
here  in the  variable  $title you have to pass the  title of you post and in
$description  the content you  want to upload   for the post
Array($_POST[‘cat’]) you can pass the category name of post in which you want to post

First you need to create simple html form  like this

<table class="accountForm">
<form method="post" name="insert_post_form" action="" enctype="multipart/form-data" onsubmit="return validateForm()">
<td> Post Title: </td> <td> <input type="text" class="inputContact_acc" name="title" ></td>
<td> Post content: </td> <td><input type="text" class="inputContact_acc" name="content"></td>
<td> Post category:</td> <td><div class="input_bg1" style="margin-top:5px;"><?php wp_dropdown_categories( 'tab_index=10&taxonomy=category&hide_empty=0&include=4' ); ?></div></td>

<td> Feature Image : </td> <td> <input name="Feture_image" class="inputContact_acc"  type="file"></td>
<td>        <input type="submit" name="posted" value="Insert Post" style="background:black;color:#AAAAAA;border:0;width:110px;height:30px;font-weight:bold;border-radius:10px;"></td>


Now for the  php code you need  to get these values

if(isset($_POST['posted'])) {

$allowedExts = array("JPG","jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["Feture_image"]["name"]));
if ((($_FILES["Feture_image"]["type"] == "image/gif")
||   ($_FILES["Feture_image"]["type"] == "image/jpeg")
||   ($_FILES["Feture_image"]["type"] == "image/png")
||   ($_FILES["Feture_image"]["type"] == "image/jpg"))
&& in_array($extension, $allowedExts) ) {


$new_post = array('post_title'=>$title ,'post_content'  =>   $description,'post_category' =>   array($_POST['cat']),'post_status'=>'publish','post_type'=>  'post');
echo  $pid = wp_insert_post($new_post);

add_theme_support( 'post-thumbnails', array( 'post' ) );

$upload_dir = wp_upload_dir();
$dir = trailingslashit($upload_dir['basedir']) . 'lostpets';
$tmppath = $_FILES['Feture_image']['tmp_name'];

if(!file_exists($dir) && @!mkdir($dir, 0777))
$error = sprintf(__("The userphoto upload content directory does not exist and could not be created. Please ensure that you have write permissions for the '%s' directory. Did you put slash at the beginning of the upload path in Misc. settings? It should be a path relative to the WordPress root directory. <code>wp_upload_dir()</code> returned:<br /> <code style='white-space:pre'>%s</code>", 'user-photo'), $dir, print_r($upload_dir, true));
$imagefile = "$pid." .$_FILES['Feture_image']['name'];
$imagepath = $dir . '/' . $imagefile;
if(!move_uploaded_file($tmppath, $imagepath)){
$error = sprintf(__("Unable to place the user photo at: %s", 'Feture_image'), $imagepath);
$wp_filetype = wp_check_filetype(basename($imagepath));

$attachment = array(
'guid' => $imagepath,
'post_type'=>       'attachment',
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($imagepath)),
'post_content' => '',
'post_status' => 'inherit'
$attach_id = wp_insert_attachment( $attachment, $imagepath, $pid );

require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $imagepath );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $pid, $attach_id );
echo $error;

echo "<script>alert('you successfully posted your deal')</script>";


echo "Try valid image type";


No comments:

Post a Comment