To create WPML multilingual RSS feeds in WordPress, you can use the pre_get_posts action hook to modify the query for the RSS feed based on the current language. Here’s an example code snippet that demonstrates how to achieve this:
function wpsnippets_wpml_multilingual_rss_feeds( $query ) {
if ( $query->is_feed() && function_exists( 'icl_object_id' ) ) {
$current_language = apply_filters( 'wpml_current_language', null );
$query->set( 'lang', $current_language );
}
}
add_action( 'pre_get_posts', 'wpsnippets_wpml_multilingual_rss_feeds' );
This code snippet checks if the current request is for a feed and if the WPML plugin is active. If both conditions are met, it retrieves the current language using the wpml_current_language filter and sets the lang query parameter to the current language. This ensures that the RSS feed will only include posts in the current language.
This code snippet can be useful if you have a multilingual website powered by WPML and you want to provide separate RSS feeds for each language. By modifying the query for the RSS feed, you can ensure that only posts in the current language are included in the feed.
Note: This code snippet assumes that you have already installed and activated the WPML plugin on your WordPress site.
Examples
Example 1: Creating a Multilingual RSS Feed
This use case demonstrates how to create a multilingual RSS feed using WPML. The code example below shows how to add a custom feed template and modify the query to include posts from all languages.
function wpsnippets_custom_feed_template( $template ) {
if ( is_feed() && get_query_var( 'feed' ) == 'my-custom-feed' ) {
$template = get_stylesheet_directory() . '/feed-my-custom-feed.php';
}
return $template;
}
add_filter( 'template_include', 'wpsnippets_custom_feed_template' );
function wpsnippets_modify_feed_query( $query ) {
if ( $query->is_feed && $query->get( 'feed' ) == 'my-custom-feed' ) {
$query->set( 'post_type', 'post' );
$query->set( 'post_status', 'publish' );
$query->set( 'ignore_sticky_posts', true );
$query->set( 'lang', '' ); // Include posts from all languages
}
return $query;
}
add_filter( 'pre_get_posts', 'wpsnippets_modify_feed_query' );
The wpsnippets_custom_feed_template function sets a custom template for the feed. In this example, the template file is feed-my-custom-feed.php located in the theme directory. The wpsnippets_modify_feed_query function modifies the query to include posts from all languages by setting the lang parameter to an empty string.
Example 2: Filtering Multilingual RSS Feed by Language
This use case demonstrates how to filter a multilingual RSS feed by language using WPML. The code example below shows how to modify the query to include posts only from a specific language.
function wpsnippets_modify_feed_query( $query ) {
if ( $query->is_feed && $query->get( 'feed' ) == 'my-custom-feed' ) {
$query->set( 'post_type', 'post' );
$query->set( 'post_status', 'publish' );
$query->set( 'ignore_sticky_posts', true );
$query->set( 'lang', 'en' ); // Include posts from English language only
}
return $query;
}
add_filter( 'pre_get_posts', 'wpsnippets_modify_feed_query' );
The wpsnippets_modify_feed_query function modifies the query to include posts only from a specific language. In this example, the lang parameter is set to 'en' to include posts from the English language only.
Example 3: Customizing Multilingual RSS Feed Output
This use case demonstrates how to customize the output of a multilingual RSS feed using WPML. The code example below shows how to modify the feed template to include language-specific information in the feed items.
function wpsnippets_custom_feed_template( $template ) {
if ( is_feed() && get_query_var( 'feed' ) == 'my-custom-feed' ) {
$template = get_stylesheet_directory() . '/feed-my-custom-feed.php';
}
return $template;
}
add_filter( 'template_include', 'wpsnippets_custom_feed_template' );
The wpsnippets_custom_feed_template function sets a custom template for the feed. In this example, the template file is feed-my-custom-feed.php located in the theme directory. You can customize this template to include language-specific information in the feed items, such as translated titles or descriptions.
