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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. // Custom Dashboard Menu Order
  145. function custom_menu_order($menu_ord) {
  146. if (!$menu_ord) return true;
  147. return array(
  148. 'index.php', // this represents the dashboard link
  149. 'upload.php', // this is the MEDIA library
  150. 'edit.php', // default POST
  151. 'edit.php?post_type=artist',
  152. 'edit.php?post_type=exhibition',
  153. 'edit.php?post_type=event',
  154. 'edit.php?post_type=episode',
  155. 'edit.php?post_type=short',
  156. 'edit.php?post_type=technique',
  157. 'edit.php?post_type=guide',
  158. 'edit.php?post_type=object',
  159. 'edit.php?post_type=publication',
  160. 'edit.php?post_type=page', // PAGES
  161. );
  162. }
  163. add_filter('custom_menu_order', 'custom_menu_order');
  164. add_filter('menu_order', 'custom_menu_order');
  165. /*
  166. * Remove COMMENTS in its entirety
  167. */
  168. // Removes COMMENTS from admin menu
  169. add_action( 'admin_menu', 'remove_admin_menus' );
  170. function remove_admin_menus() {
  171. remove_menu_page( 'edit-comments.php' );
  172. }
  173. // Removes COMMENTS from post and pages
  174. add_action('init', 'remove_comment_support', 100);
  175. function remove_comment_support() {
  176. remove_post_type_support( 'post', 'comments' );
  177. remove_post_type_support( 'page', 'comments' );
  178. }
  179. // Removes COMMENTS from admin bar
  180. add_action( 'wp_before_admin_bar_render', 'remove_comments_admin_bar' );
  181. function remove_comments_admin_bar() {
  182. global $wp_admin_bar;
  183. $wp_admin_bar->remove_menu('comments');
  184. }
  185. /*
  186. * ADMIN CUSTOM COLUMNS
  187. */
  188. // Add the custom columns for ARTIST:
  189. add_filter( 'manage_artist_posts_columns', 'set_custom_edit_artist_columns' );
  190. function set_custom_edit_artist_columns($columns) {
  191. $date = $columns['date'];
  192. unset( $columns['date'] );
  193. $columns['artist_type'] = __('Artist Type' );
  194. $columns['material'] = __('Material' );
  195. $columns['date'] = $date;
  196. return $columns;
  197. }
  198. // Add the data to the custom columns for ARTIST:
  199. add_action( 'manage_artist_posts_custom_column' , 'custom_artist_column', 10, 2 );
  200. function custom_artist_column( $column, $post_id ) {
  201. switch ( $column ) {
  202. // display a list of artist_type terms assigned to the post
  203. case 'artist_type' :
  204. $terms = get_the_term_list( $post_id , 'artist_type' , '' , ', ' , '' );
  205. echo is_string( $terms ) ? $terms : '—';
  206. break;
  207. // display a list of material terms assigned to the post
  208. case 'material' :
  209. $terms = get_the_term_list( $post_id , 'material' , '' , ', ' , '' );
  210. echo is_string( $terms ) ? $terms : '—';
  211. break;
  212. }
  213. }
  214. // Add the custom columns for EVENT:
  215. add_filter( 'manage_event_posts_columns', 'set_custom_edit_event_columns' );
  216. function set_custom_edit_event_columns($columns) {
  217. $date = $columns['date'];
  218. unset( $columns['date'] );
  219. $columns['event_type'] = __('Event Type' );
  220. $columns['material'] = __('Material' );
  221. $columns['date'] = $date;
  222. return $columns;
  223. }
  224. // Add the data to the custom columns for EVENT:
  225. add_action( 'manage_event_posts_custom_column' , 'custom_event_column', 10, 2 );
  226. function custom_event_column( $column, $post_id ) {
  227. switch ( $column ) {
  228. case 'event_type' :
  229. $terms = get_the_term_list( $post_id , 'event_type' , '' , ', ' , '' );
  230. echo is_string( $terms ) ? $terms : '—';
  231. break;
  232. // display a list of material terms assigned to the post
  233. case 'material' :
  234. $terms = get_the_term_list( $post_id , 'material' , '' , ', ' , '' );
  235. echo is_string( $terms ) ? $terms : '—';
  236. break;
  237. }
  238. }
  239. // Add the custom columns for EXHIBITION:
  240. add_filter( 'manage_exhibition_posts_columns', 'set_custom_edit_exhibition_columns' );
  241. function set_custom_edit_exhibition_columns($columns) {
  242. $date = $columns['date'];
  243. unset( $columns['date'] );
  244. $columns['material'] = __('Material');
  245. $columns['date'] = $date;
  246. return $columns;
  247. }
  248. // Add the data to the custom columns for EXHIBITION:
  249. add_action( 'manage_exhibition_posts_custom_column' , 'custom_exhibition_column', 10, 2 );
  250. function custom_exhibition_column( $column, $post_id ) {
  251. switch ( $column ) {
  252. case 'material' :
  253. $terms = get_the_term_list( $post_id , 'material' , '' , ', ' , '' );
  254. echo is_string( $terms ) ? $terms : '—';
  255. break;
  256. }
  257. }
  258. // Add the custom columns for SHORT:
  259. add_filter( 'manage_short_posts_columns', 'set_custom_edit_short_columns' );
  260. function set_custom_edit_short_columns($columns) {
  261. $date = $columns['date'];
  262. unset( $columns['date'] );
  263. $columns['material'] = __('Material');
  264. $columns['date'] = $date;
  265. return $columns;
  266. }
  267. // Add the data to the custom columns for SHORT:
  268. add_action( 'manage_short_posts_custom_column' , 'custom_short_column', 10, 2 );
  269. function custom_short_column( $column, $post_id ) {
  270. switch ( $column ) {
  271. case 'material' :
  272. $terms = get_the_term_list( $post_id , 'material' , '' , ', ' , '' );
  273. echo is_string( $terms ) ? $terms : '—';
  274. break;
  275. }
  276. }
  277. // Add the custom columns for TECHNIQUE:
  278. add_filter( 'manage_technique_posts_columns', 'set_custom_edit_technique_columns' );
  279. function set_custom_edit_technique_columns($columns) {
  280. $date = $columns['date'];
  281. unset( $columns['date'] );
  282. $columns['material'] = __('Material');
  283. $columns['date'] = $date;
  284. return $columns;
  285. }
  286. // Add the data to the custom columns for TECHNIQUE:
  287. add_action( 'manage_technique_posts_custom_column' , 'custom_technique_column', 10, 2 );
  288. function custom_technique_column( $column, $post_id ) {
  289. switch ( $column ) {
  290. case 'material' :
  291. $terms = get_the_term_list( $post_id , 'material' , '' , ', ' , '' );
  292. echo is_string( $terms ) ? $terms : '—';
  293. break;
  294. }
  295. }
  296. // Add the custom columns for GUIDE:
  297. add_filter( 'manage_guide_posts_columns', 'set_custom_edit_guide_columns' );
  298. function set_custom_edit_guide_columns($columns) {
  299. $date = $columns['date'];
  300. unset( $columns['date'] );
  301. $columns['material'] = __('Material');
  302. $columns['date'] = $date;
  303. return $columns;
  304. }
  305. // Add the data to the custom columns for GUIDE:
  306. add_action( 'manage_guide_posts_custom_column' , 'custom_guide_column', 10, 2 );
  307. function custom_guide_column( $column, $post_id ) {
  308. switch ( $column ) {
  309. case 'material' :
  310. $terms = get_the_term_list( $post_id , 'material' , '' , ', ' , '' );
  311. echo is_string( $terms ) ? $terms : '—';
  312. break;
  313. }
  314. }
  315. // Add the custom columns for OBJECT:
  316. add_filter( 'manage_object_posts_columns', 'set_custom_edit_object_columns' );
  317. function set_custom_edit_object_columns($columns) {
  318. $date = $columns['date'];
  319. unset( $columns['date'] );
  320. $columns['material'] = __('Material');
  321. $columns['date'] = $date;
  322. return $columns;
  323. }
  324. // Add the data to the custom columns for OBJECT:
  325. add_action( 'manage_object_posts_custom_column' , 'custom_object_column', 10, 2 );
  326. function custom_object_column( $column, $post_id ) {
  327. switch ( $column ) {
  328. case 'material' :
  329. $terms = get_the_term_list( $post_id , 'material' , '' , ', ' , '' );
  330. echo is_string( $terms ) ? $terms : '—';
  331. break;
  332. }
  333. }
  334. // Add the custom columns for PUBLICATION:
  335. add_filter( 'manage_publication_posts_columns', 'set_custom_edit_publication_columns' );
  336. function set_custom_edit_publication_columns($columns) {
  337. $date = $columns['date'];
  338. unset( $columns['date'] );
  339. $columns['material'] = __('Material');
  340. $columns['date'] = $date;
  341. return $columns;
  342. }
  343. // Add the data to the custom columns for PUBLICATION:
  344. add_action( 'manage_publication_posts_custom_column' , 'custom_publication_column', 10, 2 );
  345. function custom_publication_column( $column, $post_id ) {
  346. switch ( $column ) {
  347. case 'material' :
  348. $terms = get_the_term_list( $post_id , 'material' , '' , ', ' , '' );
  349. echo is_string( $terms ) ? $terms : '—';
  350. break;
  351. }
  352. }
  353. // Add Author to Custom Post Type
  354. function add_author_support_to_posts() {
  355. $args = array(
  356. 'public' => true,
  357. '_builtin' => false
  358. );
  359. $output = 'names';
  360. $post_types = get_post_types( $args, $output );
  361. foreach ( $post_types as $post_type ) {
  362. add_post_type_support( $post_type, 'author' );
  363. }
  364. }
  365. add_action( 'init', 'add_author_support_to_posts' );