Set uploaded image or image url as featured image
-
I am creating a custom submit form (yes, its heavily customized to suit my needs) but the only problem is, when I try to upload the image (via Dropzone.js) or image url, the image doesn’t upload and it is not set as featured image. The uploader can only upload images 1 at a time. Am I doing something wrong here? Unfortunately, I cannot link it to a website, as this is all done on localhost. Sorry if the code is messy, just making a basic functioning script before I secure it and clean it up.
Here’s the code.
HEADING<script src="<?php echo esc_url( get_template_directory_uri() ); ?>/js/ta.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/dropzone.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/dropzone.css" rel="stylesheet" /> <script type="text/javascript"> Dropzone.autoDiscover = false; Dropzone.options.myAwesomeDropzone = { maxFiles: 1, accept: function(file, done) { console.log("uploaded"); done(); }, init: function() { this.on("maxfilesexceeded", function(file){ alert("No more files please!"); }); } }; $(document).ready(function() { $( "#showurlupload" ).click(function() { $( "#dropupload" ).slideToggle( "slow", function() { // Animation complete. $( "#urlupload" ).slideToggle( "slow", function() { // Animation complete. }); }); }); $("div#myAwesomeDropzone").dropzone({ url: "/file/post" }); });</script> </head>
PAGE
<?php // Check if the form was submitted if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) { // Do some minor form validation to make sure there is content if (isset ($_POST['title'])) { $title = $_POST['title']; } else { echo 'Please enter a title'; } /* if (isset ($_POST['description'])) { $description = htmlentities(trim(stripcslashes($_POST['description']))); } else { echo 'Please enter the content'; } */ $tags = $_POST['post_tags']; $source = $_POST['source']; $imgurl = $_POST['imgurl']; // Add the content of the form to $post as an array $type = trim($_POST['Type']); $post = array( 'post_title' => $title, 'post_content' => $description, 'post_category' => array($_POST['cat']), // Usable for custom taxonomies too 'post_status' => 'pending', // Choose: publish, preview, future, etc. 'tags_input' => array($tags), 'comment_status' => 'open', 'post_author' => '2', 'source' => $source ); $post_id = wp_insert_post($post); wp_set_post_terms($post_id,$type,'Type',true); add_post_meta($post_id, 'source', $source, false); $upload_dir = wp_upload_dir(); // if using image url if ($imgurl) { $image_data = file_get_contents($imgurl); $filename = basename($imgurl); if(wp_mkdir_p($upload_dir['path'])) $file = $upload_dir['path'] . '/' . $filename; else $file = $upload_dir['basedir'] . '/' . $filename; file_put_contents($file, $image_data); $wp_filetype = wp_check_filetype($filename, null ); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name($filename), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $file, $post_id ); require_once(ABSPATH . 'wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); wp_update_attachment_metadata( $attach_id, $attach_data ); set_post_thumbnail( $post_id, $attach_id ); } else if ($_FILES) { foreach ($_FILES as $file => $array) { $filename = $file; if(wp_mkdir_p($upload_dir['path'])) $file = $upload_dir['path'] . '/' . $filename; else $file = $upload_dir['basedir'] . '/' . $filename; file_put_contents($file, $image_data); $wp_filetype = wp_check_filetype($filename, null ); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name($filename), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $file, $post_id ); require_once(ABSPATH . 'wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); wp_update_attachment_metadata( $attach_id, $attach_data ); set_post_thumbnail( $post_id, $attach_id ); } } wp_redirect( home_url('/listing-submitted/') ); // redirect to home page after submit exit(); } // end IF ?> <?php get_header() ?> <div class="container"> <h1>Share something awesome!</h1> <!--SUBMIT POST--> <form id="new_post" name="new_post" class="form-horizontal" method="post" enctype="multipart/form-data"> <div id="dropupload"><div class="dropzone dropzone-previews" id="myAwesomeDropzone"></div></div> <a href="#" class="lead" id="showurlupload">Upload via URL</a> <div id="urlupload" style="display:none;"> <div class="form-group"> <label for="imgurl" class="col-sm-3 control-label">Image URL</label> <div class="col-sm-9"> <input type="text" id="imgurl" class="form-control required" value="" tabindex="1" size="20" name="imgurl" /> </div> </div> </div> <div class="form-group"> <label for="title" class="col-sm-3 control-label">Title</label> <div class="col-sm-9"> <input type="text" id="title" class="form-control required" value="" tabindex="1" size="20" name="title" /> </div> </div> <div class="form-group"> <label for="cat" class="col-sm-3 control-label">Content Type:</label> <div class="col-sm-9"> <?php wp_dropdown_categories( 'tab_index=10&taxonomy=category&hide_empty=0' ); ?> </div> </div> <!-- Source --> <div class="form-group"> <label for="source" class="col-sm-3 control-label">Source</label> <div class="col-sm-9"> <input type="text" value="" id="source" class="form-control" tabindex="20" name="source" /> </div> </div> <!-- Listing Services --> <div class="form-group"> <label for="post_tags" class="col-sm-3 control-label">Tags (7 max)</label> <div class="col-sm-9"> <div class="tagsinput-primary"><input type="text" value="" tabindex="35" name="post_tags" id="post_tags" class="tagsinput" data-role="tagsinput" /> </div> </div> </div> <input type="hidden" name="post_type" id="post_type" value="domande" /> <input type="hidden" name="action" value="post" /> <input type="submit" value="Submit" class="btn btn-primary btn-lg" tabindex="6" id="submit" name="submit" /> <?php wp_nonce_field( 'new-post' ); ?> </form>
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Set uploaded image or image url as featured image’ is closed to new replies.