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
- Adding a custom image upload size and making it selected by default?
- add_filter the_content str_replace after shortcode
- Image Upload “exceeds the maximum upload size for this site”, but php.ini is correct
- Failed media upload: “The uploaded file was only partially uploaded.”
- Create Image Uploader for Widget
- WordPress Ajax always returns a 404 error
- How to resize image on client-side before upload?
- How do I make wordpress comment fields required?
- WordPress media upload “HTTP error”
- WordPress Wysiwyg Content not being displayed
- Restrict WordPress File Type Uploads by User Type
- Javascript code inside “” in core WordPress files .php
- Modify Maximum upload file size text
- How do I add HTML to a PHP function [closed]
- How to display recent posts on home page with custom HTML
- Removing unneccessary p-tags (Not every p)
- Why is $_REQUEST an empty array in admin-ajax.php?
- Multiple options not staying selected after saving
- Why the JavaScript code is ignored from wp editor?
- How to remove ?wp-mce-4103-20162941
- Why Won’t My Inputs in a form with a Get method work together?
- parts of url disappear when using # inside href=”” [closed]
- How to get the value of input hidden html from text editor to custom page template?
- Upload file could not be moved to wp-content/uploads
- Dot is not added at the end of href
- Add .htm extention to a custom taxonomy
- Auto 301 to full post permalink? (using : /archive/%post_id% to %post_name% )
- How do I add a custom css to all posts without affecting homepage css? [closed]
- How do I Import / Upload Files with jQuery AJAX?
- WordPress function when file is uploaded, deleted or edited
- Noscript not working as it should in wordpress site
- How to convert select to HTML with PHP tags inside of it [closed]
- Wrapping a Sidebar within
- HTML escaping data with ajax requests
- php within html within php
- HTML table attributes ignored
- php esc_html_e with an html link inside not working
- Using ob_start and ob_get_clean with wordpress shortcode
- Upload multiple files in randomly generated folder using wp_upload_bits
- How to edit HTML of my website on WordPress? [closed]
- How to rename woocommerce-billing-fields
- Can’t upload CSV file to plugin directory using custom upload form in admin panel
- How to add post meta in while loop?
- Adding function to child theme’s function.php
- Calling function in loop causes repeat data
- How do I increase the upload size only when editing special pages?
- Get URL of Featured Image After wp_insert_post()
- Link to page with posts of specific type
- Function Display article [closed]
- How to get the second slug of a page url?
- Some problems in custom widget
- Displaying images outside the content
- custom add note meta box with add button in wordpress
- Checking page before applying image restrictions while uploading
- Hook on file upload
- WordPress User Meta value variable into Google Gauge Chart
- List sub categories and corresponding posts of a parent category
- population category dropdown and sub category onselect
- PHP function for horizontal Woocommerce thumbnails and badges
- Using Google Analytics in an enqueued JS file
- dynamic php menu with hidden sub-categories only showing when the category name is clicked
- couldnt show short code atts in category
- separate list of programs in html/javascript coming from php
- Drop down question
- How do I disable cache for a certain page?
- AJAX and custom pages
- how to add functions to my function.php using a child theme?
- Change upload URL by mime type
- How can I create a shortcode from an html and php code written together
- WooCommerce multiple custom fields code
- How to override html codes in wordpress?
- Target a specific container to style on WordPress storefront theme
- Create html tree sitemap
- wordpress form action page not found
- Crop an image after upload on custom account page
- Pass form attributes to another php template in wordpress
- Restrict File Type Uploads by User on Wordress via functions.php
- Custom HTML form using PHP – help with ajax/username validation
- How to create a php page to collect information from a html page
- Can’t make product images clickable
- I need help implement a Javascript code into the PHP file
- Renaming “Expand Details” within “Add Media”
- WooCommerce – Print Processing orders [closed]
- Category – “No content found”
- Save the outputted image into the Media Library, with a different filename and extension
- Add extra markup to WP menus
- WordPress – Image href for home menu nav not working
- Name Input from widget displays Sidebar name instead of saved data
- Run php function after html click after page refresh
- Get all the contents of taxonomy and sort by term
- Gallery requires a featured image but does not show it in posts/pages
- Can’t upload files
- How do I modify the with wp_nav_menu()
- Edit Image/Image Details – Replace button missing
- Can you not edit the HTML and PHP of your WordPress without paying?
- Parse error: syntax error, unexpected ‘}’ in
- How to automatically convert images to WebP on WordPress?
- WordPress directories not writable after PHP version upgrade
- How to create different templates for woocommerce single-product.php?