NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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( $parse_uri[0] . 'wp-load.php' );
  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 === 'true') {
  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() . '/build/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. // Remove wordpress version number from files
  95. remove_action( 'wp_head', 'wp_generator' );
  96. // Disable XML-RPC
  97. add_filter('xmlrpc_enabled', '__return_false');
  98. // Disable WLManifest
  99. remove_action( 'wp_head', 'wlwmanifest_link' ) ;
  100. // Disable self ping
  101. function disable_pingback( &$links ) {
  102. foreach ( $links as $l => $link )
  103. if ( 0 === strpos( $link, get_option( 'home' ) ) )
  104. unset($links[$l]);
  105. }
  106. add_action( 'pre_ping', 'disable_pingback' );
  107. // Disable RSD (used with self ping and XML-RPC)
  108. remove_action( 'wp_head', 'rsd_link' ) ;
  109. // Disable post embed
  110. function disable_embed() { wp_dequeue_script( 'wp-embed' ); }
  111. add_action( 'wp_footer', 'disable_embed' );
  112. // Disable Shortlinking
  113. remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
  114. // Remove versions from static assets (prevents cache-ing)
  115. function remove_cssjs_ver( $src ) {
  116. if( strpos( $src, '?ver=' ) )
  117. $src = remove_query_arg( 'ver', $src );
  118. return $src;
  119. }
  120. // !:Possible Bug HERE
  121. add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
  122. add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
  123. // Do NOT include jquery
  124. function no_default_jquery( ) {
  125. if (!is_admin()) {
  126. wp_deregister_script('jquery');
  127. wp_register_script('jquery', false);
  128. }
  129. }
  130. add_action( 'init', 'no_default_jquery' );
  131. // REMOVE emoji support
  132. remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
  133. remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  134. remove_action( 'wp_print_styles', 'print_emoji_styles' );
  135. remove_action( 'admin_print_styles', 'print_emoji_styles' );
  136. // REMOVE block CSS
  137. function webapptiv_remove_block_library_css() {
  138. wp_dequeue_style( 'wp-block-library' );
  139. }
  140. add_action( 'wp_enqueue_scripts', 'webapptiv_remove_block_library_css' );
  141. // header( 'Access-Control-Allow-Origin: http://localhost:8080' );
  142. header( 'Content-Type: application/json' );
  143. // Add featured image support
  144. function craft_post_thumbnails() { add_theme_support( 'post-thumbnails' ); }
  145. add_action( 'after_setup_theme', 'craft_post_thumbnails' );
  146. // Gutenberg custom stylesheet for editor
  147. add_theme_support('editor-styles');
  148. add_editor_style( 'editor-style.css' );