I have been struggling for days to figure this one out. I have successfully implemented a form to submit posts from the front end of my site. ButI can't figure out how to make the image uploads work.
I want whatever image the user uploads to automatically attach to the post and become its featured image.
Can someone please explain how to do this with my existing code?
I have searched through all relevant questions on this forum and none of the solutions work for me.
<?
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['insert_post'] )) { //check that our form was submitted
//If it has, redirect to the dashboard
?><script language="javascript"><!--
location.replace("/dashboard/")
//-->
</script> <?php
$title = $_POST['thread_title']; //set our title
if ($_POST['thread_description']=="") { // check if a description was entered
$description = "See thread title..."; // if not, use placeholder
} else {
$description = $_POST['thread_description']; //if so, use it
}
$tags = $_POST['thread_tags']; //load thread tags (custom tax) into array
$post = array( //our wp_insert_post args
'post_title' => wp_strip_all_tags($title),
'post_content' => $description,
'post_category' => array('0' => $_POST['cat']),
'tax_input' => array('thread_tag' => $tags),
'post_status' => 'publish',
'post_type' => 'tsv_userpost'
);
$my_post_id = wp_insert_post($post); //send our post, save the resulting ID
$current_user = wp_get_current_user(); //check who is logged in
add_post_meta($my_post_id, '_your_custom_meta', $var); //add custom meta data, after the post is inserted
} else {
if(is_user_logged_in()) { // check that the user is logged in before presenting form
$current_user = wp_get_current_user();
?>
<div id="postbox">
<form id="new_thread" name="new_thread" method="post" action="" enctype="multipart/form-data">
<input class="required" type="text" id="thread_title" value="" tabindex="1" name="thread_title" placeholder="Thread Title" />
<textarea id="thread_description" name="thread_description" cols="80" rows="20" tabindex="2"></textarea>
<div class="left">
<select name='cat' id='cat' class='postform required' tabindex="3">
<option value="" selected="selected">Choose:</option>
<option class="writing" value="90">Writing</option>
<option class="image" value="91">Image</option>
<option class="video" value="92">Video</option>
<option class="audio" value="94">Audio</option>
<option class="link" value="95">Link</option>
</select>
</div>
<input type="text" value="" tabindex="4" size="16" name="thread_tags" id="thread_tags" placeholder="Tags" />
<br />
<!-- images -->
<label for="images">Featured Image:</label>
<input type="file" name="user-image-featured" id="user-image-featured" size="20">
<!-- Submit button-->
<br />
<input type="submit" value="Save Post" tabindex="5" id="thread_submit" name="thread_submit" class="thread-button" />
<input type="hidden" name="insert_post" value="post" />
<?php wp_nonce_field( 'new_thread' ); ?>
</form>
</div>
<?php } else { echo 'please login'; } } ?>
I have been struggling for days to figure this one out. I have successfully implemented a form to submit posts from the front end of my site. ButI can't figure out how to make the image uploads work.
I want whatever image the user uploads to automatically attach to the post and become its featured image.
Can someone please explain how to do this with my existing code?
I have searched through all relevant questions on this forum and none of the solutions work for me.
<?
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['insert_post'] )) { //check that our form was submitted
//If it has, redirect to the dashboard
?><script language="javascript"><!--
location.replace("/dashboard/")
//-->
</script> <?php
$title = $_POST['thread_title']; //set our title
if ($_POST['thread_description']=="") { // check if a description was entered
$description = "See thread title..."; // if not, use placeholder
} else {
$description = $_POST['thread_description']; //if so, use it
}
$tags = $_POST['thread_tags']; //load thread tags (custom tax) into array
$post = array( //our wp_insert_post args
'post_title' => wp_strip_all_tags($title),
'post_content' => $description,
'post_category' => array('0' => $_POST['cat']),
'tax_input' => array('thread_tag' => $tags),
'post_status' => 'publish',
'post_type' => 'tsv_userpost'
);
$my_post_id = wp_insert_post($post); //send our post, save the resulting ID
$current_user = wp_get_current_user(); //check who is logged in
add_post_meta($my_post_id, '_your_custom_meta', $var); //add custom meta data, after the post is inserted
} else {
if(is_user_logged_in()) { // check that the user is logged in before presenting form
$current_user = wp_get_current_user();
?>
<div id="postbox">
<form id="new_thread" name="new_thread" method="post" action="" enctype="multipart/form-data">
<input class="required" type="text" id="thread_title" value="" tabindex="1" name="thread_title" placeholder="Thread Title" />
<textarea id="thread_description" name="thread_description" cols="80" rows="20" tabindex="2"></textarea>
<div class="left">
<select name='cat' id='cat' class='postform required' tabindex="3">
<option value="" selected="selected">Choose:</option>
<option class="writing" value="90">Writing</option>
<option class="image" value="91">Image</option>
<option class="video" value="92">Video</option>
<option class="audio" value="94">Audio</option>
<option class="link" value="95">Link</option>
</select>
</div>
<input type="text" value="" tabindex="4" size="16" name="thread_tags" id="thread_tags" placeholder="Tags" />
<br />
<!-- images -->
<label for="images">Featured Image:</label>
<input type="file" name="user-image-featured" id="user-image-featured" size="20">
<!-- Submit button-->
<br />
<input type="submit" value="Save Post" tabindex="5" id="thread_submit" name="thread_submit" class="thread-button" />
<input type="hidden" name="insert_post" value="post" />
<?php wp_nonce_field( 'new_thread' ); ?>
</form>
</div>
<?php } else { echo 'please login'; } } ?>
you can do that by running the function
set_post_thumbnail( $my_post_id, $thumbnail_id );
remember, you have to process and insert the image into the library first:
$uploaddir = wp_upload_dir();
$file = $_FILES[ ... whatever you have in your POST data ... ];
$uploadfile = $uploaddir['path'] . '/' . basename( $file );
move_uploaded_file( $file , $uploadfile );
$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',
'menu_order' => $_i + 1000
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );
did not test the script - it is adapted from a version i created. maybe you want to change the filename and stuff like that, but all in all it works this way :)
This is working correctly !
$uploaddir = wp_upload_dir();
$file = $_FILES["post_Fimage"]["name"];
$uploadfile = $uploaddir['path'] . '/' . basename( $file );
move_uploaded_file( $_FILES["post_Fimage"]["tmp_name"] , $uploadfile );
$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',
'menu_order' => $_i + 1000
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );
//echo "<pre>";print_r($wp_filetype);echo "</pre>";
//echo "<pre>";print_r($_FILES);echo "</pre>";
set_post_thumbnail( $post_id, $attach_id );
/******* This is working for me ! ********/
$uploaddir = wp_upload_dir();
$file = $_FILES["post_Fimage"]["name"];
$uploadfile = $uploaddir['path'] . '/' . basename( $file );
move_uploaded_file( $_FILES["post_Fimage"]["tmp_name"] , $uploadfile );
$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',
'menu_order' => $_i + 1000
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );
set_post_thumbnail( $post_id, $attach_id );
Can I fix your code ?
$uploaddir = wp_upload_dir();
$file = $_FILES[ ... whatever you have in your POST data ... ];
$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(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
'post_content' => '',
'post_status' => 'inherit',
'menu_order' => $_i + 1000
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );
You might want to try checking the recapcha box first if you are using recapcha. The recapcha will prevent any post or get
I have the same issue. The featured image is not correctly imported at the post that just is submitted front-end. I can see the image in the media library, so I know it works till that point.
This function works till '//function works till here'. Can someone help me out?
function save_post ( $form ){
$errors = array();
$form_setting = $form->get_settings();
$form_fields = $form->get_fields();
$form_fields_files = $form->get_uploaded_files();
$form_id = $form_fields['formId'];
if ($form_id === 'fxuqcf') {
$post_title = isset($form_fields['post-titel']) ? $form_fields['post-titel'] : '';
$post_content = isset($form_fields['post-bericht']) ? $form_fields['post-bericht'] : '';
$post_category_name = isset($form_fields['post-categorie']) ? $form_fields['post-categorie'] : '';}
//get category name
$post_category_id = get_category_by_slug($post_category_name)->term_id;
if (!$post_category_id) {
$errors[] = 'invalid category name';
return $errors;}
//create the post array
$new_post = array(
'post_type' => 'post',
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_category' => array($post_category_id),);
//inserting post
$post_id = wp_insert_post($new_post);
//check if post is correctly created
if ($post_id && is_wp_error($post_id)){
//function works till here
if (!empty($form_fields_files['post-afbeelding'])){
$uploaded_files = $form_fields_files['post-afbeelding'];
$file = $uploaded_files[0];
$file_array = array(
'name' => basename($file['file']),
'tmp-name' => $file['file']);
if (file_exist($file['file'])){
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_id = media_handle_sideload($file_array, $post_id);
if (is_wp_error($attachment_id)){
$errors[] = 'There is an error with the image:' . $attachment_id->get_error_message();
}else {
$attachment_id = wp_insert_attachment();
set_post_thumbnail($post_id, $attachment_id);
}
}else {
$errors[] = 'Uploaded file does not exist.' ;
}
}
}
}