0

This is the code and i want to stop the post publishing if the file is not an image Is this possible ?

case "window-image":
        $format = 'image';
        $title = esc_html($_POST['title_image']);
        $tags = $_POST['tags_image'];
        $comments = isset($_POST['comments_image']) ? 1 : 0;
        $anonymous = isset($_POST['anonymous_image']) ? 1 : 0;
        $comment_status = $comments == 1 ? 'open' : 'closed';
        $user_id = $id;

        if(($_FILES['image_imagepost']['error'] != 0 && $image == '') || $title == '') {
            wp_redirect(home_url('/') . '?posterror=1');
            exit; 
        }       

        $post = array(
          'comment_status' => $comment_status,
          'ping_status'    => 'open',
          'post_author'    => $user_id,
          'post_content'   => '',
          'post_status'    => $status,
          'post_title'     => $title,
          'post_type'      => 'post',
         );  
        $post_id = wp_insert_post($post);

#Create attachment
    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');
    }
        $upload = wp_upload_bits( $_FILES['file']['name'], null, file_get_contents( $_FILES['file']['tmp_name'] ) );
        $wp_filetype = wp_check_filetype( basename( $upload['file'] ), null );
        $wp_upload_dir = wp_upload_dir();
        $attachment = array(
            'guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $upload['file'] ),
            'post_mime_type' => $wp_filetype['type'],
            'post_title' => preg_replace('/\.[^.]+$/', '', basename( $upload['file'] )),    
            'post_content' => '',
            'post_author' => $user_id,
            'post_status' => 'inherit'
        );
        $attach_id = wp_insert_attachment( $attachment, $upload['file'], $post_id );
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] );
        wp_update_attachment_metadata( $attach_id, $attach_data );
#Create attachment

#Use attachment
        update_post_meta( $post_id, '_thumbnail_id', $attach_id );
#Use attachment


        $nsfw = isset($_POST['nsfw_image']) ? 1 : 2;
        add_post_meta($post_id, '_standard_nsfw', $nsfw);
        add_post_meta($post_id, '_anonymous', $anonymous);

        break;
4
  • Is this using the save_post hook by chance?
    – Howdy_McGee
    Commented Mar 11, 2016 at 22:42
  • No, i don't think so .. I think i need something like this : if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; wp_trash_post( $post_id ); } but i think the code is wrong because he keeps giving me same message for every file
    – I.Alex
    Commented Mar 11, 2016 at 22:47
  • What hook are you using for the above code or, how are you implementing this code?
    – Howdy_McGee
    Commented Mar 11, 2016 at 22:48
  • i don't know know for sure what you mean i'm beginner to wordpress but look here : textuploader.com/5v817
    – I.Alex
    Commented Mar 11, 2016 at 22:55

1 Answer 1

0

Seems like I managed to work . I put this code into upload code

$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
    wp_trash_post( $post_id  );
}

And this allow users to post just images and if he upload another file type get a message and the post is automaticaly going to trash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.