Skip to content

Instantly share code, notes, and snippets.

@stefanmm
Created August 8, 2023 23:12
Show Gist options
  • Save stefanmm/02c8895185d48e1f3f402934cf87223f to your computer and use it in GitHub Desktop.
Save stefanmm/02c8895185d48e1f3f402934cf87223f to your computer and use it in GitHub Desktop.
WP 6.3 - Open "Details" block automatically on page load based on URL hash
// First, set a class name inside the "Details" accordion block (because we do not have ability to set the ID)
// If multiple elements with the same class name are detected, the first one will be triggered
// Append that class name after the page URL, like so: site.domain/page/#classname
if(window.location.hash) {
var hash = window.location.hash;
var el = document.getElementsByClassName(hash.slice(1))[0];
if(!el.hasAttribute('open')){
el.setAttribute('open', '');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment