Crocoblock / suggestions

The suggestions for CrocoBlock project
194 stars 78 forks source link

Automatically set 'Featured Image' from Gallery #3985

Open kugcmadushan opened 3 years ago

kugcmadushan commented 3 years ago

JetEngine indeed a great plugin for directory and listing websites. The feature missing there is, we have to set featured image and gallery images separately. Most themes dedicated for listing websites allows users to set images in one go and featured image would be automatically set. I am aware this can be done with coding. But, better to implement this in benefit of users who are not capable of coding.

I already made the concern commenting with one of your tutorials and was asked to raise the concern in this platform.

nreljed commented 3 years ago

The following code makes the first photo in the gallery the featured image of the post. I already use it actively


add_action( 'save_post', function ( $post_id ) {

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    $gallery_key = 'property_photos';

    $gallery = get_post_meta( $post_id, $gallery_key, true );

    if ( empty( $gallery ) ) {
        return;
    }

    $gallery = explode( ',', $gallery );

    $image_id = $gallery[0];
    set_post_thumbnail( $post_id, $image_id );

}, 99 );

More than that, this question has been posted and seen and needs a more than a simple featured image or drag and drop uploader it's about creating a real super smart uploader for forms, which takes over on basic wordpress upload way and understands what happens when the user has double uploaded or abandoned the upload or deleted an upload by accident .. with resize parameters and extensive control.

no plugin has thought of that (except sites or super popular themes) it's an incredible opportunity to seize this empty place this type of uploader will be beneficial for all types of projects

3154

3411

3885

the ultimate solution may be found in the open source side of the force.. I hope that Santa Croco will listen to our wishes 🙏

kamstudios commented 2 years ago

how to implement the code? I've done it from functions.php and it doesn't seem to work, it helps!

thtasca commented 2 years ago

This code was working perfectly on my form, but I upgraded from the latest version (2.1.6) and now it's not adding the image as the post thumbnail anymore. Does it have any solution?

girafffee commented 2 years ago

@thiagotask This action works for me. Can you export your form and attach it here?

thtasca commented 1 year ago

Hi @girafffee, I found where the "problem" is but I don't know how to solve it, my registration form is creating the post but in pending status, before the update to the last version it was saving the image as the post's thumb, but now it it only saves it when I enter the other form that I use to review and then change its status to published. Is there any way to adjust the code to add the thumb in other statuses?

It looks like the code is for adding only when the post is published, but somehow it was adding if it was in other statuses.

Edit: I was doing a rollback in the plugin, and in version 2.1.4 the code worked again and adding the thumb in my form before reviewing and changing the status to published in the post.

form.zip

girafffee commented 1 year ago

@thiagotask The problem with the save_post hook is that it can be called both when submitting the form and changing any property of the post on the backend. And in both cases, your code will run, so I suggest you write the code below

const POST_META_PROPERTY = 'property_photos';

add_filter(
    'jet-form-builder/post-modifier/object-properties',
    function ( $collection ) {
        $meta = new class extends \Jet_Form_Builder\Actions\Methods\Post_Modification\Post_Meta_Property {

            public function do_before( string $key, $value, \Jet_Form_Builder\Actions\Methods\Abstract_Modifier $modifier ) {
                parent::do_before( $key, $value, $modifier );

                if ( POST_META_PROPERTY !== $key || ! is_string( $value ) ) {
                    return;
                }
                $image_id = explode( ',',  $value )[0] ?? '';

                if ( ! is_numeric( $image_id ) ) {
                    return;
                }
                $thumbnail = $modifier->get( '_thumbnail_id' );
                $thumbnail->set_value( '', $image_id, $modifier );
            }
        };

        /** @var \Jet_Form_Builder\Actions\Methods\Object_Properties_Collection $collection */
        $collection->replace( $meta );

        return $collection;
    }
);

This snippet should work fine since version 2.1.6

galaxyp100 commented 1 year ago

Hi, is it possible to avoid the Featured Image being duplicated in the Gallery? At the moment if you open the featured image in a lightbox and press the next arrow for the next image it is the Featured Image repeated again? Thanks :)

tunacan34 commented 1 month ago

For custom post type working code. My custom post type name is " saat " change it with your own post name jetformbuilder settings : reload submit - not ajax gallery field value : Media ID

`add_action( 'save_post', function ( $post_id, $post, $update ) {

// Otomatik kaydediliyorsa çık
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return;
}

// Revizyonlarda çalışmasını engelle
if ( wp_is_post_revision( $post_id ) ) {
    return;
}

// Sadece 'saat' post type için çalışmasını sağla
if ( get_post_type( $post_id ) !== 'saat' ) {
    return;
}

// Post kaydedildikten sonra bir süre bekleyip sonra çalıştır
add_action('shutdown', function() use ($post_id) {
    $gallery_key = 'galeri';  // Meta alanı anahtarı

    $gallery = get_post_meta( $post_id, $gallery_key, true );

    if ( empty( $gallery ) ) {
        return;
    }

    // Eğer $gallery bir dize ise, explode fonksiyonunu çalıştır
    if ( is_string( $gallery ) ) {
        $gallery = explode( ',', $gallery );
    }

    // Eğer $gallery zaten bir dizi ise, ilk öğeyi al
    $image_id = is_array( $gallery ) ? $gallery[0] : $gallery;

    // Öne çıkarılan görsel olarak ayarla
    set_post_thumbnail( $post_id, $image_id );
});

}, 99, 3 ); `

SangDon-KIm commented 1 week ago

Hello, I am a college student who became interested in WordPress and came to Crocoblock.

When I was making a personal site, I was first surprised to see that Jet Engine does not support this feature, and then I was surprised twice to see that this problem has been around since 2021 and still needs to be supplemented with code.

I am not a developer and I try to avoid situations where problems can occur as much as possible, so I think supplementing with code is the last resort.

Will the solution Crocoblock presents for this problem exist in September 2024?