Skip to content

Instantly share code, notes, and snippets.

View danielbachhuber's full-sized avatar

Daniel Bachhuber danielbachhuber

View GitHub Profile
@danielbachhuber
danielbachhuber / correct-cookie-domain.php
Last active August 31, 2020 16:45
Correct the cookie domain when a custom domain is mapped to the site.
@danielbachhuber
danielbachhuber / cmd-list.php
Last active May 18, 2016 17:03
Generate a task list of all WP-CLI commands
<?php
/**
* Run with: wp eval-file cmd-list.php --skip-wordpress | pbcopy
*/
$ret = WP_CLI::launch_self( 'cli cmd-dump', array(), array( 'format' => 'json' ), true, true );
$cmds = json_decode( $ret->stdout, true );
$cmd_list = array();
foreach( $cmds['subcommands'] as $cmd ) {
@danielbachhuber
danielbachhuber / install-required-plugins.php
Created April 13, 2016 10:54
Example for how you might install a plugin after a theme is activated
<?php
/**
* Run with wp --require=install-required-plugins.php theme activate p2
*/
WP_CLI::add_hook( 'after_wp_load', function() {
add_action( 'switch_theme', function( $_, $theme ){
if ( 'p2' !== $theme->get_stylesheet() ) {
return;
}
@danielbachhuber
danielbachhuber / empty-p2p-tables.php
Last active April 12, 2016 13:18
Empty Posts 2 Posts tables when using wp site empty
<?php
/**
* Requires WP-CLI v0.24.0-alpha or later because it needs https://github.com/wp-cli/wp-cli/pull/2647
*/
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_hook( 'after_invoke:site empty', function(){
global $wpdb;
foreach( array( 'p2p', 'p2pmeta' ) as $table ) {
$table = $wpdb->$table;
$wpdb->query( "TRUNCATE $table" );
@danielbachhuber
danielbachhuber / import-cache.php
Created March 31, 2016 18:27
During `wp import`, cache remote files locally for subsequent imports
<?php
/**
* Run with `wp --require=import-cache.php import ...
*/
WP_CLI::add_hook( 'after_wp_load', function(){
// Only intercept HTTP requests when the importer is running
if ( ! defined( 'WP_IMPORTING') || ! WP_IMPORTING ) {
return;
@danielbachhuber
danielbachhuber / class-records-controller.php
Created March 30, 2016 20:04
A real-world controller for a WP REST API endpoint
<?php
namespace LitART\LMS\REST;
use LitART\LMS\Query;
use WP_Error;
use WP_REST_Server;
class Records_Controller extends Posts_Controller {
@danielbachhuber
danielbachhuber / class-methods.php
Created March 16, 2016 12:46
Get all of the public, protected, and private methods on WP-API classes
<?php
/**
* Get all of the public, protected, and private methods on WP-API classes
*
* Run with `wp eval-file class-methods.php`
*/
foreach( get_declared_classes() as $class ) {
if ( ! preg_match( '#WP_REST_([A-Za-z_]+)?Controller#', $class ) ) {
continue;
@danielbachhuber
danielbachhuber / cache-purge.php
Created February 22, 2016 17:34
Trigger Varnish cache purge on deploy to WP Engine
<?php
add_action( 'init', function(){
// Replace '901bcc678021c0e12f1583085cafda1d' with a secret of your own.
if ( ! empty( $_GET['purge-cache'] ) && '901bcc678021c0e12f1583085cafda1d' === $_GET['purge-cache'] ) {
WpeCommon::purge_varnish_cache_all();
echo 'Cache purged';
exit;
}
});
@danielbachhuber
danielbachhuber / collection-filter.js
Last active January 29, 2025 04:58
Add a custom taxonomy dropdown filter to the WordPress Media Library
(function(){
/**
* Create a new MediaLibraryTaxonomyFilter we later will instantiate
*/
var MediaLibraryTaxonomyFilter = wp.media.view.AttachmentFilters.extend({
id: 'media-attachment-taxonomy-filter',
createFilters: function() {
var filters = {};
// Formats the 'terms' we've included via wp_localize_script()
@danielbachhuber
danielbachhuber / wp-hook-command.php
Last active September 4, 2024 15:03
WP-CLI command to list callbacks registered to a given action or filter
<?php
/**
* List callbacks registered to a given action or filter.
*
* <hook>
* : The key for the action or filter.
*
* [--format=<format>]
* : List callbacks as a table, JSON, or CSV. Default: table.
*