May be you are not returning correctly from function, Try this,
function insert_attachment($file_handler, $post_id, $setthumb=false) {
// check to make sure its a successful upload
// changes start
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) {
return __return_false();
}
// changes end
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;
}
Related Posts:
- Upload Image in a WordPress page using PHP
- How can we upload custom page files?
- How do I change/modify the_post_thumbnail(); html output?
- How do I make an HTML table from a database table?
- How to change upload directory in wp_handle_upload
- Uploading media with the REST API
- start_lvl Ignored in Custom walker_nav_menu
- How to get the custom header image’s alt text?
- Why do templates contain so many PHP tag pairs?
- Decrease file size upload in Media
- Modify previous and next text from pagination links
- How do I use Shortcodes inside of HTML tags?
- Adding a custom image upload size and making it selected by default?
- add_filter the_content str_replace after shortcode
- Correct regex for wp_embed_register_handler
- Change the filename format of saved featured images
- Why would a GET variable one one page of a site cause a 404 error when a GET variable works on another page of a site?
- What exactly does “Posts page” do in WordPress?
- Where should I install a PHP library into WordPress so that code in a webpage can activate it?
- Image Upload “exceeds the maximum upload size for this site”, but php.ini is correct
- How to register images uploaded via FTP in media library?
- Failed media upload: “The uploaded file was only partially uploaded.”
- Create Image Uploader for Widget
- Using file_exists to check file in Uploads
- WordPress Ajax always returns a 404 error
- Adding a search form inside a div
- WordPress custom taxonomy description for each post?
- Rename image uploads replacing “width” and “height” in filename with “medium”, “large”, “small”
- WordPress Shortcode and Dynamic CSS
- Rename image uploads with width in filename
- Modify image while uploading
- Debugging upload problem: What part of WP does actual image-resizing?
- Calling a method from functions.php on a click of a button
- Load custom field value into div with AJAX
- How to resize image on client-side before upload?
- Blank on static home page?
- Correct way to make static pages editable in wordpress
- Specified file failed upload test. wp_upload_handle
- Conditional statement for parent, child and grandchild pages
- How to upload imagick resource to media in wordpress
- How do I edit the php/html for a particular post?
- Accessing a random image via ajax
- Make WordPress upload directory outside wordpress root with custom url
- Is it unsafe to put php in the /wp-content/uploads directory?
- Nice scroll to wordpress
- How to use SRCSET with get_the_post_thumbnail()?
- Multiple pages on one with different HTML
- How to use a frontend URL with a Plugin
- Upload images and attachments from frontend form
- How do I make wordpress comment fields required?
- Two posts in same div – WP loop
- Image upload via FTP to wordpress media library
- WordPress media upload “HTTP error”
- Adding featured image via PHP
- How to enlarge the media file upload size in wordpress admin
- Is it ok to replace php code with html?
- How to display blog images using wordpress in existing project
- Add custom HTML to posts page
- How to add a post or page tag to the list of classes appearing in the body tag’s class attribute?
- WordPress Wysiwyg Content not being displayed
- Restrict WordPress File Type Uploads by User Type
- File upload, uploads only file name
- Javascript code inside “” in core WordPress files .php
- HTML table from shortcode with multiple parameters
- Minify HTML, CSS, JS with PHPWee?
- Modify Maximum upload file size text
- Adding a querystring to an image URL when clicking ‘insert into post’?
- How to display a value from a radio button in the options menu in wordpress
- Header has extra HTML block at top before my code
- Use WordPress’s Media Uploader/ Manager in non WordPress php application
- Upload a json file in php [closed]
- To echo or not to echo?
- Blank space at beginning of tag?
- How do I add HTML to a PHP function [closed]
- wp_handle_upload – The uploaded file could not be moved to wp-content/uploads/2012/09
- How to display recent posts on home page with custom HTML
- is it possible to force wordpress to always save thumbnails as ‘jpg’ not ‘png’
- Memory errors with media upload, WordPress can’t use more than 96M (while there’s 512 available!)
- WordPress automatically adds tags when pasting code, how to stop it?
- Add HTML Attributes To Anchor Tags In `wp_list_categories()` Function
- Placing raw HTML inside a WordPress shortcode
- How to only show page id as body class?
- Validate Uploaded Image using WordPress’ Built-in Functions?
- Get most recent media upload
- Slide in Panel WordPress Post
- Shortcode content output but not in correct place
- Convert all images to PNG on file upload
- Correct syntax to html php conversion
- How to restrict wp generate attachment metadata() to certain intermediate image sizes
- An unwanted inline style is added to my body tag
- How to properly insert a link to a template in WordPress?
- Show post in slider
- Add itemprop Schema.org Markup to li Elements in wp_nav_menu
- WordPress (3.9.1) MultiSite Permissions. Is chown the answer?
- Removing unneccessary p-tags (Not every p)
- How can I hide Home in my menu in a specific page?
- WordPress automatically adding html tag around PHP code
- Missing sidebar parameter “fix” – before_content
- Randomise upload filenames (or another solution to hide the original image URL from theft?)
- How Display Posts on category