URL for Single Image Field not working with wp filter

Support General URL for Single Image Field not working with wp filter

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #42393
    goninski@gmail.com[email protected]
    Participant

    Hello

    I'm trying to create the option for an alternative featured image.

    This basically works fine with this filter as described here
    https://wordpress.stackexchange.com/questions/299844/possible-to-swap-in-a-placeholder-image-globally-for-the-post-thumbnail-url-if-o

    But it only works when I hardcode the URL. As soon I want it to fetch from single image field, I got plenty of errors. Although only fetching the url by it self is working fine.

    See example in this short video (including audio)
    https://www.berrycast.com/conversations/86c539fb-e04e-57b5-91a1-6f38faddc18a

    I spent already many hours without success and hope for a hint, why this is not working.
    Thanks
    François

    Filtercode

    function gof_image_src( $image, $attachment_id, $size, $icon ) {
    
        if( ! is_single() ) { return $image; }
    
        $opt = rwmb_meta( 'gop_base_bg_img_opt' );
    
        if( $opt == 'show-global' ) {
    
            $img_alt = rwmb_meta( 'gop_base_img', ['size' => 'full'] );
            $img_alt_url = $img_alt['url'];
            $image = [$img_alt_url];
            //$image = ['https://dev-mf.local/wp-content/uploads/demo-po-tasha-jolley-jqSysL9rQHc-unsplash.jpg',];
        }
    
        return $image;
    }
    add_filter( 'wp_get_attachment_image_src', 'gof_image_src', 10, 4 );
    
    #42406
    PeterPeter
    Moderator

    Hello,

    Which is the object type that the field "gop_base_img" associated with? The post or the image (attachment post type)?

    You can try to pass the third parameter to the helper function rwmb_meta() to get the field value. For example:

    rwmb_meta( 'gop_base_img', ['size' => 'full'], 1234 );

    where 1234 is the object ID.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.