Skip to content

Instantly share code, notes, and snippets.

@stevygee
Last active May 10, 2022 10:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stevygee/ed18db68a247c2cd4fb7680d39d41502 to your computer and use it in GitHub Desktop.
Save stevygee/ed18db68a247c2cd4fb7680d39d41502 to your computer and use it in GitHub Desktop.
Add WPML translations to WP2Static v7
<?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' );
@stevygee
Copy link
Author

@fertek You are right, thanks for the simplification!

@bkno
Copy link

bkno commented May 6, 2022

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.

@stevygee
Copy link
Author

@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