add_filter( 'wpml_new_post_language', 'the_callback_function', 10, 2 );
Description
- Filters the language of a post that has no language yet, when WPML saves it for the first time. A post that already has a language keeps it.
- Use it in a plugin that creates posts without a language, for example an importer. Without the filter, these posts get the language of the request, which is often the language selected in the admin bar.
- The filter runs for every new post. Return a different language only for the posts that your own code creates.
Arguments
Two parameters are passed to this filter: language and post_id.
$languagestringThe language code that WPML takes from the request: the language selected in the post form, thelangURL argument, or the current language.$post_idintThe ID of the new post.ReturnsstringThe language code of the new post. WPML uses only an active language. Any other value keeps the language of the request.
Example Usage
Give the posts that your importer creates the default language of the site:
add_filter( 'wpml_new_post_language', function ( $language, $post_id ) { if ( did_action( 'my_importer_start' ) && ! did_action( 'my_importer_end' ) ) { return apply_filters( 'wpml_default_language', $language ); } return $language; }, 10, 2 );