NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

functions.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Theme Functions - functions.php
  4. *
  5. * 1. Scripts needed to run the vue theme
  6. */
  7. $path = $_SERVER['DOCUMENT_ROOT'];
  8. require_once $path . '/wp-load.php';
  9. require_once $path . '/wp-config.php';
  10. require_once $path . '/wp-includes/wp-db.php';
  11. require_once $path . '/wp-includes/pluggable.php';
  12. // https://github.com/EvanAgee/vuejs-wordpress-theme-starter/blob/master/functions.php
  13. remove_action( 'template_redirect', 'redirect_canonical' );
  14. function remove_redirects() {
  15. add_rewrite_rule( '^/(.+)/?', 'index.php', 'top' );
  16. }
  17. add_action( 'init', 'remove_redirects' );
  18. function vue_theme_routes() {
  19. $routes = array();
  20. $query = new WP_Query( array(
  21. 'post_type' => 'any',
  22. 'post_status' => 'publish',
  23. 'posts_per_page' => -1
  24. ) );
  25. if( $query->have_posts() ) {
  26. while( $query->have_posts() ) {
  27. $query->the_post();
  28. $routes[] = array(
  29. 'id' => get_the_ID(),
  30. 'type' => get_post_type(),
  31. 'slug' => basename( get_permalink() ),
  32. );
  33. }
  34. }
  35. wp_reset_postdata();
  36. return $routes;
  37. }
  38. /* 1 */
  39. function vue_theme_scripts() {
  40. wp_enqueue_style( 'style', get_stylesheet_uri() );
  41. if ( defined( 'IS_DEV' ) && IS_DEV ) {
  42. wp_register_script(
  43. 'vue-theme',
  44. 'http://localhost:8081/build/main.js',
  45. array( 'jquery' ),
  46. false,
  47. true
  48. );
  49. } else {
  50. wp_register_script(
  51. 'vue-theme',
  52. get_template_directory_uri() . '/public/main.js',
  53. array( 'jquery' ),
  54. false,
  55. true
  56. );
  57. }
  58. wp_localize_script( 'vue-theme', 'wp', array(
  59. 'template' => get_stylesheet_directory_uri(),
  60. 'rest' => esc_url_raw( rest_url() ),
  61. 'site_name' => get_bloginfo( 'name' ),
  62. // 'nonce' => wp_create_nonce( 'wp_rest' ),
  63. 'routes' => vue_theme_routes(),
  64. ) );
  65. wp_enqueue_script( 'vue-theme' );
  66. }
  67. add_action( 'wp_enqueue_scripts', 'vue_theme_scripts' );
  68. /**
  69. * disable endpoints in rest api
  70. **/
  71. add_filter( 'rest_endpoints', function ( $endpoints ) {
  72. if ( isset( $endpoints['/wp/v2/users'] ) ) {
  73. unset( $endpoints['/wp/v2/users'] );
  74. }
  75. if ( isset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] ) ) {
  76. unset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] );
  77. }
  78. if ( isset( $endpoints['/wp/v2/comments'] ) ) {
  79. unset( $endpoints['/wp/v2/comments'] );
  80. }
  81. if ( isset( $endpoints['/wp/v2/comments/(?P<id>[\d]+)'] ) ) {
  82. unset( $endpoints['/wp/v2/comments/(?P<id>[\d]+)'] );
  83. }
  84. if ( isset( $endpoints['/wp/v2/settings'] ) ) {
  85. unset( $endpoints['/wp/v2/settings'] );
  86. }
  87. return $endpoints;
  88. } );
  89. /**
  90. * remove wordpress version number from files
  91. **/
  92. remove_action( 'wp_head', 'wp_generator' );