Skip to content

Instantly share code, notes, and snippets.

View jshwlkr's full-sized avatar
🙈

Joshua Walker jshwlkr

🙈
View GitHub Profile
@jshwlkr
jshwlkr / bookmarklet.js
Created July 17, 2024 02:53
Searches for previous incarnations of the current page on the Wayback Machine. Source: https://lmnt.me/blog/two-bookmarklets.html
javascript:(function()%7Bvar%20url%20%3D%20document.URL%3B%0Avar%20newURL%20%3D%20%60https%3A%2F%2Fweb.archive.org%2Fweb%2F%24%7Burl%7D%60%3B%0A%0Awindow.location.href%20%3D%20newURL%3B%7D)()%3B
@jshwlkr
jshwlkr / bookmarklet.js
Created July 17, 2024 02:51
This opens a new tab and searches the English Wikipedia for the selected text on the current page. If you’ve selected no text, it goes to the Wikipedia home page. Source: https://lmnt.me/blog/two-bookmarklets.html
javascript:(function(){var selectedText=window.getSelection().toString().trim();var searchUrl=selectedText?'https://en.wikipedia.org/wiki/Special:Search?search='+encodeURIComponent(selectedText):'https://en.wikipedia.org/wiki/Main_Page';window.open(searchUrl,'_blank');})();
@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' );
}
}