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 / api-caching-example.php
Created June 9, 2020 15:30
An example of how to make an external API call in WordPress and cache the response.
<?php
$cache_key = 'my_api_call_response';
$response = get_transient( $cache_key );
if ( ! $response ) {
$response = wp_remote_get('https://example.com/api/v1/endpoint');
$status_code = (int) wp_remote_retrieve_response_code( $response );
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true );
if ( 200 === $status_code && $data ) {
@wpscholar
wpscholar / post-content-shortcode.php
Created May 22, 2020 00:47
A simple plugin providing a shortcode that will output the post content for a specific post ID.
<?php
/**
* Post Content Shortcode
*
* @package PostContentShortcode
* @author Micah Wood
* @copyright Copyright 2020 by Micah Wood - All rights reserved.
* @license GPL2.0-or-later
*
* @wordpress-plugin
@wpscholar
wpscholar / prevent-plugin-updates.php
Created May 19, 2020 17:25
A WordPress MU plugin that can prevent plugin updates for specific plugins.
<?php
add_filter(
'site_transient_update_plugins',
function ( $transient ) {
$plugins = array(
'wordpress-seo/wp-seo.php',
);
foreach ( $plugins as $plugin ) {
@wpscholar
wpscholar / single.php
Created May 8, 2020 18:37
A custom Stellar Places template to show nearby places.
<?php
$place = Stellar_Places::get_place_object( get_post() );
$nearby = Stellar_Places::get_places(
array(
'post__not_in' => array( get_the_ID() ),
'geo_query' => array(
'lat' => $place->latitude,
'lng' => $place->longitude,
'distance' => 1,
),
@wpscholar
wpscholar / install-xdebug.sh
Created April 19, 2020 02:16
Install xdebug from the command line.
# Use homebrew to switch PHP versions, run this command for each version as needed:
pecl install xdebug
@wpscholar
wpscholar / grep-status-codes.sh
Last active October 6, 2023 17:46
How to use grep to lookup status codes in access logs.
# Output the access log
cat access.log
# Run the results through grep to find any 500 status codes
cat access.log | grep "[: ]500[: ]"
# Find any non 200 status codes
cat access.log | grep -v "[: ]200[: ]"
# Find multiple status codes
@wpscholar
wpscholar / unstick-valet-php-version.sh
Created March 18, 2020 16:40
What to do when Laravel Valet won't let you change PHP versions even though you ran `valet use [email protected]`
brew services stop [email protected]
brew services start [email protected]
valet restart
@wpscholar
wpscholar / create-database.sql
Created March 13, 2020 12:20
Create a MySQL database
CREATE DATABASE <name>;
GRANT ALL PRIVILEGES ON <name>.* TO "<user>"@"localhost";
FLUSH PRIVILEGES;
@wpscholar
wpscholar / brew-setup-mysql.sh
Created March 13, 2020 12:18
Setup MySQL using Brew
brew install mysql
brew services start mysql
# Set password to be 'root' for the root user
$(brew --prefix mysql)/bin/mysqladmin -u root password root
@wpscholar
wpscholar / cypress--support--commands.js
Created January 22, 2020 21:12
Cypress setup for conditional WordPress login. (Double dashes in file names should be replaced with slashes)
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//