-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
80 lines (69 loc) · 2.92 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* Theme entry point
*/
if (!defined('ABSPATH')) exit;
require get_template_directory() . '/inc/setup.php'; // Theme basic setup
require get_template_directory() . '/inc/vite-assets.php'; // vite-related functions
/**
* Function returns a list of entrypoints that need to be included on the current page
*
* @return string[]
*/
function theme_get_entry_points_for_current_page(): array {
// files will be included in the same order in which they were added
$entry_points = array( 'src/js/main-entrypoint.js' );
$entry_points[] = 'src/scss/style-only-entrypoint.scss'; // css-only entrypoint
// files could be added conditionally
if (is_front_page()) $entry_points[] = 'src/js/frontpage-entrypoint.js';
if (is_page_template('page-templates/page-1.php')) $entry_points[] = 'src/js/page-1-entrypoint.js';
if (is_page_template('page-templates/page-2.php')) $entry_points[] = 'src/js/page-2-entrypoint.js';
return $entry_points;
}
// example of using a function with a different name to return entry points
/*
add_filter(
'theme_assets_entry_points_function',
function () {
return 'your_function_name';
}
);
*/
// (Optional) Example of passing data to js (one 'phpData' object for all the scripts and pages, but data could be
// added conditionally)
function theme_output_js_data() {
$data = [
'ajax_url' => admin_url('admin-ajax.php'),
];
?>
<script type="text/javascript">
const phpData = <?= wp_json_encode($data) ?>;
</script>
<?php
}
add_action('wp_head', 'theme_output_js_data', 5);
/**
* (Optional) Load main compiled css files in the gutenberg editor. 'current_screen' is used to avoid downloading files
* (and displaying errors) on all pages, as would be the case with 'after_setup_theme'
*
* @param $screen WP_Screen
*/
function theme_add_editor_styles(WP_Screen $screen) {
if ($screen->base !== 'post') return; // 'post_type' is not checked, assuming all CPTs could have gutenberg
$main_entry = 'src/js/main-entrypoint.js';
try {
$frontend_config = theme_get_frontend_config(); // shared variables between js and php
$manifest = theme_get_vite_manifest_data($frontend_config['distFolder']);// vite manifest
$css_files = theme_get_styles_for_entry($main_entry, $manifest);
if (pathinfo($manifest[ $main_entry ]['file'], PATHINFO_EXTENSION) === 'css') {
$css_files[] = $manifest[ $main_entry ]['file']; // add if your entry is css-only
}
foreach ($css_files as $css_file) {
add_editor_style("{$frontend_config['distFolder']}/$css_file"); // path relative to the theme!
}
} catch (Exception $e) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions -- intentional trigger_error for admin area
trigger_error($e->getMessage(), E_USER_WARNING);// don't break the entire admin page
}
}
add_action('current_screen', 'theme_add_editor_styles');