Skip to content

Instantly share code, notes, and snippets.

@diggeddy
diggeddy / accordion-faq-schema.php
Last active March 9, 2023 12:06
Create FAQ json-ld schema from GB Pro Accordion block
<?php
function create_faq_schema($content) {
// get the bloocks
global $post;
$blocks = parse_blocks( $post->post_content );
// create arrays to store our questions
$faq = array();
$questions = array();
@diggeddy
diggeddy / toggle-vanilla.js
Created February 14, 2023 12:36
vanilla JS simple toggle
const elements = document.querySelectorAll('.className');
elements.forEach(element => {
element.addEventListener('click', () => {
elements.forEach(sibling => sibling.classList.remove('active'));
element.classList.toggle('active');
});
});
@diggeddy
diggeddy / menu-image-subtitle.php
Last active February 13, 2023 15:46
Menu image and subtitle
<?php
// simple function to insert post meta
// subtitle and menu_image
// in to sub menu items of primary navigation
function db_menu_insert_post_meta($sorted_menu_objects, $args) {
// select the menu
if ($args->theme_location === 'primary'){
// get the menu objects
foreach ($sorted_menu_objects as $menu_object) {
@diggeddy
diggeddy / heirarchical-terms.php
Last active February 3, 2023 09:27
display terms that match query heirarchy
<?php
// terms that match the tax archives heierarchy
// show only parent terms in parent archives
// show only child terms in child archives
function custom_get_terms() {
// set the taxonomy
$tax_name = 'category';
// is archive parent or child logic
$current_term = get_queried_object();
@diggeddy
diggeddy / gp-element-class.php
Created January 30, 2023 13:00
Add GeneratePress Element Class to body tag
<?php
add_filter( 'body_class', function( $classes ) {
global $generate_elements;
foreach ( $generate_elements as $element => $data ) {
$element_title = 'gp-elem-' . strtolower(str_replace(' ', '-', get_the_title( $data['id'] ) ));
$classes[] = $element_title;
}
return $classes;
});
@diggeddy
diggeddy / parent-menu-item.php
Last active January 17, 2023 10:13
set current parent menui item
<?php
add_filter( 'wp_nav_menu_objects', 'add_menu_ancestor_parent_class' );
function add_menu_ancestor_parent_class( $items ) {
// ancestor classes to find
$ancestor = array('current-page-ancestor', 'current-post-ancestor');
$parents = array();
foreach ( $items as $item ) {
if ( array_intersect( $ancestor , $item->classes) ) {
@diggeddy
diggeddy / get_related_tags.php
Created December 21, 2022 09:35
Get related tags for category archives
@diggeddy
diggeddy / gp-mega-menu.php
Last active February 15, 2024 17:09
Create a sub menu item container with action hook
<?php
add_filter( 'walker_nav_menu_start_el', 'db_sub_menu_item_hook', 10, 4 );
function db_sub_menu_item_hook( $item_output, $item, $depth, $args ) {
// Specify menu item class to target
$class_string = 'gp_mega_item';
$menu_item_classes = $item->classes;
if ( empty( $menu_item_classes ) || !in_array( $class_string , $menu_item_classes ) ) {
return $item_output;
@diggeddy
diggeddy / add_widget_title_tag.php
Created December 12, 2022 11:39
Add Title to widget aside for better A11y
<?php
function db_add_widget_title_attr($params) {
// get the widget ID
$id = $params[0]['id'];
// get the widget name
$name = isset($params[0]['widget_name']) ? $params[0]['widget_name'] : "";
// create a new aside html string with concatenated name + id title attribute
$title = '<aside title="' . $name . ' ' . $id . '"';
// string replace the before widget opening tag
$params[0]['before_widget'] = str_replace( '<aside' , $title , $params[0]['before_widget'] );
@diggeddy
diggeddy / muteObserverStyle.js
Last active November 19, 2022 10:10
mutationObserverAPI looking for style changes
// simple mutation observer
// listen to group of elements with target-class
// for style change and toggle class on root HTML
var modals = document.querySelectorAll('.target-class');
var rootHTML = document.getElementsByTagName( 'html' )[0];
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutationRecord) {
// do something on observed style change