Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_action('rest_api_init', 'polylang_json_api_init' );
- add_action('rest_api_init', 'polylangroute');
- function polylang_json_api_init(){
- global $polylang;
- // $default = pll_default_language();
- $langs = pll_languages_list();
- $cur_lang = $_GET['lang'];
- if (!in_array($cur_lang, $langs)) {
- // $cur_lang = $default;
- return;
- }
- $polylang->curlang = $polylang->model->get_language($cur_lang);
- $GLOBALS['text_direction'] = $polylang->curlang->is_rtl ? 'rtl' : 'ltr';
- }
- function polylang_json_api_languages(){
- return pll_languages_list();
- }
- add_action( 'rest_api_init', 'dt_register_api_fields' );
- function dt_register_api_fields() {
- register_rest_field(
- 'post',
- 'translation',
- array(
- 'get_callback' => function () {
- $terms = wp_get_post_terms(get_the_ID(), 'post_translations', []);
- $description = unserialize($terms[0]->description);
- $res = [];
- foreach ($description as $lang => $post_id) {
- $slug = get_post_field( 'post_name', $post_id );
- $res[$lang] = $slug;
- }
- return $res;
- },
- )
- );
- register_rest_field(
- 'post',
- 'lang',
- array(
- 'get_callback' => function () {
- $terms = wp_get_post_terms(get_the_ID(), 'language', []);
- return $terms[0]->slug;
- },
- )
- );
- register_rest_field(
- 'post',
- 'seo',
- array(
- 'get_callback' => function () {
- $seo = [];
- $seo['keywords'] = get_post_meta(get_the_ID(), '_yoast_wpseo_focuskw')[0];
- $seo['description'] = get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc')[0];
- return $seo;
- },
- )
- );
- register_rest_field(
- 'post',
- 'featured_image_url',
- array(
- 'get_callback' => function($object) {
- $featured_image_id = $object['featured_media'];
- $featured_image_url = wp_get_attachment_image_url( $featured_image_id, 'original');
- return $featured_image_url;
- },
- )
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement