• Hi, i’ve a problem with the featured image/thumbnail

    i’ve added this code to the functions.php:

    add_action("adverts_post_type", "customize_adverts_post_type");
    function customize_adverts_post_type( $args ) {
       $args["supports"][] = "thumbnail";
       return $args;
    }

    It All works, i can set the featured image and all works.

    Now, My question is: how do i get the featured image URL?

    something like “get the post thumbnail” which contains the advert post ID too.
    I need to use it in another plugin (onesignal) for push notifications on my iOS app.

    at the moment i’ve this

    $image = adverts_get_main_image( get_the_ID() ); 
    $fields['ios_attachments'] = array ('img1' => esc_attr($image),);

    this works but it’s wrong.. because it doesn’t show the correct image for every post i publish, it always shows the latest image uploaded in the gallery

    CASE 1: when i publish the adverts in real time it works, in the push notification i see the right image (because it’s also the latest in the gallery) but..

    CASE 2: when i schedule the adverts, for example 5 posts, for every publication it always shows the latest image uploaded in the gallery. So Post 1, post 2 , post 3, and post 4 will always show the Post 5 image

    I think this happens because i use a wrong code to recall the advert-post image, how can i do it properly?

    • This topic was modified 4 years, 11 months ago by fabiano1987.
    • This topic was modified 4 years, 11 months ago by fabiano1987.
    • This topic was modified 4 years, 11 months ago by fabiano1987.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    You can get an URL to an image marked as featured using the get_the_post_thumbnail_url() function.

    
    echo get_the_post_thumbnail_url( $post_id, "full" );
    

    where $post_id is an actual ID of an advert.

    Thread Starter fabiano1987

    (@fabiano1987)

    it works but not always..

    i mean, i’ve created a onesignal plugin, so i’ve added this code in a php file which is inside another directory , (different from wpadverts plugin folder )

    What happens:
    when i update an already-published advert post, it works fine and i receive the push notification with the image

    but when i schedule an advert post , i receive the push notification without the image.

    It’s like it cannot get the image on scheduled posts, or cannot read the post-id, i don’t know

    maybe i also need to specify $post_id = something?

    This is the whole OneSignal Plugin code, (the php page)

    add_filter('onesignal_send_notification', 'onesignal_send_notification_filter', 11, 4);
    
    function onesignal_send_notification_filter($fields, $new_status, $old_status, $advert) {
    
    	$id = $post->ID;
    	$immagine = get_the_post_thumbnail_url( $post_id, "full" );
    
    	$app_data = [
    		'id_article' => $id,
    		'imageURL' => ''
    	];
    
    	$fields['ios_sound'] = 'unsure.aiff';
    	$fields['ios_attachments'] = array ('img1' => $immagine,);
    	$fields['content_available'] = 1;
      	$fields['data'] = $app_data;
    	$fields['ios_badgeType'] = 'Increase';
    	$fields['ios_badgeCount'] = 1;
    	$fields['mutable_content'] = 1;
    
    	return $fields;
    	
    }
    

    Am i missing something? Because it’s only on scheduled posts which it doesn’t work

    Plugin Author Greg Winiarski

    (@gwin)

    In your code you do not have $post_id variable defined (neither $post variable which you are using) you need to replace it with the variable you have, maybe $advert->ID i cannot tell for sure as i am not familiar with onesignal_send_notification filter so i do not know that does the $post variable contains.

    Thread Starter fabiano1987

    (@fabiano1987)

    Yes in fact the $post_id is not defined and that’s why i can’t see the image

    (i don’t know why it works for advert posts update, but not for scheduled posts.. it should not work in both cases)

    By the way, You don’t need to be an OneSignal expert because the plugin was created from scratch, it’s not something existing to understand before to modify

    so my question is: How would you do, to indicate the Advert Post ID inside a php file which is located outside the WP-Adverts directory?

    if you write me the code i will try to re-use it inside that plugin and this could be very helpfull for everyone

    Plugin Author Greg Winiarski

    (@gwin)

    The Ad ID needs to be passed to the filter somehow if it isn’t then it is not possible to get the Advert ID regardless of the PHP file being inside or outside the wpadverts directory.

    I might not need to be OneSignal expert but i need to know how the onesignal_send_notification filter is applied otherwise i will not know what the function params are.

    Either way i checked the code and the 4th param is a WP_Post then you should be able to get the Advert ID with $advert->ID call.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to get the “advert” Featured Image URL?’ is closed to new replies.