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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. $remove = [
  78. 'users',
  79. 'users/(?P<id>[\d]+)',
  80. 'comments',
  81. 'comments/(?P<id>[\d]+)',
  82. 'settings',
  83. ];
  84. foreach( $remove as $endpoint )
  85. if ( isset( $endpoints['/wp/v2/' . $endpoint] ) )
  86. unset( $endpoints['/wp/v2/' . $endpoint] );
  87. return $endpoints;
  88. } );
  89. // Remove wordpress version number from files
  90. remove_action( 'wp_head', 'wp_generator' );
  91. // Disable XML-RPC
  92. add_filter('xmlrpc_enabled', '__return_false');
  93. // Disable WLManifest
  94. remove_action( 'wp_head', 'wlwmanifest_link' ) ;
  95. // Disable self ping
  96. function disable_pingback( &$links ) {
  97. foreach ( $links as $l => $link )
  98. if ( 0 === strpos( $link, get_option( 'home' ) ) )
  99. unset($links[$l]);
  100. }
  101. add_action( 'pre_ping', 'disable_pingback' );
  102. // Disable RSD (used with self ping and XML-RPC)
  103. remove_action( 'wp_head', 'rsd_link' ) ;
  104. // Disable post embed
  105. function disable_embed() { wp_dequeue_script( 'wp-embed' ); }
  106. add_action( 'wp_footer', 'disable_embed' );
  107. // Disable Shortlinking
  108. remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
  109. // Remove versions from static assets (prevents cache-ing)
  110. function remove_cssjs_ver( $src ) {
  111. if( strpos( $src, '?ver=' ) )
  112. $src = remove_query_arg( 'ver', $src );
  113. return $src;
  114. }
  115. // !:Possible Bug HERE
  116. add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
  117. add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
  118. // Do NOT include jquery
  119. function no_default_jquery( ) {
  120. if (!is_admin()) {
  121. wp_deregister_script('jquery');
  122. wp_register_script('jquery', false);
  123. }
  124. }
  125. add_action( 'init', 'no_default_jquery' );
  126. // REMOVE emoji support
  127. remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
  128. remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  129. remove_action( 'wp_print_styles', 'print_emoji_styles' );
  130. remove_action( 'admin_print_styles', 'print_emoji_styles' );
  131. // REMOVE block CSS
  132. function webapptiv_remove_block_library_css() {
  133. wp_dequeue_style( 'wp-block-library' );
  134. }
  135. // add_action( 'wp_enqueue_scripts', 'webapptiv_remove_block_library_css' );
  136. // header( 'Access-Control-Allow-Origin: http://localhost:8080' );
  137. header( 'Content-Type: application/json' );
  138. // Add featured image support
  139. function craft_post_thumbnails() { add_theme_support( 'post-thumbnails' ); }
  140. add_action( 'after_setup_theme', 'craft_post_thumbnails' );
  141. // Gutenberg custom stylesheet for editor
  142. add_theme_support('editor-styles');
  143. add_editor_style( 'editor-style.css' );
  144. // Add the custom columns for EVENT:
  145. add_filter( 'manage_event_posts_columns', 'set_custom_edit_event_columns' );
  146. function set_custom_edit_event_columns($columns) {
  147. $date = $columns['date'];
  148. unset( $columns['date'] );
  149. $columns['event_type'] = __('Event Type');
  150. $columns['date'] = $date;
  151. return $columns;
  152. }
  153. // Add the data to the custom columns for EVENT:
  154. add_action( 'manage_event_posts_custom_column' , 'custom_event_column', 10, 2 );
  155. function custom_event_column( $column, $post_id ) {
  156. switch ( $column ) {
  157. case 'event_type' :
  158. $terms = get_the_term_list( $post_id , 'event_type' , '' , ',' , '' );
  159. echo is_string( $terms ) ? $terms : '—';
  160. break;
  161. }
  162. }
  163. // Add the custom columns for ARTIST:
  164. add_filter( 'manage_artist_posts_columns', 'set_custom_edit_artist_columns' );
  165. function set_custom_edit_artist_columns($columns) {
  166. $date = $columns['date'];
  167. unset( $columns['date'] );
  168. $columns['artist_type'] = __('Artist Type');
  169. $columns['material'] = __('Material');
  170. $columns['date'] = $date;
  171. return $columns;
  172. }
  173. // Add the data to the custom columns for ARTIST:
  174. add_action( 'manage_artist_posts_custom_column' , 'custom_artist_column', 10, 2 );
  175. function custom_artist_column( $column, $post_id ) {
  176. switch ( $column ) {
  177. case 'artist_type' :
  178. $terms = get_the_term_list( $post_id , 'artist_type' , '' , ',' , '' );
  179. echo is_string( $terms ) ? $terms : '—';
  180. break;
  181. case 'material' :
  182. $terms = get_the_term_list( $post_id , 'material' , '' , ',' , '' );
  183. echo is_string( $terms ) ? $terms : '—';
  184. break;
  185. }
  186. }
  187. // Add the custom columns for SHORT:
  188. add_filter( 'manage_short_posts_columns', 'set_custom_edit_short_columns' );
  189. function set_custom_edit_short_columns($columns) {
  190. $date = $columns['date'];
  191. unset( $columns['date'] );
  192. $columns['material'] = __('Material');
  193. $columns['date'] = $date;
  194. return $columns;
  195. }
  196. // Add the data to the custom columns for SHORT:
  197. add_action( 'manage_short_posts_custom_column' , 'custom_short_column', 10, 2 );
  198. function custom_short_column( $column, $post_id ) {
  199. switch ( $column ) {
  200. case 'material' :
  201. $terms = get_the_term_list( $post_id , 'material' , '' , ',' , '' );
  202. echo is_string( $terms ) ? $terms : '—';
  203. break;
  204. }
  205. }
  206. // Add the custom columns for GUIDE:
  207. add_filter( 'manage_guide_posts_columns', 'set_custom_edit_guide_columns' );
  208. function set_custom_edit_guide_columns($columns) {
  209. $date = $columns['date'];
  210. unset( $columns['date'] );
  211. $columns['material'] = __('Material');
  212. $columns['date'] = $date;
  213. return $columns;
  214. }
  215. // Add the data to the custom columns for GUIDE:
  216. add_action( 'manage_guide_posts_custom_column' , 'custom_guide_column', 10, 2 );
  217. function custom_guide_column( $column, $post_id ) {
  218. switch ( $column ) {
  219. case 'material' :
  220. $terms = get_the_term_list( $post_id , 'material' , '' , ',' , '' );
  221. echo is_string( $terms ) ? $terms : '—';
  222. break;
  223. }
  224. }
  225. // Add the custom columns for OBJECT:
  226. add_filter( 'manage_object_posts_columns', 'set_custom_edit_object_columns' );
  227. function set_custom_edit_object_columns($columns) {
  228. $date = $columns['date'];
  229. unset( $columns['date'] );
  230. $columns['material'] = __('Material');
  231. $columns['date'] = $date;
  232. return $columns;
  233. }
  234. // Add the data to the custom columns for OBJECT:
  235. add_action( 'manage_object_posts_custom_column' , 'custom_object_column', 10, 2 );
  236. function custom_object_column( $column, $post_id ) {
  237. switch ( $column ) {
  238. case 'material' :
  239. $terms = get_the_term_list( $post_id , 'material' , '' , ',' , '' );
  240. echo is_string( $terms ) ? $terms : '—';
  241. break;
  242. }
  243. }
  244. // Add the custom columns for PUBLICATION:
  245. add_filter( 'manage_publication_posts_columns', 'set_custom_edit_publication_columns' );
  246. function set_custom_edit_publication_columns($columns) {
  247. $date = $columns['date'];
  248. unset( $columns['date'] );
  249. $columns['material'] = __('Material');
  250. $columns['date'] = $date;
  251. return $columns;
  252. }
  253. // Add the data to the custom columns for PUBLICATION:
  254. add_action( 'manage_guide_posts_custom_column' , 'custom_publication_column', 10, 2 );
  255. function custom_publication_column( $column, $post_id ) {
  256. switch ( $column ) {
  257. case 'material' :
  258. $terms = get_the_term_list( $post_id , 'material' , '' , ',' , '' );
  259. echo is_string( $terms ) ? $terms : '—';
  260. break;
  261. }
  262. }
  263. // Add the custom columns for TECHNIQUE:
  264. add_filter( 'manage_technique_posts_columns', 'set_custom_edit_technique_columns' );
  265. function set_custom_edit_technique_columns($columns) {
  266. $date = $columns['date'];
  267. unset( $columns['date'] );
  268. $columns['material'] = __('Material');
  269. $columns['date'] = $date;
  270. return $columns;
  271. }
  272. // Add the data to the custom columns for TECHNIQUE:
  273. add_action( 'manage_technique_posts_custom_column' , 'custom_technique_column', 10, 2 );
  274. function custom_technique_column( $column, $post_id ) {
  275. switch ( $column ) {
  276. case 'material' :
  277. $terms = get_the_term_list( $post_id , 'material' , '' , ',' , '' );
  278. echo is_string( $terms ) ? $terms : '—';
  279. break;
  280. }
  281. }