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

@maciejmokrzycki Yes that looks good, should work this way. I just tested again with WP2Static v7.alpha-004 and a snapshot from May 8th and both worked. Please check if you translations show up in the WP2Static logs: WP2Static > Caches > Show URLs/Paths.

@thegulshankumar
Copy link

thegulshankumar commented May 15, 2020

Hi, @stevygee

I have noticed some issues

Suppose, I have Contact Page in English and Spanish with Different languages in directories settings in WPML.

Typically, my homepage looks like example.com/ and example.com/es/

Case 1. It works perfectly if I maintain same permalink for all languages.

example.com/contact/ returns 200
example.com/es/contact/ returns 200

All good.

Case 2. Doesn't work with Translated permalinks

example.com/contact/ returns 200
example.com/es/contacto/ returns 404

Issues

  1. Crawled logs shows 404 for /es/contact/ which should not be because I have already translated permalink (Ref: case 2)
  2. I do not see /es/contacto/ in the crawl logs.
  3. However, the actual content of /es/contacto/ appears at /es/contact/ permalink.

This problem presist for Post and Pages.

Expected: I wish to make content appear at translated URL /es/contacto/ which is for Spanish version of Contact page.

Thanks

@stevygee
Copy link
Author

@thegulshankumar Two things come to mind:

  1. Apologize if this is too obvious, but are your translated permalinks working in the WP frontend? So are you getting 200 for /es/contacto/ when accessing WordPress directly? If not, try to get the permalinks working first.
  2. Clear your WP2Static Cache before deploying. I found that WP2Static v7 often refuses to crawl a page (even if a change has been made), if it was already cached during a previous deployment process.

@fertek
Copy link

fertek commented Jun 19, 2020

Expected: I wish to make content appear at translated URL /es/contacto/ which is for Spanish version of Contact page.

Hi @thegulshankumar

I think this issue can be solved by:
$wpml_permalink = apply_filters( 'wpml_permalink', $permalink, $lang['language_code'] );

$wpml_permalink = apply_filters( 'wpml_permalink', $permalink, $lang['language_code'], true );

See $full_resolution parameter at https://wpml.org/wpml-hook/wpml_permalink/.

@stevygee thanks for sharing! I have an idea for improvement.

	$posts = get_posts( array(
		'post_type'        => array( 'post', 'page' ),
		'numberposts'      => -1,
		'suppress_filters' => true, // Get posts in all languages
	) );

	$ids = wp_list_pluck( $posts, 'ID' );

	$ids = get_posts( array(
		'fields'           => 'ids',
		'post_type'        => array( 'post', 'page' ),
		'numberposts'      => -1,
		'suppress_filters' => true, // Get posts in all languages
	) );

@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