Skip to content

Instantly share code, notes, and snippets.

View spacedmonkey's full-sized avatar
🖐️
Open for work

Jonny Harris spacedmonkey

🖐️
Open for work
View GitHub Profile
@spacedmonkey
spacedmonkey / gist:d9a3c066c63dff41470a2dedd2d8734d
Created October 18, 2019 22:43
Get all posts for all post types in javascript.
wp.apiFetch( { path: '/wp/v2/types' } ).then( ( post_types ) => {
for ( let post_type in post_types ){
let {rest_base} = post_types[post_type];
wp.apiFetch( { path: '/wp/v2/' + rest_base } ).then( ( posts ) => {
console.log(posts);
});
}
} );
@spacedmonkey
spacedmonkey / Functions
Last active May 3, 2019 18:05
Starts of a feature plugin for global user roles.
Create table
Remove Table
Activate
Uninstall
Changes to 5.1
Multisite
https://core.trac.wordpress.org/ticket/37923
https://core.trac.wordpress.org/ticket/40364
https://core.trac.wordpress.org/ticket/44368
https://core.trac.wordpress.org/ticket/41333
https://core.trac.wordpress.org/ticket/40647
function get_site_id_of_user( $user_id, $all = false ) {
global $wpdb;
$user_id = (int) $user_id;
// Logged out users can't have sites
if ( empty( $user_id ) ) {
return array();
}
$keys = get_user_meta( $user_id );
if ( empty( $keys ) ) {
@spacedmonkey
spacedmonkey / gist:6d0b395df4664040df9095fdd54a1562
Last active July 11, 2018 17:10
Change the admin theme depending on enviroments in WordPress
<?php
add_filter( 'get_user_metadata', function ( $value, $object_id, $meta_key, $single ) {
if ( 'admin_color' !== $meta_key || ! $single ) {
return $value;
}
if ( ! defined( 'WPCOM_IS_VIP_ENV' ) ) {
return $value;
}
switch ( WPCOM_IS_VIP_ENV ) {
case 'develop':
public function migrate1( $args, $assoc_args ) {
global $wpdb;
$user_list = $wpdb->get_col( "SELECT ID FROM $wpdb->users;" );
// Number of users returned by query
$found_users = count( $user_list );
// Generate progess bar
$progress = new \cli\progress\Bar( 'Progress', $found_users );
@spacedmonkey
spacedmonkey / gist:be74fb8160d5746f43614774db4d877b
Created December 24, 2017 17:58
Using S3 Uploads with digital ocean spaces
add_filter('s3_uploads_s3_client_params', function($params){
$params['endpoint'] = 'https://ams3.digitaloceanspaces.com';
return $params;
}, 10, 1);
<?php
/**
* Force cookie constants to be local domain / path
* after multisite is loaded.
*/
add_action(
'ms_loaded', function () {
$site = get_site();
$properties = array( 'blogname', 'siteurl', 'post_count', 'home' );
foreach ( $properties as $property ) {
add_action("add_option_".$property,"add_option_site_meta" 99, 2);
add_action("delete_option_".$property,"delete_option_site_meta" 99, 1);
add_action("update_option_".$property,"update_option_site_meta" 99, 3);
}
function delete_option_site_meta( $option ){
delete_site_meta(get_current_blog_id(), $option);
}
class WP_Short_Circuit_Result {
private $name = null;
private $value = null;
public $has_changed = false;
public function __construct( $name = null, $value = null ) {
$this->name = $name;