Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect WordPress theme slugs #157

Merged
merged 4 commits into from
Feb 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions dist/cms.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,38 @@ function usesBlockTheme() {
return !!document.querySelector('div.wp-site-blocks');
}

/**
* Detects the WordPress parent and child theme slugs.
*
* See https://core.trac.wordpress.org/changeset/59698.
*
* @returns {object} Object with fields `theme` and `parent_theme`.
*/
function getWordPressTheme() {
const theme = {
theme: null,
child_theme: null,
};
try {
const bodyClass = document.body.classList;

const parentTheme = Array.from( bodyClass ).find( c => c.startsWith( 'wp-theme-' ) );

if ( parentTheme ) {
theme.theme = parentTheme.replace( 'wp-theme-', '' );
theme.child_theme = '';
}

const childTheme = Array.from( bodyClass ).find( c => c.startsWith( 'wp-child-theme-' ) );

if ( childTheme ) {
theme.child_theme = childTheme.replace( 'wp-child-theme-', '' );
}

} catch ( e ) {}
return theme;
}

// Detects if a WordPress embed block is on the page
function hasWordPressEmbedBlock() {
return !!document.querySelector('figure.wp-block-embed');
Expand Down Expand Up @@ -195,6 +227,7 @@ function usesInteractivityAPI() {
}

const wordpress = {
theme: getWordPressTheme(),
block_theme: usesBlockTheme(),
has_embed_block: hasWordPressEmbedBlock(),
embed_block_count: getWordPressEmbedBlockCounts(),
Expand Down