Support » Plugin: Post Type Switcher » Simple hack to add WPML support

  • Hi, if someone’s interested, that’s how you can add WPML support to Post Type Switcher V1.7.0. That’s the version I used for it, probably other versions will require some adaptation..

    So..

    1. Open the main plugin PHP file (post-type-switcher.php).
    2. Replace these lines:

    // Set the new post type.
    set_post_type( $post_id, $post_type_object->name );

    With this ones:

    global $wpdb, $sitepress;
    
    include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );
    
    $post_trid = wpml_get_content_trid( 'post_' . $post_type, $post_id );
    $translations = $sitepress->get_element_translations( $post_trid );
    
    if( is_array( $translations ) && !empty( $translations ) ){
    
    	foreach( $translations as $translation ){
    
    		$elem_id = $translation->element_id;
    
    		// Set the new post type.
    		set_post_type( $elem_id, $post_type_object->name );
    
    		if( function_exists('icl_object_id') ){
    
    			$wpdb->update(
    				$wpdb->prefix . 'icl_translations',
    				array( 'element_type' => 'post_' . $post_type_object->name,	),
    				array( 'element_id' => $elem_id, 'element_type' => 'post_' . $post->post_type )
    			);
    
    			$wpdb->print_error();
    		}
    
    	}
    
    }

    Now when you change a post type of some post, all translations will be changed also.

    Hope it helps someone.

    TO THE AUTHOR – Why don’t you add it in one of your next releases? It’s so simple and so useful.

    Have fun!

    https://wordpress.org/plugins/post-type-switcher/

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Simple hack to add WPML support’ is closed to new replies.