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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

swissspidy
Copy link

There is a new change in the upcoming WordPress 6.8 release that exposes the slugs of the currently active theme(s) via a class on the <body>.

This makes it easy to track individual theme adoption.


Test websites:

Copy link
Contributor

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@swissspidy Thanks for the PR! Looks good overall, though I think tracking this slightly differently would be more efficient for queries.

dist/cms.js Outdated Show resolved Hide resolved
dist/cms.js Outdated Show resolved Hide resolved
dist/cms.js Outdated Show resolved Hide resolved
@swissspidy swissspidy requested a review from felixarntz January 27, 2025 20:28
dist/cms.js Outdated
Comment on lines 16 to 17
theme: '',
child_theme: '',
Copy link
Contributor

@felixarntz felixarntz Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, though related to #157 (comment) it might still be good to differentiate between "unknown" and "none". I'd say if wp-theme-... is present and wp-child-theme-... is not present, we should set child_theme to '' (none).

But if neither of the two are present, we for sure don't know (because every site must have at least a theme). So then using another value for both (e.g. null) to indicate "unknown" would be good.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So null if both are absent? Works for me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also theme: null if for some reason a wp-child-theme-... class is present, but wp-theme-... isn't. That would be invalid and is probably an edge-case, so might also fine to ignore it.

Copy link
Contributor

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@swissspidy LGTM!

Copy link

https://almanac.httparchive.org/en/2022/

WPT result details

Changed custom metrics values:

{
  "_cms": {
    "wordpress": {
      "theme": {
        "theme": null,
        "child_theme": null
      },
      "block_theme": false,
      "has_embed_block": false,
      "embed_block_count": {
        "total": 0,
        "total_by_type": []
      },
      "scripts": [],
      "content_type": {
        "template": "unknown",
        "post_type": "",
        "taxonomy": ""
      },
      "uses_interactivity_api": false,
      "interactivity_api_usage": {
        "total_regions": 0,
        "total_regions_by_namespace": []
      }
    }
  }
}
https://ma.tt

WPT result details

Changed custom metrics values:

