Skip to content

Instantly share code, notes, and snippets.

@bkno
Forked from stevygee/wp2static_v7_wpml.php
Last active May 10, 2022 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkno/3785a4c337401a96f9ea64a9d1828d1a to your computer and use it in GitHub Desktop.
Save bkno/3785a4c337401a96f9ea64a9d1828d1a to your computer and use it in GitHub Desktop.
Add WPML translations to WP2Static v7
<?php
/**
* ------------------------------
* | WP2Static WPML Integration |
* ------------------------------
*
* Important: The option WPML > Languages > "Adjust IDs for multilingual functionality" must be disabled.
*/
function wp2static_wpml_get_translated_posts_urls() {
// Get all public custom post types
$post_types = get_post_types( array( 'public' => true ), 'names' );
// Fetch all post IDs across all translations and public post types
$ids = get_posts( array(
'fields' => 'ids',
'post_type' => $post_types,
'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 );
$permalink = get_permalink( $id );
// Strip domain from permalink
$permalink = parse_url( $permalink, PHP_URL_PATH );
$permalinks[] = $permalink;
}
return $permalinks;
}
function wp2static_wpml_add_additional_urls( $url_queue ) {
$translation_urls = wp2static_wpml_get_translated_posts_urls();
$url_queue = array_merge(
$url_queue,
$translation_urls
);
return $url_queue;
}
add_filter( 'wp2static_modify_initial_crawl_list', 'wp2static_wpml_add_additional_urls' );
@bkno
Copy link
Author

bkno commented May 9, 2022

Please note: 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.

@bkno
Copy link
Author

bkno commented May 10, 2022

In fact.. if you disable that option you may not need this script at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment