Skip to content

Instantly share code, notes, and snippets.

😀
Happy

Micah Wood wpscholar

😀
Happy
Block or report user

Report or block wpscholar

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@wpscholar
wpscholar / .htaccess
Last active Mar 4, 2019
Set up WordPress Multi Network
View .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
@wpscholar
wpscholar / wp-list-find.php
Created Oct 26, 2018
Created a function that will return the first found match from a list of items.
View wp-list-find.php
<?php
/**
* Find a single item in a list based on specific criteria.
*
* @uses wp_list_filter()
*
* @param array $list
* @param array $args
* @param string $operator
@wpscholar
wpscholar / mm_get_client_ip()
Last active Sep 28, 2018
What are the security implications of overwriting `REMOTE_ADDR`? Do we need to explicitly check for trusted IP addresses (e.g. https://www.cloudflare.com/ips/)? Should 'HTTP_X_FORWARDED_FOR' ever be used? See https://kb.sucuri.net/firewall/Troubleshooting/same-user-ip
View mm_get_client_ip()
/**
* Get the client IP address.
*
* @return string
*/
function mm_get_client_ip() {
// Default to REMOTE_ADDR
$ip = $_SERVER['REMOTE_ADDR'];
@wpscholar
wpscholar / schema-api.php
Created May 30, 2018
Plugin for creating a post type with a custom API that reflects Schema.org data structures
View schema-api.php
<?php
/*
* Plugin Name: Schema API
* Plugin URI:
* Description:
* Version: 0.1.0
* Author: Micah Wood
* Author URI: https://wpscholar.com
* License: GPL2
@wpscholar
wpscholar / nojs.webpack.js
Created Apr 26, 2018
A custom WebPack 4 plugin for removing generated JS files that aren't needed.
View nojs.webpack.js
function() {
// Custom webpack plugin - remove generated JS files that aren't needed
this.hooks.done.tap( 'webpack', function( stats ) {
stats.compilation.chunks.forEach( chunk => {
if (!chunk.entryModule._identifier.includes( '.js' )) {
chunk.files.forEach( file => {
if (file.includes( '.js' )) {
fs.unlinkSync( path.join( __dirname, `/${file}` ) );
}
} );
@wpscholar
wpscholar / .eslintignore
Last active Aug 18, 2019
Webpack 4 Config for WordPress plugin, theme, and block development
View .eslintignore
**/*.min.js
**/*.build.js
**/node_modules/**
**/vendor/**
build
coverage
cypress
node_modules
vendor
@wpscholar
wpscholar / wp-rest-api-add-gutenberg-blocks.php
Last active Jan 16, 2019
Surface all Gutenberg blocks in the WordPress REST API
View wp-rest-api-add-gutenberg-blocks.php
<?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 Feb 20, 2018
Use the WordPress REST API directly in PHP without actually making an HTTP request.
View wp-internal-rest-request.php
<?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 );
}
View wp-max-upload-size.php
<?php
echo esc_html( size_format( wp_max_upload_size() ) );
@wpscholar
wpscholar / plugins-url.php
Created Feb 7, 2018
Fix plugin URLs for plugins inside the theme
View plugins-url.php
<?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" );
You can’t perform that action at this time.