Custom Fields
Support MB Frontend Submission How to Sideload Featured Image on Frontend Submission?

  • This topic has 2 replies, 2 voices, and was last updated 2 years ago by thomastran40.
  • Creator
  • #9477

    I have a custom post type of “Websites” and a Front-end Submission form to add a website. I use the following that works via the WP Admin Panel. How can i trigger the same type of functionality on my website frontend submission form?

    function download_featured_image( $post_id ) {
    if ( has_post_thumbnail() ) {
    // example image
    $image = "";
    // magic sideload image returns an HTML image, not an ID
    $media = media_sideload_image($image, $post_id);
    // therefore we must find it so we can set it as featured ID
    if(!empty($media) && !is_wp_error($media)){
        $args = array(
            'post_type' => 'attachment',
            'posts_per_page' => -1,
            'post_status' => 'any',
            'post_parent' => $post_id
        // reference new image to set as featured
        $attachments = get_posts($args);
        if(isset($attachments) && is_array($attachments)){
            foreach($attachments as $attachment){
                // grab source of full size images (so no 300x150 nonsense in path)
                $image = wp_get_attachment_image_src($attachment->ID, 'full');
                // determine if in the $media image we created, the string of the URL exists
                if(strpos($media, $image[0]) !== false){
                    // if so, we found our image. set it as thumbnail
                    set_post_thumbnail($post_id, $attachment->ID);
                    // only want one image
    add_action( 'save_post', 'download_featured_image');  
Viewing 1 replies (of 1 total)
  • Author
  • #9482
    Anh Tran


    The MB Frontend Submission uses WordPress wp_insert_post() and wp_update_post() to create/update a post. Both functions fire the save_post hook. So, your code still works in the frontend. Please try and let me know how it goes.

Viewing 1 replies (of 1 total)
  • The topic ‘How to Sideload Featured Image on Frontend Submission?’ is closed to new replies.
grid gear info copy caret-up caret-down trash