Skip to content

Instantly share code, notes, and snippets.

View kmwalsh's full-sized avatar
🤖

Kate Walsh kmwalsh

🤖
View GitHub Profile
@kmwalsh
kmwalsh / gist:abebb8d8a9d42e41f0ad893267d27008
Last active January 24, 2017 19:48
modify wordpress menu for anchor links in semi-single page site
/**
* modify primary menu so that the anchor links always point back to the homepage
*/
if( ! function_exists('nav_items') ) {
add_filter( 'wp_get_nav_menu_items','nav_items', 11, 3 );
function nav_items( $items, $menu, $args )
{
if( is_admin() )
return $items;
@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 );
}
@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 / 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 / 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 );
<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.
/**
* 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
/**
*
* 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:47f65c2d8790ff914f45cd7866c1859f
Created July 19, 2021 23:04
px to em with rem support
@function em($pixels, $context: 16) {
@if ( comparable($pixels, 1px)) {
@if (unitless($pixels)) {
$pixels: $pixels * 1px;
}
@if (unitless($context)) {
$context: $context * 1px;
}
@return $pixels / $context * 1em;
}