I struggle creating separate page feeds (list of URLs) in each language when WPML is active.
The plan is to have different feed names for each language, such as feed_name_en
or feed_name_fr
.
I wrote the following function that iterates through each available language on the site and should output a feed for each language containing only the pages in the particular language.
I found other examples that show how to pass parameters within add_action
using closures. (eg. add_action('init', function() use($param) { some_out_of_scope_function($param) } )
But in my function I am getting the following error:
Object of class Closure could not be converted to string in
I have the suspicion that it is because I am using OOP to encapsule the function in an array add_feed( $feedname, array( $this, ... ) )
. But I have no idea how to work around this.
How shall I approach this?
$feedname = 'feed_name';
private function createLanguageFeeds(){
foreach($this->wpml_languages as $lang){
// add the lang to the feed name. example: feed_name_en
$feedname = $this->feedname . '_' . $lang;
// making sure that the feed is created
if ( ! in_array( $feedname, $wp_rewrite->feeds ) ) {
$wp_rewrite->feeds[] = $feedname;
flush_rewrite_rules( FALSE );
}
add_feed( $feedname, array( $this, function() use ($lang) {$this->create_page_feed($lang);} ) );
}
}
private function create_page_feed($lang){
// echo pages
}