Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamedmoody/915be9ba7bfe38ac6a460ee46a95d841 to your computer and use it in GitHub Desktop.
Save hamedmoody/915be9ba7bfe38ac6a460ee46a95d841 to your computer and use it in GitHub Desktop.
Use media upload in wordpress frontend.
<form method="post" action="#" enctype="multipart/form-data" >
<input type="file" name="featured_image">
</form>
<?php
function insert_attachment($file_handler,$post_id,$setthumb='false') {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment