Skip to content

Commit 8750d94

Browse files
Detect WordPress theme slugs (#157)
* Detect WordPress theme slugs * Fix class name * default null --------- Co-authored-by: Barry Pollard <[email protected]>
1 parent fa75f28 commit 8750d94

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

dist/cms.js

+33
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,38 @@ 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: null,
17+
child_theme: null,
18+
};
19+
try {
20+
const bodyClass = document.body.classList;
21+
22+
const parentTheme = Array.from( bodyClass ).find( c => c.startsWith( 'wp-theme-' ) );
23+
24+
if ( parentTheme ) {
25+
theme.theme = parentTheme.replace( 'wp-theme-', '' );
26+
theme.child_theme = '';
27+
}
28+
29+
const childTheme = Array.from( bodyClass ).find( c => c.startsWith( 'wp-child-theme-' ) );
30+
31+
if ( childTheme ) {
32+
theme.child_theme = childTheme.replace( 'wp-child-theme-', '' );
33+
}
34+
35+
} catch ( e ) {}
36+
return theme;
37+
}
38+
739
// Detects if a WordPress embed block is on the page
840
function hasWordPressEmbedBlock() {
941
return !!document.querySelector('figure.wp-block-embed');
@@ -195,6 +227,7 @@ function usesInteractivityAPI() {
195227
}
196228

197229
const wordpress = {
230+
theme: getWordPressTheme(),
198231
block_theme: usesBlockTheme(),
199232
has_embed_block: hasWordPressEmbedBlock(),
200233
embed_block_count: getWordPressEmbedBlockCounts(),

0 commit comments

Comments
 (0)