Last active
March 11, 2021 22:32
-
-
Save maxon755/efafd49d75769e6fac8dc7a6e540c569 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
}, | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Код выше добавляет к ответу информацию о текущем языке статьи или страницы, наличие переводов, и тэги.
Разместите код в файле functions.php текущей темы:
/app/wp-content/themes/*some theme*/template-parts/functions.php
или глобально для всей инсталяции:
/app/wp-includes/functions.php
Также необходимо установить следующие плагины: