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 1 commit
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
34 changes: 34 additions & 0 deletions dist/cms.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,39 @@ 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: 'unknown',
parent_theme: '',
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
};
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
try {
const bodyClass = document.body.classList;

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

if ( childTheme ) {
theme.theme = childTheme;

// If there is a child theme, there should always be a parent theme.
if ( parentTheme ) {
theme.parent_theme = parentTheme;
}
} else if ( parentTheme ) {
// There is no child theme, only a parent theme.
theme.theme = parentTheme;
}
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
} 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 +228,7 @@ function usesInteractivityAPI() {
}

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