Skip to content

Instantly share code, notes, and snippets.

@davidsword
davidsword / Enable-lazyload-Jetpack-Module.php
Last active March 5, 2019 20:37
WordPress - Jetpack: Enable lazyload Module via functions.php
<?php
/**
* Some Jetpack modules can be manually loaded.
*
* Here's an example of how to enable Lazy Images via code instead of database.
*
* @see https://github.com/Automattic/jetpack/blob/593ee4cccd6aead047c72aa78378f368f084035e/modules/lazy-images.php#L27
*/
if ( defined( 'JETPACK__PLUGIN_DIR' ) ) {
@davidsword
davidsword / dropdownmove.js
Last active March 5, 2019 20:40
JS - Move a dropdown menu that's overflowing out off viewport back into view
// If the sub-navigation is off-screen we want to bump it left.
const parentsli = document.querySelectorAll( 'ul > li.page_item_has_children' );
for ( let parent of parentsli ) {
const children = parent.querySelector( 'ul.children' );
parent.addEventListener( 'mouseenter', evt => {
// Compare with width, to dropdown offset, to width of drop down.
const windowWidth = document.documentElement.clientWidth;
@davidsword
davidsword / google-query-calendar.php
Last active March 5, 2019 21:37
PHP - Fetch and display Google Calendar events as text list and total time
<?php
/**
* Retreive a list events from a Google Calender in PHP.
*
* Useful when using Google Calendar as a project/time log.
* This script can be adapted to peal out events from a specific calendar.
* I use this for my personal timelog'ing. My events follow a naming
* pattern of `{category} - {project}: {details}`.
*
@davidsword
davidsword / wp-pass-php-into-js-alt.php
Last active March 6, 2019 17:08
WordPress - Pass PHP values into JS
<?php
wp_scripts()->add_data(
'my_js_theme',
'my_theme_data',
sprintf(
'var my_theme_data = %s;',
wp_json_encode( [
'nonce' => wp_create_nonce( 'wp_rest' ),
'path' => $path,
@davidsword
davidsword / wp-pass-php-into-js.php
Last active March 6, 2019 15:59
WordPress - Pass PHP values into JS
<?php
add_action( 'wp_enqueue_scripts', function() {
$version = SCRIPT_DEBUG ? time() : null;
wp_enqueue_script(
'_my_js_app',
get_template_directory_uri() . '/_my_js_app.js',
[],
$version,
true // in footer.
@davidsword
davidsword / section-sticky-buttons.css
Last active March 6, 2019 15:53
JS/CSS - Sticky buttons within a section. Demo: https://codepen.io/davidsword/pen/OEZMay
#events {
overflow: hidden;
position: relative;
}
#events .show {
width: 30.3%;
background: white;
float: left;
padding: 150px 0;
margin: 20px 1.5%;
@davidsword
davidsword / wp-image-filters.php
Last active January 15, 2023 22:20
WordPress - All filters to modify an images URL (as far as I could find)
<?php
// Images in post galleries
add_filter( 'get_post_galleries', '_img_url_filter', PHP_INT_MAX );
add_filter( 'widget_media_image_instance', '_img_url_filter', PHP_INT_MAX );
// Core image retrieval
add_filter( 'image_downsize', '_img_url_filter', 10, 3 );
// Responsive image srcset substitution
@davidsword
davidsword / wp-fix-tax-query.php
Last active June 4, 2018 21:50
Wordpress - do tax_query with switch_to_blog() work-around
<?php
global $wp_taxonomies;
switch_to_blog($new);
if(!taxonomy_exists('my_missing_tax'))
$wp_taxonomies['my_missing_tax'] = 'delete';
// do `tax_query`'s in $new with `my_missing_tax`
@davidsword
davidsword / alfred--change-local-dev.php
Last active March 12, 2019 18:09
PHP - part of an Alfred Workflow that changes a development url to production
<?php // don't actually use the php in workflow
// get the result of Apple Script action
$url = '{query}';
$a = ['http:','.loc']; // dev
$b = ['https:','.com']; // production
// identify if production and invert if so
if (strstr($url,$b[1]))
@davidsword
davidsword / get_browser_url.as
Created May 31, 2018 17:59
Apple Script - get the current browser URL
tell application "System Events"
set myApp to name of first application process whose frontmost is true
if myApp is "Google Chrome" then
tell application "Google Chrome" to return URL of active tab of front window
else if myApp is "Opera" then
tell application "Opera" to return URL of front document
else if myApp is "Safari" then
tell application "Safari" to return URL of front document
else if myApp is "Firefox" then
tell application "System Events"