NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

functions.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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', 'my-text-domain' );
  194. $columns['material'] = __('Material', 'my-text-xx' );
  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['date'] = $date;
  221. return $columns;
  222. }
  223. // Add the data to the custom columns for EVENT:
  224. add_action( 'manage_event_posts_custom_column' , 'custom_event_column', 10, 2 );
  225. function custom_event_column( $column, $post_id ) {
  226. switch ( $column ) {
  227. case 'event_type' :
  228. $terms = get_the_term_list( $post_id , 'event_type' , '' , ', ' , '' );
  229. echo is_string( $terms ) ? $terms : '—';
  230. break;
  231. }
  232. }
  233. // Add the custom columns for SHORT:
  234. add_filter( 'manage_short_posts_columns', 'set_custom_edit_short_columns' );
  235. function set_custom_edit_short_columns($columns) {
  236. $date = $columns['date'];
  237. unset( $columns['date'] );
  238. $columns['material'] = __('Material');
  239. $columns['date'] = $date;
  240. return $columns;
  241. }
  242. // Add the data to the custom columns for SHORT:
  243. add_action( 'manage_short_posts_custom_column' , 'custom_short_column', 10, 2 );
  244. function custom_short_column( $column, $post_id ) {
  245. switch ( $column ) {
  246. case 'material' :
  247. $terms = get_the_term_list( $post_id , 'material' , '' , ', ' , '' );
  248. echo is_string( $terms ) ? $terms : '—';
  249. break;
  250. }
  251. }
  252. // Add the custom columns for TECHNIQUE:
  253. add_filter( 'manage_technique_posts_columns', 'set_custom_edit_technique_columns' );
  254. function set_custom_edit_technique_columns($columns) {
  255. $date = $columns['date'];
  256. unset( $columns['date'] );
  257. $columns['material'] = __('Material');
  258. $columns['date'] = $date;
  259. return $columns;
  260. }
  261. // Add the data to the custom columns for TECHNIQUE:
  262. add_action( 'manage_technique_posts_custom_column' , 'custom_technique_column', 10, 2 );
  263. function custom_technique_column( $column, $post_id ) {
  264. switch ( $column ) {
  265. case 'material' :
  266. $terms = get_the_term_list( $post_id , 'material' , '' , ', ' , '' );
  267. echo is_string( $terms ) ? $terms : '—';
  268. break;
  269. }
  270. }
  271. // Add the custom columns for GUIDE:
  272. add_filter( 'manage_guide_posts_columns', 'set_custom_edit_guide_columns' );
  273. function set_custom_edit_guide_columns($columns) {
  274. $date = $columns['date'];
  275. unset( $columns['date'] );
  276. $columns['material'] = __('Material');
  277. $columns['date'] = $date;
  278. return $columns;
  279. }
  280. // Add the data to the custom columns for GUIDE:
  281. add_action( 'manage_guide_posts_custom_column' , 'custom_guide_column', 10, 2 );
  282. function custom_guide_column( $column, $post_id ) {
  283. switch ( $column ) {
  284. case 'material' :
  285. $terms = get_the_term_list( $post_id , 'material' , '' , ', ' , '' );
  286. echo is_string( $terms ) ? $terms : '—';
  287. break;
  288. }
  289. }
  290. // Add the custom columns for OBJECT:
  291. add_filter( 'manage_object_posts_columns', 'set_custom_edit_object_columns' );
  292. function set_custom_edit_object_columns($columns) {
  293. $date = $columns['date'];
  294. unset( $columns['date'] );
  295. $columns['material'] = __('Material');
  296. $columns['date'] = $date;
  297. return $columns;
  298. }
  299. // Add the data to the custom columns for OBJECT:
  300. add_action( 'manage_object_posts_custom_column' , 'custom_object_column', 10, 2 );
  301. function custom_object_column( $column, $post_id ) {
  302. switch ( $column ) {
  303. case 'material' :
  304. $terms = get_the_term_list( $post_id , 'material' , '' , ', ' , '' );
  305. echo is_string( $terms ) ? $terms : '—';
  306. break;
  307. }
  308. }
  309. // Add the custom columns for PUBLICATION:
  310. add_filter( 'manage_publication_posts_columns', 'set_custom_edit_publication_columns' );
  311. function set_custom_edit_publication_columns($columns) {
  312. $date = $columns['date'];
  313. unset( $columns['date'] );
  314. $columns['material'] = __('Material');
  315. $columns['date'] = $date;
  316. return $columns;
  317. }
  318. // Add the data to the custom columns for PUBLICATION:
  319. add_action( 'manage_guide_posts_custom_column' , 'custom_publication_column', 10, 2 );
  320. function custom_publication_column( $column, $post_id ) {
  321. switch ( $column ) {
  322. case 'material' :
  323. $terms = get_the_term_list( $post_id , 'material' , '' , ', ' , '' );
  324. echo is_string( $terms ) ? $terms : '—';
  325. break;
  326. }
  327. }