Skip to content

Instantly share code, notes, and snippets.

@davidsword
davidsword / wpcli-media-count.php
Last active October 2, 2019 19:47
WordPress WPCLI - Count Media Library by Types
<?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 / wp-force-subdir-on-upload_dir.php
Last active September 20, 2019 04:18
WordPress - force a specific /YYYY/MM folder for media uploads.
<?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 / list-plugins.php
Last active April 8, 2019 17:16
WordPress - list all plugins in text via shortcode
<?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 / WP-async-enqued.php
Last active March 18, 2019 16:19
WordPress - Make `wp_enqueue`'d scripts async.
<?php
/**
* Set all enqueued scripts except jQuery to include the `async` attribute.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#Attributes
*/
add_filter('script_loader_tag', function ($tag, $handle) {
if ( 'jquery' !== $handle )
return $tag;
@davidsword
davidsword / WP-disable-widgets.php
Last active March 18, 2019 16:17
WordPress - Disable default widgets
<?php
/**
* Disable default Widgets
*
* @see https://digwp.com/2014/02/disable-default-dashboard-widgets/
*/
add_action('wp_dashboard_setup', function () {
global $wp_meta_boxes;
@davidsword
davidsword / sword-toolkit.php
Last active March 18, 2019 16:16
WordPress - https://github.com/davidsword/sword-toolkit plugin sample configuration
<?php
/**
* Sword Toolkit configuration
*
* @see https://github.com/davidsword/sword-toolkit
*/
add_filter('sword_toolkit', function() {
return [
'remove_blog' => true,
@davidsword
davidsword / remote-ftp-status.less
Last active March 18, 2019 16:14
Atom - better visual que to show if Atoms's RemoteFTP plugin is connected
.status-bar-left .ftp-statusbar-view {
.icon {
color: white !important;
position: relative;
z-index: 2;
&:after {
content: "";
display: block;
position: absolute;
width: 30px;
@davidsword
davidsword / template-include.php
Last active March 15, 2019 15:30
WordPress - template include file for theme from a location. Allows things like custom post types single-posttype.php to be in /myplugin/.
<?php
/**
* Use a plugins template file, unless one exists in theme.
*
* Useful when creating custom post types, allowing themes to create
* their own template.
*/
add_filter('template_include', function ( $template ) {
if ( is_singular( 'location' ) ) { // `location` is a custom post type
@davidsword
davidsword / wordpress-convert-post-to-cpt.php
Last active March 13, 2019 15:01
WordPress convert post post_type to cpt based on post category
<?php
/**
* Implements `dsca` command.
*/
class DSCA_Helpers {
/**
* Change a custom post type.
*
#!/bin/bash
S3DIR="s3://BUCKET_NAME"
BACKUPPATH=/tmp/backups
SITESTORE=/var/www
SITE=public_html
DATEFORM=$(date +"%Y-%m-%d")
DAYSKEEP=7