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

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