post thumb nail image from upload directory
-
Hi,
I am trapped. I want to upload an image from front end. The image croped and uploaded to upload folder, Now I want to set that image as the featured image. I spend hours to make it fine. But no use. Please help me<?php $success = true; function eeme_save_data_init() { if ( isset( $_POST[ 'eeme_form_submit' ] ) ) { foreach ($_POST['eeme_form_submit'] as $name) { # code... $variales = $name['name']; $values = $name['value']; $array[$variales] = $values; } //readigng the form values for users $image = $array["image"]; $username = esc_html( $array[ 'username' ] ); $vorname = esc_html( $array[ 'vorname' ] ); $nachename = esc_html( $array[ 'nachname' ] ); $alter = (int) esc_html( $array[ 'alter' ] ); $telefone = esc_html( $array[ 'telefon' ] ); $email = esc_html( $array[ 'email' ] ); $accept = $array[ 'accept' ]; // reading the form values for the post $post_content = esc_textarea( $array[ 'limitedtextarea' ] ); $title = wp_trim_words( $post_content, 3 ); // form validation by using PHP if ( $accept != 'on' ) { die(_e( "Please accept the terms and conditions" )); } //Validating email if ( !is_email( $email ) ) { die(_e( "This is not a valid email" )); } //Valiadting age if ( !is_numeric( $alter ) ) { die(_e( "This is not a valid Number, enter a valid number" )); } $user_data_array = array( //hold user data 'username' => $username, 'vorname' => $vorname, 'nachename' => $nachename, 'alter' => $alter, 'telefone' => $telefone, 'email' => $email, 'name' => $username ); $post_data_array = array( 'title' => $title, 'description' => $post_content, 'image' => $image ); //Check wheather user exist or not $have_email = (bool)email_exists( $email ); $have_user = (bool)username_exists( $username ); if ( $have_email && $have_user ) { $user_id = get_user_by( 'login', $username ); eeme_post_upload( $post_data_array, $user_id->ID ); $return_array["status"] = TRUE; die(_e(" <p>Vielen Dank fur deinen Beitrag! <br> Jeder Beitrag wird gepruft, bevor er freigeschaltet wird. <br>Wir wunschen dir viel Gluck!</p>")); } else if(!$have_email && !$have_user){ $user_id = eeme_user_insert( $user_data_array ); eeme_post_upload( $post_data_array, $user_id ); $return_array["status"] = TRUE; die(_e(" <p>Vielen Dank fur deinen Beitrag! <br> Jeder Beitrag wird gepruft, bevor er freigeschaltet wird. <br>Wir wunschen dir viel Gluck!</p>")); }else { die(_e("Can not use same username/email with different email/username")); } } else{ die(_e("Some unknown error occures, Please try again")); } } /* * Function eeme_user_insert() used to insert the details to the user table and user meta table * from the form * $user_data_array is an array that store all the value read from the form. * Return the id of user that created at the time */ function eeme_user_insert( $user_data_array ) { $userdata = array( 'ID' => '', 'user_pass' => wp_generate_password( 12,true, true ), 'nickname' => $user_data_array[ 'nachename' ], 'user_login' => $user_data_array[ 'username' ], 'user_email' => $user_data_array[ 'email' ], 'display_name' => $user_data_array[ 'vorname' ], 'first_name' => $user_data_array[ 'vorname' ], 'role' => 'post_builder' ); $id = wp_insert_user( $userdata ); add_user_meta( $id, 'alter', $user_data_array[ 'alter' ] ); add_user_meta( $id, 'telefone', $user_data_array[ 'telefone' ] ); return $id; } /* * Function eeme_image_upload is used to upload the image * Return a boolean value * true if success else false */ function eeme_image_upload($post_id, $image ) { if ( !function_exists( 'wp_generate_attachment_metadata' ) ) { require_once(ABSPATH . "wp-admin" . '/includes/image.php'); require_once(ABSPATH . "wp-admin" . '/includes/file.php'); require_once(ABSPATH . "wp-admin" . '/includes/media.php'); } if($image){ //uploading the image $time = date('h:i:s'); $dir = wp_upload_dir(); $path = $dir['path']; $url = $dir['url']; $name_file = $post_id.$time.'.png'; $file_path = $path.'/'.$name_file; $file_url = $url.'/'.$name_file; $filename = fopen( $file_path,"w+"); chmod ( $file_path, 0777 ); $data = $image; list($type, $data) = explode(';', $data); list(, $data) = explode(',', $data); $data = base64_decode($data); file_put_contents($file_path, $data); $attachment = array( 'guid' => $file_path, 'post_mime_type' => '', 'post_content' => '', 'post_status' => 'publish' ); $attach_id = wp_insert_attachment( $attachment, $name_file, $post_id ); //set_post_thumbnail( $post_id, $attach_id ); $thumb_attach_attr = wp_get_attachment_image_src( $attach_id ); $postmeta_id = add_post_meta($post_id , '_thumbnail', $thumb_attach_attr[0]); // die("attachment".var_dump($attach_id)); // require_once(ABSPATH . 'wp-admin/includes/image.php'); // $metadata = wp_generate_attachment_metadata( $attach_id, $file_url); // wp_update_attachment_metadata( $attach_id, $metadata ); // // Finally! set our post thumbnail // update_post_meta( $post_id, '_thumbnail_id', $attach_id ); } // else{ // $attach_id = NULL; // return $attach_id; // } } /* * Function eeme_post_upload is used to upload post from front end form * $post_data_array contain title and content of the post * $user_id is the id of user who create the post */ function eeme_post_upload( $post_data_array, $user_id ) { //and if you want to set that image as Post then use: $post_information = array( 'post_title' => $post_data_array[ 'title' ], 'post_content' => $post_data_array[ 'description' ], 'post_type' => 'post', 'post_status' => 'publish', 'post_category' => array( get_cat_id( 'Got the power' ) ), 'tags_input' => '#Got the power', 'post_author' => $user_id ); $post_id = wp_insert_post( $post_information ); $image_data = $post_data_array[ 'image' ]; $image_id = eeme_image_upload($post_id, $image_data ); //die("image id".$image_id); //update_post_meta( $post_id, '_thumbnail_id', $image_id ); //die("id".set_post_thumbnail( $post_id, $image_id )); // $file = $post_data_array['image']; // $attachment = array( // 'post_mime_type' => '', // 'post_title' => '', // '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 ); // $res1= wp_update_attachment_metadata( $attach_id, $attach_data ); // $res2= set_post_thumbnail( $post_id, $attach_id ); } /* * Ajax in wordpress */ function eeme_add_scripts(){ wp_enqueue_script('eeme_script',get_template_directory_uri() . '/inc/js/eeme_ajax.js',array('jquery'), '1.0.0', true ); wp_localize_script( 'eeme_script','eeme_save', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); } add_action('wp_enqueue_scripts', 'eeme_add_scripts'); add_action('wp_ajax_eeme_save_data', 'eeme_save_data_init'); add_action('wp_ajax_nopriv_eeme_save_data', 'eeme_save_data_init');
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘post thumb nail image from upload directory’ is closed to new replies.