Skip to content

Instantly share code, notes, and snippets.

View kmwalsh's full-sized avatar
🤖

Kate Walsh kmwalsh

🤖
View GitHub Profile
// fix for keyboard focus states on scroll position or timed based popup implementations
//
// forces the focus back to the element that was focused prior to popup open
// fixes focus bug when faking a click on existing element to bring up a modal window
// fixes accessibility issue where the user's focus was changed when the popup shows
//
/**
* Gets keyboard-focusable elements within a specified element
* @param {HTMLElement} [element=document] element
/**
*
* Prerequisites:
* (1) All of your Gutenberg block Sass is in its own folder, or somewhere separated from the rest of your Sass. `components/blocks/*` is the location of the Sass in this example.
* (2) Your variables, mixins, functions, etc. are separated from the rest of your sass. Mixin/variables/function files DO NOT CONTAIN any styling whatsoever -- just definitions.
* (3) Do not use the class wp-block anywhere in your custom Gutenblock templates.
*
* Steps:
* 1. Create a new file, editor-style.scss, alongside your style.scss -- do not use an underscore to start filename as this is not a partial
* 2. Import your block Sass, your mixins, variables, and functions, as well as any external libraries, in patterns shown below
/**
* Redirect single of CPT to page
*/
if ( ! function_exists('kw_redirect_resource') ) {
add_action( 'template_redirect', 'kw_redirect_resource' );
function shnj_redirect_resource() {
if ( is_singular( 'CUSTOM_POST_TYPE_NAME' ) ) :
//get the url by path
<h1>H1 heading Medium length headline<h1>
<h2>H2 heading Medium length headline</h2>
<h3>H3 heading Medium length headline</h3>
<h3 class="heading--alternative">H3 heading Medium length headline</h3>
<h4>H4 heading Medium length headline</h4>
<h5>H5 heading Medium length headline</h5>
<h6>H6 heading Medium length headline</h6>
<a href="#">Hello i am a link</a> Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consectetur sapien ut sem auctor, id suscipit orci auctor. Nullam nec consequat magna. Aliquam vulputate condimentum sem vitae imperdiet. Duis dignissim ligula quis ligula aliquam, in dapibus sapien auctor.
/**
*
* All internal links should open in a new window with rel=noopener
*
*/
if ( ! function_exists( 'force_external_links' ) ) {
add_filter('the_content', 'force_external_links');
add_filter('acf_the_content', 'force_external_links');
@kmwalsh
kmwalsh / gist:aeaf0b2115660154affcc146fcb8e319
Created September 9, 2020 19:55 — forked from gyrus/gist:3157198
Check if a WordPress page is "current"
<?php
/**
* Check if a WordPress page is "current", i.e. it's the current page or the ancestor of a current page
*
* @uses is_search()
* @uses get_post_type()
* @uses site_url()
* @uses get_permalink()
* @uses get_post_type_object()
/**
* Get accessible post thumbnail images - fills alt attribute with post title if alt attribute is empty
*
* @param [type] $id The post ID being called.
* @param [type] $size The size of the image to call. Default thumbnail.
*
* @return void
*/
function a11y_images_with_alt( $id, $size = 'thumbnail' ) {
$alt = get_post_meta( get_post_thumbnail_id( $id ), '_wp_attachment_image_alt', true );
@kmwalsh
kmwalsh / migrate-acf-sel-to-tax.php
Last active December 7, 2018 05:00
Migrate ACF select values into taxonomy, then apply taxonomy terms to all posts
/**
* Ran into an issue with a WP site where I needed to change an ACF select dropdown into a real WordPress taxonomy. Wrote this to move the data from an ACF select to taxonomy. This will create terms from an ACF select (manually, have to get the data from the ACF field editing interface). Then it will go through all posts and apply the new taxonomy term according to the ACF select value that already exists on the post.
*
* ⚠⚠⚠ WARNING ⚠⚠⚠
*
* This is not a professional migration thing. If you use this:
* Put into `functions.php` or as your own `mu-plugin` file.
* Run on a local installation first. If you can run locally and export your DB back to live, even better.
* No idea how it'd perform if you had thousands of ACF fields. It worked OK for me with 250 potential ACF select values.
@kmwalsh
kmwalsh / wordpress--breadcrumb-navxt--search-and-filter
Created November 16, 2018 07:14
Manually fix Breadcrumb NavXT breadcrumbs for pages on Search & Filter pages
/**
* Fix Breadcrumb NavXT breadcrumbs for pages on Search & Filter pages
*/
add_action('bcn_after_fill', 'kmw_navxt_filter_breadcrumb_fix');
function kmw_navxt_filter_breadcrumb_fix($trail)
{
$query_var = get_query_var( 'sfid' );
if( !empty( $query_var ) ) {
array_pop($trail->trail);
array_pop($trail->trail);
@kmwalsh
kmwalsh / gist:92d4d681c822a855ef2caa059ce3e117
Created May 22, 2018 23:52
Enqueuing WordPress scripts - all pages, just posts, just certain templates
function 8ii010b_scripts() {
// this adds your script to all pages on the site
wp_enqueue_script( 'custom-8ii010-js', get_template_directory_uri() . '/js/YOUR-JS-FILE.js', array(), '20151215', true );
// this adds your script to all single posts
if ( is_singular('post') ) {
wp_enqueue_script( 'custom-8ii010-js', get_template_directory_uri() . '/js/YOUR-JS-FILE.js', array(), '20151215', true );
}