How to bypass the wordpress API luck of language selection. WPML is payable and only for that I found an API update.
1 Answers
I have build a site in wordpress in order to broadcast some news. I had problem in language selection since the default API doesn't support that and also WPML is payable plugin. So I used Polylang in order to use multi languages. After that the problem to send them to the server and after that to the other APPs came up. What we (with the backend developer) did was the below:
- Create a page template (create a php file in child-theme folder with /* Template Name: CustomPageEl */ )
- After that, when you create a page, you may choose this template "CustomPageEl".
- Create 2 pages, one for each language.
In there a call PHP functions with infos I needed.(I wanted to create the below JSON)
{"id": 835,"date": "2017-06-22T16:06:45", "link": "", "title": "","content": "", "excerpt":"" ,"tags": [], "featureImageLink":"" }
<?php /* Template Name: CustomPageEl */ query_posts('post_type=post&post_status=publish'); $index=0; $mainObj=array(); if( have_posts()): while( have_posts() ): the_post(); $posttags = get_the_tags(); $count=0; if ($posttags) { $index1=0; foreach($posttags as $tag) { $alltags[$index1] = ($tag->name); $index1=$index1+1; } } $imageurl=get_the_post_thumbnail(); if (!function_exists('my_custom_function')) { function my_custom_function($string, $start, $end){ $string = ' ' . $string; $ini = strpos($string, $start); if ($ini == 0) return ''; $ini += strlen($start); $len = strpos($string, $end, $ini) - $ini; return substr($string, $ini, $len); } } $imageurl = my_custom_function($imageurl, 'src="', '" class='); $mainObj[$index]=array('id' =>get_the_ID(), 'date'=>get_the_date('c'),'link'=>get_permalink(),'title'=>get_the_title(),'content'=>str_replace('"','\'',get_the_content()),'excerpt'=>str_replace('"','\'',get_the_excerpt()),'tags'=>$alltags,'featureImageLink'=>$imageurl); $index=$index+1; endwhile; $finalObf = json_encode($mainObj); wp_send_json($finalObf); endif; wp_reset_query(); ?>
The result is when you enter the page, you get an Array of JSON with posts, in the language the page is.
In order to choose the functions for wordpress see the below link. https://codex.wordpress.org/Function_Reference

- 342
- 3
- 14