NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

functions.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Theme Functions - functions.php
  4. *
  5. * 1. Scripts needed to run the vue theme
  6. */
  7. // Load WP stuff as needed
  8. // https://www.smashingmagazine.com/2018/10/headless-wordpress-decoupled/#improving-performance-decoupled-json-approach
  9. define( 'SHORTINIT', true );
  10. $parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
  11. // require_once filter_var( $parse_uri[0] . 'wp-load.php', FILTER_SANITIZE_STRING );
  12. $path = $_SERVER['DOCUMENT_ROOT'];
  13. require_once $path . '/wp-load.php';
  14. require_once $path . '/wp-config.php';
  15. require_once $path . '/wp-includes/wp-db.php';
  16. require_once $path . '/wp-includes/pluggable.php';
  17. // https://github.com/EvanAgee/vuejs-wordpress-theme-starter/blob/master/functions.php
  18. remove_action( 'template_redirect', 'redirect_canonical' );
  19. function remove_redirects() {
  20. add_rewrite_rule( '^/(.+)/?', 'index.php', 'top' );
  21. }
  22. add_action( 'init', 'remove_redirects' );
  23. function vue_theme_routes() {
  24. $routes = array();
  25. $query = new WP_Query( array(
  26. 'post_type' => 'any',
  27. 'post_status' => 'publish',
  28. 'posts_per_page' => -1
  29. ) );
  30. if( $query->have_posts() ) {
  31. while( $query->have_posts() ) {
  32. $query->the_post();
  33. $routes[] = array(
  34. 'id' => get_the_ID(),
  35. 'type' => get_post_type(),
  36. 'slug' => basename( get_permalink() ),
  37. );
  38. }
  39. }
  40. wp_reset_postdata();
  41. return $routes;
  42. }
  43. /* 1 */
  44. function vue_theme_scripts() {
  45. wp_enqueue_style( 'style', get_stylesheet_uri() );
  46. if ( defined( 'IS_DEV' ) && IS_DEV ) {
  47. wp_register_script(
  48. 'vue-theme',
  49. 'http://localhost:8081/build/main.js',
  50. array( 'jquery' ),
  51. false,
  52. true
  53. );
  54. } else {
  55. wp_register_script(
  56. 'vue-theme',
  57. get_template_directory_uri() . '/public/main.js',
  58. array( 'jquery' ),
  59. false,
  60. true
  61. );
  62. }
  63. wp_localize_script( 'vue-theme', 'wp', array(
  64. 'template' => get_stylesheet_directory_uri(),
  65. 'rest' => esc_url_raw( rest_url() ),
  66. 'site_name' => get_bloginfo( 'name' ),
  67. // 'nonce' => wp_create_nonce( 'wp_rest' ),
  68. 'routes' => vue_theme_routes(),
  69. ) );
  70. wp_enqueue_script( 'vue-theme' );
  71. }
  72. add_action( 'wp_enqueue_scripts', 'vue_theme_scripts' );
  73. /**
  74. * disable endpoints in rest api
  75. **/
  76. add_filter( 'rest_endpoints', function ( $endpoints ) {
  77. if ( isset( $endpoints['/wp/v2/users'] ) ) {
  78. unset( $endpoints['/wp/v2/users'] );
  79. }
  80. if ( isset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] ) ) {
  81. unset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] );
  82. }
  83. if ( isset( $endpoints['/wp/v2/comments'] ) ) {
  84. unset( $endpoints['/wp/v2/comments'] );
  85. }
  86. if ( isset( $endpoints['/wp/v2/comments/(?P<id>[\d]+)'] ) ) {
  87. unset( $endpoints['/wp/v2/comments/(?P<id>[\d]+)'] );
  88. }
  89. if ( isset( $endpoints['/wp/v2/settings'] ) ) {
  90. unset( $endpoints['/wp/v2/settings'] );
  91. }
  92. return $endpoints;
  93. } );
  94. /**
  95. * remove wordpress version number from files
  96. **/
  97. remove_action( 'wp_head', 'wp_generator' );
  98. // Disable XML-RPC
  99. add_filter('xmlrpc_enabled', '__return_false');
  100. // Disable WLManifest
  101. remove_action( 'wp_head', 'wlwmanifest_link' ) ;
  102. // Disable self ping
  103. function disable_pingback( &$links ) {
  104. foreach ( $links as $l => $link )
  105. if ( 0 === strpos( $link, get_option( 'home' ) ) )
  106. unset($links[$l]);
  107. }
  108. add_action( 'pre_ping', 'disable_pingback' );
  109. // Disable RSD (used with self ping and XML-RPC)
  110. remove_action( 'wp_head', 'rsd_link' ) ;
  111. // Disable post embed
  112. function disable_embed() { wp_dequeue_script( 'wp-embed' ); }
  113. add_action( 'wp_footer', 'disable_embed' );
  114. // Disable Shortlinking
  115. remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
  116. // Remove versions from static assets (prevents cache-ing)
  117. function remove_cssjs_ver( $src ) {
  118. if( strpos( $src, '?ver=' ) )
  119. $src = remove_query_arg( 'ver', $src );
  120. return $src;
  121. }
  122. add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
  123. add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
  124. // Do NOT include jquery
  125. function no_default_jquery( ) {
  126. if (!is_admin()) {
  127. wp_deregister_script('jquery');
  128. wp_register_script('jquery', false);
  129. }
  130. }
  131. add_action( 'init', 'no_default_jquery' );
  132. // header( 'Access-Control-Allow-Origin: http://localhost:8080' );
  133. header( 'Content-Type: application/json' );