Skip to content

Instantly share code, notes, and snippets.

Avatar

David Sword davidsword

View GitHub Profile
@davidsword
davidsword / wp-force-subdir-on-upload_dir.php
Last active Sep 20, 2019
WordPress - force a specific /YYYY/MM folder for media uploads.
View wp-force-subdir-on-upload_dir.php
<?php
const FORCE_UPLOAD_DIR_SUBDIR_TO = '/1990/08'; // MUST be "/YYYY/MM" format.
/**
* Force uploads to land in a specific /YYYY/MM folder.
*
* @param array $uploads information about the upload directory
* @see https://github.com/WordPress/WordPress/blob/2b92bcab85cecd819596b79b0b52f44aa4dfaffa/wp-includes/functions.php#L2223-L2239
* @return array
@davidsword
davidsword / wpcli-media-count.php
Last active Oct 2, 2019
WordPress WPCLI - Count Media Library by Types
View wpcli-media-count.php
<?php
/**
* Implements `media-count` command.
*/
class WPCLI_Media_Library_Count {
/**
* Get total count of all media and break down counts for each media type.
*
* ## EXAMPLES
*
@davidsword
davidsword / list-plugins.php
Last active Apr 8, 2019
WordPress - list all plugins in text via shortcode
View list-plugins.php
<?php
/**
* List off all plugins used via `[list_plugins]` shortcode.
*
* Note that this includes inactive plugins, so keep the plugins tidy.
*
* @return string of html, list of plufins and links to their site.
*/
add_shortcode('list_plugins', function(){
@davidsword
davidsword / vs-code-settings.json
Last active Feb 20, 2019
VSCode settings json (20190219)
View vs-code-settings.json
{
// App
"workbench.startupEditor": "newUntitledFile",
"workbench.settings.editor": "json",
"files.defaultLanguage": "php",
"editor.minimap.enabled": false,
"breadcrumbs.enabled": true,
// Explorer
"workbench.quickOpen.closeOnFocusLost": false,
@davidsword
davidsword / php-isset-ternary.php
Last active Mar 6, 2019
PHP7 - New Feature! Null coalescing operator. Quickly do isset check and defines.
View php-isset-ternary.php
<?php
/**
* The new null coalescing operator! 🎉
* @see http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op
*/
$username = $_GET['user'] ?? 'nobody';
// The old way. 🙅
$username = isset( $_GET['user'] ) ? $_GET['user'] : 'nobody';
@davidsword
davidsword / vs-code-ext.txt
Last active Feb 20, 2019
vs code extensions I use, my starter pack (20190219)
View vs-code-ext.txt
code --install-extension aaron-bond.better-comments
code --install-extension agauniyal.vscode-caniuse
code --install-extension ahmadawais.shades-of-purple
code --install-extension alefragnani.Bookmarks
code --install-extension alefragnani.project-manager
code --install-extension Asuka.insertnumbers
code --install-extension bmewburn.vscode-intelephense-client
code --install-extension BriteSnow.vscode-toggle-quotes
code --install-extension christian-kohler.npm-intellisense
code --install-extension claudiosanches.wpcs-whitelist-flags
@davidsword
davidsword / Enable-lazyload-Jetpack-Module.php
Last active Mar 5, 2019
WordPress - Jetpack: Enable lazyload Module via functions.php
View Enable-lazyload-Jetpack-Module.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 Mar 5, 2019
JS - Move a dropdown menu that's overflowing out off viewport back into view
View dropdownmove.js
// 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 Mar 5, 2019
PHP - Fetch and display Google Calendar events as text list and total time
View google-query-calendar.php
<?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 Mar 6, 2019
WordPress - Pass PHP values into JS
View wp-pass-php-into-js-alt.php
<?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,