NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/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' );