Last active
May 10, 2022 10:04
-
-
Save stevygee/ed18db68a247c2cd4fb7680d39d41502 to your computer and use it in GitHub Desktop.
Add WPML translations to WP2Static v7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function sg_get_translated_posts_urls() { | |
$default_lang = apply_filters( 'wpml_default_language', NULL ); | |
$ids = get_posts( array( | |
'fields' => 'ids', | |
'post_type' => array( 'post', 'page' ), | |
'numberposts' => -1, | |
'suppress_filters' => true, // Get posts in all languages | |
) ); | |
$permalinks = array(); | |
foreach ( $ids as $id ) { | |
$lang = apply_filters( 'wpml_post_language_details', NULL, $id ); | |
// We only need translations; skip posts in default language | |
if ( $lang['language_code'] === $default_lang ) { | |
continue; | |
} | |
$permalink = get_permalink( $id ); | |
$wpml_permalink = apply_filters( 'wpml_permalink', $permalink, $lang['language_code'] ); | |
// Strip home URL (domain) from permalink | |
$wpml_permalink = str_replace( trailingslashit( home_url() ), '/', $wpml_permalink ); | |
$permalinks[] = $wpml_permalink; | |
} | |
return $permalinks; | |
} | |
function sg_add_additional_urls( $url_queue ) { | |
/* Add WPML translations */ | |
$translation_urls = sg_get_translated_posts_urls(); | |
$url_queue = array_merge( | |
$url_queue, | |
$translation_urls | |
); | |
return $url_queue; | |
} | |
add_filter( 'wp2static_modify_initial_crawl_list', 'sg_add_additional_urls' ); |
Edited: After all I was able to get it working using fertek's suggestion above using the true
parameter after all. For this to work reliably, you need to go to WPML > Languages > and disable "Adjust IDs for multilingual functionality". Otherwise, the IDs get converted and you will be missing some pages during the crawl.
Thank you for sharing these solutions.
@bkno Glad to hear that it works for you now!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@fertek You are right, thanks for the simplification!