Skip to content

Instantly share code, notes, and snippets.

View wpscholar's full-sized avatar
😀
Happy

Micah Wood wpscholar

😀
Happy
View GitHub Profile
@wpscholar
wpscholar / .eslintignore
Last active March 14, 2022 10:21
Webpack 4 Config for WordPress plugin, theme, and block development
**/*.min.js
**/*.build.js
**/node_modules/**
**/vendor/**
build
coverage
cypress
node_modules
vendor
@wpscholar
wpscholar / wp-rest-api-add-gutenberg-blocks.php
Last active March 15, 2022 01:19
Surface all Gutenberg blocks in the WordPress REST API
<?php
add_action('rest_api_init', function() {
// Surface all Gutenberg blocks in the WordPress REST API
$post_types = get_post_types_by_support( [ 'editor' ] );
foreach ( $post_types as $post_type ) {
if ( gutenberg_can_edit_post_type( $post_type ) ) {
@wpscholar
wpscholar / wp-internal-rest-request.php
Created February 20, 2018 22:44
Use the WordPress REST API directly in PHP without actually making an HTTP request.
<?php
function wpscholar_rest_get_request( $route, array $params = [] ) {
$request = new WP_REST_Request( 'GET', $route );
$request->set_query_params( $params );
$response = rest_do_request( $request );
return rest_get_server()->response_to_data( $response, false );
}
<?php
echo esc_html( size_format( wp_max_upload_size() ) );
@wpscholar
wpscholar / plugins-url.php
Created February 7, 2018 16:20
Fix plugin URLs for plugins inside the theme
<?php
/**
* Fix plugin URLs for plugins inside the theme
*/
add_filter( 'plugins_url', function ( $url, $path, $plugin ) {
$theme_path = trailingslashit( wp_normalize_path( get_theme_file_path() ) );
if ( 0 === strpos( $plugin, $theme_path ) ) {
$basename = trim( dirname( str_replace( $theme_path, '', $plugin ) ), '/' );
$url = get_theme_file_uri( "$basename/$path" );
@wpscholar
wpscholar / loader.js
Created January 17, 2018 18:56
Vanilla JS loader indicator
function Loader(el) {
el.loader = this;
this.el = el;
this.style = document.getElementById('js-loader-styles');
this.init();
if (!document.body.animate) {
if (!this.style) {
this.style = document.createElement('style');
@wpscholar
wpscholar / printPartialScreen.js
Created October 18, 2017 21:15
Print part of the screen based on a CSS selector
function print(selector) {
var w = window.open('', '', 'left=0,top=0,width=' + window.width + ',height=' + window.height + ',toolbar=0,scrollbars=0,status=0');
w.document.write('<link rel="stylesheet" type="text/css" href="/path/to.css" />');
w.document.write(document.querySelector(selector).innerHTML);
w.document.close();
w.addEventListener('load', function () {
w.focus();
w.print();
w.close();
}, true);
@wpscholar
wpscholar / maybeConvertCallable.php
Last active October 18, 2017 19:38
A function for converting callables to values, or allowing values to simply pass through.
<?php
/**
* Check if value is a callable, if so, covert the callable to a value.
* Accepts any number of additional arguments, all of which will be passed to the callable.
*
* @param callable|mixed $value
*
* @return mixed
*/
@wpscholar
wpscholar / global.js
Created August 10, 2017 19:54
JS for handling empty links, PDF links and external links
(function (win, doc) {
doc.addEventListener('DOMContentLoaded', () => {
// Prevent empty links from doing anything
[].forEach.call(doc.querySelectorAll('a[href="#"]'), function (el) {
el.addEventListener('click', function (e) {
e.preventDefault();
});
});
@wpscholar
wpscholar / pantheon-wp-config.php
Last active March 26, 2021 05:34
My Pantheon wp-config.php template.
<?php
define( 'SITE_LIVE_DOMAIN', SITE_LIVE_DOMAIN );
define( 'WP_ENV', isset( $_ENV['PANTHEON_ENVIRONMENT'] ) ? $_ENV['PANTHEON_ENVIRONMENT'] : 'local' );
define( 'SCHEME', isset( $_SERVER['HTTP_USER_AGENT_HTTPS'] ) && $_SERVER['HTTP_USER_AGENT_HTTPS'] == 'ON' ? 'https' : 'http' );
// Redirect all traffic on live site to the correct domain
if ( 'live' === WP_ENV && php_sapi_name() !== 'cli' ) {
if ( $_SERVER['HTTP_HOST'] !== SITE_LIVE_DOMAIN ) {