{
  "_cms": {
    "wordpress": {
      "theme": {
        "theme": "twentythirteen",
        "child_theme": "matt"
      },
      "block_theme": false,
      "has_embed_block": true,
      "embed_block_count": {
        "total": 7,
        "total_by_type": {
          "soundcloud": 1,
          "spotify": 1,
          "youtube": 5
        }
      },
      "scripts": [
        {
          "handle": "the-neverending-homepage",
          "src": "https://ma.tt/blog-content/plugins/jetpack/_inc/build/infinite-scroll/infinity.min.js?ver=14.2.1-is5.0.1",
          "in_footer": true,
          "async": false,
          "defer": false,
          "intended_strategy": null,
          "after_script_size": null,
          "before_script_size": null,
          "extra_script_size": null,
          "translations_script_size": null
        },
        {
          "handle": "jp-lite-youtube-embed-script",
          "src": "https://ma.tt/blog-content/mu-plugins/jp-lite-youtube-embed/lite-yt-embed.min.js?ver=0.3.2-5",
          "in_footer": true,
          "async": false,
          "defer": true,
          "intended_strategy": "defer",
          "after_script_size": null,
          "before_script_size": null,
          "extra_script_size": null,
          "translations_script_size": null
        },
        {
          "handle": "wp-polyfill",
          "src": "https://ma.tt/blog/wp-includes/js/dist/vendor/wp-polyfill.min.js?ver=3.15.0",
          "in_footer": true,
          "async": false,
          "defer": false,
          "intended_strategy": null,
          "after_script_size": null,
          "before_script_size": null,
          "extra_script_size": null,
          "translations_script_size": null
        },
        {
          "handle": "wp-hooks",
          "src": "https://ma.tt/blog-content/plugins/gutenberg/build/hooks/index.min.js?ver=84e753e2b66eb7028d38",
          "in_footer": true,
          "async": false,
          "defer": false,
          "intended_strategy": null,
          "after_script_size": null,
          "before_script_size": null,
          "extra_script_size": null,
          "translations_script_size": null
        },
        {
          "handle": "wp-i18n",
          "src": "https://ma.tt/blog-content/plugins/gutenberg/build/i18n/index.min.js?ver=bd5a2533e717a1043151",
          "in_footer": true,
          "async": false,
          "defer": false,
          "intended_strategy": null,
          "after_script_size": 68,
          "before_script_size": null,
          "extra_script_size": null,
          "translations_script_size": null
        },
        {
          "handle": "wp-jp-i18n-loader",
          "src": "https://ma.tt/blog-content/plugins/jetpack-boost/jetpack_vendor/automattic/jetpack-assets/build/i18n-loader.js?minify=true&ver=becd7d9884bc1b331e45",
          "in_footer": true,
          "async": false,
          "defer": false,
          "intended_strategy": null,
          "after_script_size": 4097,
          "before_script_size": null,
          "extra_script_size": null,
          "translations_script_size": null
        },
        {
          "handle": "wp-url",
          "src": "https://ma.tt/blog-content/plugins/gutenberg/build/url/index.min.js?ver=499ac283dc628dfb623e",
          "in_footer": true,
          "async": false,
          "defer": false,
          "intended_strategy": null,
          "after_script_size": null,
          "before_script_size": null,
          "extra_script_size": null,
          "translations_script_size": null
        },
        {
          "handle": "jetpack-instant-search",
          "src": "https://ma.tt/blog-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-search/build/instant-search/jp-search.js?minify=false&ver=da5ecd9f722e7fcb409e",
          "in_footer": true,
          "async": false,
          "defer": false,
          "intended_strategy": null,
          "after_script_size": null,
          "before_script_size": 1489,
          "extra_script_size": null,
          "translations_script_size": null
        },
        {
          "handle": "jp-tracks",
          "src": "https://stats.wp.com/w.js?ver=202505",
          "in_footer": true,
          "async": false,
          "defer": false,
          "intended_strategy": null,
          "after_script_size": null,
          "before_script_size": null,
          "extra_script_size": null,
          "translations_script_size": null
        },
        {
          "handle": "wp-parsely-loader",
          "src": "https://ma.tt/blog-content/plugins/wp-parsely/build/loader.js?ver=b681bb9905652ac12735",
          "in_footer": true,
          "async": false,
          "defer": false,
          "intended_strategy": null,
          "after_script_size": null,
          "before_script_size": null,
          "extra_script_size": null,
          "translations_script_size": null
        },
        {
          "handle": "matt-script",
          "src": "https://ma.tt/blog-content/themes/ma.tt/js/matt.js?ver=1719254596",
          "in_footer": true,
          "async": false,
          "defer": true,
          "intended_strategy": "defer",
          "after_script_size": null,
          "before_script_size": null,
          "extra_script_size": null,
          "translations_script_size": null
        },
        {
          "handle": "jetpack-carousel",
          "src": "https://ma.tt/blog-content/plugins/jetpack/_inc/build/carousel/jetpack-carousel.min.js?ver=14.2.1",
          "in_footer": true,
          "async": false,
          "defer": false,
          "intended_strategy": null,
          "after_script_size": null,
          "before_script_size": null,
          "extra_script_size": 1524,
          "translations_script_size": null
        },
        {
          "handle": "gravatar-enhanced-hovercards",
          "src": "https://ma.tt/blog-content/plugins/gravatar-enhanced/build/hovercards.js?ver=379f2afa46f37169f7a3",
          "in_footer": true,
          "async": false,
          "defer": false,
          "intended_strategy": null,
          "after_script_size": null,
          "before_script_size": null,
          "extra_script_size": null,
          "translations_script_size": null
        },
        {
          "handle": "matt-base-theme-functions",
          "src": "https://ma.tt/blog-content/themes/ma.tt/js/base-functions.js?ver=6.8-alpha-59711",
          "in_footer": true,
          "async": false,
          "defer": true,
          "intended_strategy": "defer",
          "after_script_size": null,
          "before_script_size": null,
          "extra_script_size": null,
          "translations_script_size": null
        },
        {
          "handle": "tiled-gallery",
          "src": "https://ma.tt/blog-content/plugins/jetpack/_inc/build/tiled-gallery/tiled-gallery/tiled-gallery.min.js?ver=14.2.1",
          "in_footer": true,
          "async": false,
          "defer": true,
          "intended_strategy": "defer",
          "after_script_size": null,
          "before_script_size": null,
          "extra_script_size": null,
          "translations_script_size": null
        },
        {
          "handle": "jetpack-stats",
          "src": "https://stats.wp.com/e-202505.js",
          "in_footer": true,
          "async": false,
          "defer": false,
          "intended_strategy": "defer",
          "after_script_size": 219,
          "before_script_size": null,
          "extra_script_size": null,
          "translations_script_size": null
        }
      ],
      "content_type": {
        "template": "home-blog",
        "post_type": "post",
        "taxonomy": ""
      },
      "uses_interactivity_api": false,
      "interactivity_api_usage": {
        "total_regions": 0,
        "total_regions_by_namespace": []
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants