• Resolved zzkamikazezz

    (@zzkamikazezz)


    Hi Greg

    Sorry for starting a new thread. I can’t find the old one. I have looked and looked.

    I was after a code snippet that would take the first photo a user added to their advert and make it the featured image for their post. You mentioned you may be able to help with a code snippet for this.

    This would allow me to use essential grids to pull in the latest advert postings with an image for each ad in the grid and then display that grid on numerous pages on my site. That would be super useful!! At the minute classified are kind of sitting on a page that won’t get much attention and this would help steer people there having the grid on a few pages.

    Many thanks

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi, i am travelling right now and i wasn’t able to write the snippet yet, but it should be available next week.

    Plugin Author Greg Winiarski

    (@gwin)

    In the link below you will find a code snippet which will force a featured image when posting an Ad (using [adverts_add]) or when editing it (from [adverts_manage]).

    https://github.com/simpliko/wpadverts-snippets/blob/master/force-featured-image/force-featured-image.php

    This code works fine, but the update(0.2.0 – X-Team, Jonathan Bardo) I could not make it work.

    Plugin Author Greg Winiarski

    (@gwin)

    I am not sure what do you mean by

    update(0.2.0 – X-Team, Jonathan Bardo)

    ??

    Thread Starter zzkamikazezz

    (@zzkamikazezz)

    Greg, I didn’t get email notification of your reply, so I’ve only recently seen this.

    WOW! Thank you! This is perfect! Just what I was after.

    Take a look at the essential grids grid I have created in my side bar here. https://www.carshow.life (site is still being build by the way)

    Thank you again, it obviously took a fair bit of your time up.

    I used this snippet like a plugin.
    An update was released (0.2.0).
    The author informed is X-Team, Jonathan Bardo
    When i updated the script it not work.
    Am I doing something wrong?

    force-featured-image-wp-advert

    Plugin Author Greg Winiarski

    (@gwin)

    It seems the add-on name is conflicting with the Force Featured Image https://www.ads-software.com/plugins/force-featured-image/ already available in the repository, you will need to install it again, but change the folder name in which the snippet is to my-force-featured-image or install it in default folder but do not update.

    I will try. Thanks Greg.

    Thread Starter zzkamikazezz

    (@zzkamikazezz)

    I know it is a basic question, but how do you use this like a plugin?

    I tried uploading the pho file into a folder I created in my plugins folder, but it didnt appear for me to see or activate in the admin dashboard under plugins…

    Plugin Author Greg Winiarski

    (@gwin)

    Please see the “Using Snippets” here https://github.com/simpliko/wpadverts-snippets it explains how to do that.

    Works fine Greg, thanks.

    zzkamikazezz, I did a quick solution that is really much easy.

    I named the folder and plugin with “my-force-featured-image” and my-force-featured-image.php respectively, to avoid conflict with existent plugin force featured image, like Greg said.

    Put the snippet into the file and create a header plugin, like this:

    /*
     * Plugin Name: My Force Featured Image
     * Plugin URI: https://wpadverts.com/
     * Description: This code snippet/plugin forces user to select a featured image or selects it for the user automatically.
     * Author: Greg Winiarski
     */

    For some reason the code snippet is not working for me. I tried it as a plugin and using Code Snippets – neither seems to work…

    
    add_filter( "adverts_action_preview", "force_featured_image_add" );
    add_filter( "adverts_template_load", "force_featured_image_edit" );
    /**
     * Forces featured image when adding new Ad using [adverts_add].
     * 
     * @uses force_featured_image()
     * 
     * @param string $content Page content
     * @return string
     */
    function force_featured_image_add( $content ) {
        
        $post_id = adverts_request("_post_id", null);
        force_featured_image( $post_id );
        
        return $content;
    }
    /**
     * Forces featured image when editing and Ad in [adverts_manage].
     * 
     * @uses force_featured_image()
     * 
     * @param string $path Path to template file
     * @return string
     */
    function force_featured_image_edit( $path ) {
        
        if( basename( $path ) == 'manage-edit.php' && isset( $_POST['_post_id'] ) && is_numeric( $_POST['_post_id' ] ) ) {
            force_featured_image( $_POST['_post_id'] );
        }
        return $path;
    }
    /**
     * Sets featured image for $post_id
     * 
     * @param int $post_id  ID of a post for which we wish to force featured image
     * @return int          1 if success less or equal to 0 on failure
     */
    function force_featured_image( $post_id ) {
        if( $post_id < 1 ) {
            // No images uploaded
            return -1;
        } else if( $post_id > 0 && get_post_thumbnail_id( $post_id ) ) {
            // Has main image selected
            return -2;
        } 
        
        $children = get_children( array( 'post_parent' => $post_id ) );
        
        foreach( $children as $child ) {
            update_post_meta( $post_id, '_thumbnail_id', $child->ID );
            return 1;
        }
        
        return 0;
    }
    
    Plugin Author Greg Winiarski

    (@gwin)

    This code is executed on preview, you will need to add some debugging code in function force_featured_image() to see if it is being executed and what happens on execution.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Featured image for each advert’ is closed to new replies.