Skip Navigation

[Resolved] Copying same featured image for translation

This thread is resolved. Here is a description of the problem and solution.

Problem:

I keep having following error showing in the debugging info:

Notice: Undefined variable: src_thumbnail_id in /home3/domain/domain.co/wp-content/toolset-customizations/duplicate-featured-img.php on line 39

Solution:

It is a bug of user's custom PHP codes, you can define a default value for variable: src_thumbnail_id, for example:

https://toolset.com/forums/topic/copying-same-featured-image-for-translation/#post-1252505

Relevant Documentation:

This support ticket is created 4 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by Nashaat 4 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1252353

Hi i am using following snippet for copying the same image url to all translations when create for a specific language from the frontend.


class WpmlFeaturedImageDuplicationFix{
 
    function __construct(){
        // check $sitepress object
        global $sitepress;
        if ( !is_object($sitepress) ) {
            return;
        }
 
        // Add hooks
        add_action( 'icl_make_duplicate', array($this, 'make_duplicate'), 99, 4 );
        add_action('save_post', array($this, 'save_post'), 99, 3);
    }
 
    function make_duplicate($master_post_id, $lang, $post_array, $id){
        $this->copyImageToDuplicates($master_post_id);
    }
 
    function copyImageToDuplicates($post_id){
        global $sitepress;
        $trid = $sitepress->get_element_trid($post_id);
        $translations = $sitepress->get_element_translations($trid);
 
        // get thumbnail and id of src post
        foreach( $translations as $translation ) {
            if ($translation->original=='1') {
                $src_id = $translation->element_id;
                $src_thumbnail_id = get_post_meta($translation->element_id, '_thumbnail_id', true);
            }
        }
 		
        // ensure a src post is found
        if (!$src_thumbnail_id) {
            return;
        } 
 
        // get all duplicates of this post
        $duplicates = $sitepress->get_duplicates($src_id);
 
        // set thumbnail on all duplicates
        foreach( $duplicates as $countryCode => $duplicateId ) {
            update_post_meta ( $duplicateId, '_thumbnail_id', $src_thumbnail_id );
        }
    }
 
    function save_post( $post_id, $post, $update ){
        $this->copyImageToDuplicates($post_id);
    }
}
 
$wpmlFeaturedImageDuplicationFix = new WpmlFeaturedImageDuplicationFix();

all working fine as expected. but i keep having following error showing in the debugging info:

Notice: Undefined variable: src_thumbnail_id in /home3/domain/domain.co/wp-content/toolset-customizations/duplicate-featured-img.php on line 39

I am not sure how to get rid of this error.

#1252505

Hello,

It is a bug of your custom PHP codes, you can define a default value for variable: src_thumbnail_id, for example, add a line after this line

// get thumbnail and id of src post

like this:

// get thumbnail and id of src post
$src_thumbnail_id = 0;
#1253247

My issue is resolved now. Thank you Luo!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.