Advertisement
Guest User

Untitled

a guest
Sep 1st, 2019
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. <?php
  2.  
  3. add_action('rest_api_init', 'polylang_json_api_init' );
  4. add_action('rest_api_init', 'polylangroute');
  5.  
  6. function polylang_json_api_init(){
  7. global $polylang;
  8. // $default = pll_default_language();
  9. $langs = pll_languages_list();
  10. $cur_lang = $_GET['lang'];
  11. if (!in_array($cur_lang, $langs)) {
  12. // $cur_lang = $default;
  13. return;
  14. }
  15. $polylang->curlang = $polylang->model->get_language($cur_lang);
  16. $GLOBALS['text_direction'] = $polylang->curlang->is_rtl ? 'rtl' : 'ltr';
  17. }
  18.  
  19. function polylang_json_api_languages(){
  20. return pll_languages_list();
  21. }
  22.  
  23.  
  24. add_action( 'rest_api_init', 'dt_register_api_fields' );
  25. function dt_register_api_fields() {
  26. register_rest_field(
  27. 'post',
  28. 'translation',
  29. array(
  30. 'get_callback' => function () {
  31. $terms = wp_get_post_terms(get_the_ID(), 'post_translations', []);
  32. $description = unserialize($terms[0]->description);
  33.  
  34. $res = [];
  35. foreach ($description as $lang => $post_id) {
  36. $slug = get_post_field( 'post_name', $post_id );
  37. $res[$lang] = $slug;
  38. }
  39.  
  40. return $res;
  41. },
  42. )
  43. );
  44.  
  45. register_rest_field(
  46. 'post',
  47. 'lang',
  48. array(
  49. 'get_callback' => function () {
  50. $terms = wp_get_post_terms(get_the_ID(), 'language', []);
  51.  
  52. return $terms[0]->slug;
  53. },
  54. )
  55. );
  56.  
  57. register_rest_field(
  58. 'post',
  59. 'seo',
  60. array(
  61. 'get_callback' => function () {
  62. $seo = [];
  63.  
  64. $seo['keywords'] = get_post_meta(get_the_ID(), '_yoast_wpseo_focuskw')[0];
  65. $seo['description'] = get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc')[0];
  66.  
  67. return $seo;
  68. },
  69. )
  70. );
  71.  
  72. register_rest_field(
  73. 'post',
  74. 'featured_image_url',
  75. array(
  76. 'get_callback' => function($object) {
  77. $featured_image_id = $object['featured_media'];
  78. $featured_image_url = wp_get_attachment_image_url( $featured_image_id, 'original');
  79.  
  80. return $featured_image_url;
  81. },
  82. )
  83. );
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement