-
-
Save bkno/3785a4c337401a96f9ea64a9d1828d1a 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 | |
/** | |
* ------------------------------ | |
* | 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' ); |
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
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.