Skip to content

Instantly share code, notes, and snippets.

View jshwlkr's full-sized avatar
🙈

Joshua Walker jshwlkr

🙈
View GitHub Profile
@jshwlkr
jshwlkr / functions.php
Created August 30, 2022 02:45
add customizer link back to admin
add_action( 'customize_register', function ( $manager ) { }, 10, 1 );
@jshwlkr
jshwlkr / posts.11tydata.js
Last active August 14, 2022 22:21
Preventing drafts from being published in Eleventy.
module.exports = {
eleventyComputed: {
tags: data => {
if(!data.draft) return 'posts';
return '';
}
}
}
@jshwlkr
jshwlkr / functions.php
Created August 10, 2022 14:58
Display a different favicon for different environment types, while respecting the site icon setting.
function prefix_site_icon() {
// I'm assuming separate files for separate environments.
// You could adapt this to one svg icon with different css varaibles controlling the color per environemnt.
if ( ! has_site_icon() ) {
// this is your default/production favicon
$icon = '<link rel="apple-touch-icon" sizes="180x180" href="">';
$icon .= "\n" . '<link rel="icon" type="image/png" sizes="32x32" href="">';
@jshwlkr
jshwlkr / functions.php
Created July 11, 2022 21:10
Add the featured image (and title) to an RSS feed. Note that this does not touch the atom feed. Additionally this snippet passes analysis by PHPStan.
add_action( 'rss2_item', 'prefix_add_rss_image' );
function prefix_add_rss_image() {
global $post;
if ( is_feed() && has_post_thumbnail( $post->ID ) ) {
$image_url = get_the_post_thumbnail_url( $post->ID );
if ( $image_url ) {
$image_id = get_post_thumbnail_id( $post->ID );
if ( $image_id ) {
$image_title = get_the_title( $image_id );
@jshwlkr
jshwlkr / functions.php
Created June 29, 2022 14:54
Redirect WordPress' native search to an external engine, like Google's Programmable Search
function redirect_search() {
if (is_search() && !empty($_GET['s'])) {
wp_redirect('https://cse.google.com/cse?cx=PSE_ID&q=' . urlencode(get_query_var('s')) );
exit();
}
}
add_action('template_redirect', 'redirect_search' );
(?d).DS_Store
(?i).git*
@jshwlkr
jshwlkr / functions.php
Last active April 7, 2022 19:06
Append a random string to WordPress static assets, to bust a cache, if you are not on a production server.
function prefix_enqueued_assets() {
$version_string = NULL;
if ( 'production' !== wp_get_environment_type() ) {
$version_string = bin2hex(random_bytes(4));
}
wp_register_style(
'prefix-theme-css',
@jshwlkr
jshwlkr / functions.php
Created February 15, 2022 21:03
Alter your WordPress page meta (like changing the html element) based on post template
function prefix_template_meta() {
if (get_page_template_slug() == 'page template slug') {
// This is an arbitrary operation, in this case. You could do anything here.
// Beware making assumptions about the availability of things, like the post ID, if you change the action hook.
add_filter( 'language_attributes', 'prefix_xml_namespaces' );
}
}
@jshwlkr
jshwlkr / functions.php
Created February 15, 2022 20:42
Add additional XML namespaces to a WordPress html element
add_filter( 'language_attributes', 'prefix_xml_namespaces' );
function prefix_xml_namespaces($output) {
return $output . ' xmlns="http://www.w3.org/1999/xhtml" etc';
}
@jshwlkr
jshwlkr / functions.php
Last active February 9, 2022 15:45
Scan your WordPress content for (anything) a URL and then insert some prefetch hint etc into the head element.
function prefix_filter_embeds( $content ) {
if strpos( $content, 'string' ) !== false ) {
add_action( 'wp_head', 'prefix_url_actions', 0 );
}
return $content;
}
add_filter( 'the_content', 'prefix_filter_embeds' );
function prefix_url_actions() {