Skip to content

Commit 24e7111

Browse files
committed
Detect WordPress theme slugs
1 parent e080feb commit 24e7111

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

dist/cms.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,39 @@ function usesBlockTheme() {
44
return !!document.querySelector('div.wp-site-blocks');
55
}
66

7+
/**
8+
* Detects the WordPress parent and child theme slugs.
9+
*
10+
* See https://core.trac.wordpress.org/changeset/59698.
11+
*
12+
* @returns {object} Object with fields `theme` and `parent_theme`.
13+
*/
14+
function getWordPressTheme() {
15+
const theme = {
16+
theme: 'unknown',
17+
parent_theme: '',
18+
};
19+
try {
20+
const bodyClass = document.body.classList;
21+
22+
const parentTheme = Array.from( bodyClass ).find( c => c.startsWith( 'wp-theme-' ) );
23+
const childTheme = Array.from( bodyClass ).find( c => c.startsWith( 'wp-child-theme-' ) );
24+
25+
if ( childTheme ) {
26+
theme.theme = childTheme;
27+
28+
// If there is a child theme, there should always be a parent theme.
29+
if ( parentTheme ) {
30+
theme.parent_theme = parentTheme;
31+
}
32+
} else if ( parentTheme ) {
33+
// There is no child theme, only a parent theme.
34+
theme.theme = parentTheme;
35+
}
36+
} catch ( e ) {}
37+
return theme;
38+
}
39+
740
// Detects if a WordPress embed block is on the page
841
function hasWordPressEmbedBlock() {
942
return !!document.querySelector('figure.wp-block-embed');
@@ -195,6 +228,7 @@ function usesInteractivityAPI() {
195228
}
196229

197230
const wordpress = {
231+
theme: getWordPressTheme(),
198232
block_theme: usesBlockTheme(),
199233
has_embed_block: hasWordPressEmbedBlock(),
200234
embed_block_count: getWordPressEmbedBlockCounts(),

0 commit comments

Comments
 (0)