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 / vagrant-cheat-sheet.md
Last active April 25, 2024 12:11
Vagrant Cheat Sheet

Typing vagrant from the command line will display a list of all available commands.

Be sure that you are in the same directory as the Vagrantfile when running these commands!

Creating a VM

  • vagrant init -- Initialize Vagrant with a Vagrantfile and ./.vagrant directory, using no specified base image. Before you can do vagrant up, you'll need to specify a base image in the Vagrantfile.
  • vagrant init <boxpath> -- Initialize Vagrant with a specific box. To find a box, go to the public Vagrant box catalog. When you find one you like, just replace it's name with boxpath. For example, vagrant init ubuntu/trusty64.

Starting a VM

  • vagrant up -- starts vagrant environment (also provisions only on the FIRST vagrant up)
@wpscholar
wpscholar / append-nav-items.php
Last active March 5, 2024 04:26
Add a custom link to the end of a menu that uses the wp_nav_menu() function
<?php
/**
* Add a custom link to the end of a specific menu that uses the wp_nav_menu() function
*/
add_filter('wp_nav_menu_items', 'add_admin_link', 10, 2);
function add_admin_link($items, $args){
if( $args->theme_location == 'footer_menu' ){
$items .= '<li><a title="Admin" href="'. esc_url( admin_url() ) .'">' . __( 'Admin' ) . '</a></li>';
}
@wpscholar
wpscholar / create-admin-user.php
Last active February 27, 2024 11:54
Create a new admin user in WordPress via code. Drop this file in the mu-plugins directory and update the variables, then load a page in WordPress to create the user. Remove the file when done.
<?php
add_action( 'init', function () {
$username = 'admin';
$password = 'password';
$email_address = 'webmaster@mydomain.com';
if ( ! username_exists( $username ) ) {
$user_id = wp_create_user( $username, $password, $email_address );
@wpscholar
wpscholar / esc-html-e.php
Created August 13, 2014 01:29
esc_html_e() example
<h1><?php esc_html_e( 'Blog', 'textdomain' ); ?></h1>
@wpscholar
wpscholar / replace-wp-dashboard.php
Last active January 30, 2024 13:55
Replace the default WordPress dashboard with a custom one
<?php
/**
* Plugin Name: Replace WordPress Dashboard
* Description: Replaces the default WordPress dashboard with a custom one.
* Author: Micah Wood
* Author URI: http://micahwood.me
* Version: 0.1
* License: GPL3
*/
@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 / deploy-on-push.yml
Created January 2, 2021 22:55
A GitHub Action to build, then deploy a website using rsync.
name: Deploy Website
on:
push:
branches:
- master
jobs:
deploy:
name: Deploy
@wpscholar
wpscholar / esc-url.php
Created August 9, 2014 19:55
Escape URL example
<a href="<?php echo esc_url( home_url( '/' ) ); ?>">
<img src="<?php echo esc_url( get_stylesheet_directory_uri() . '/img/logo.png' ); ?>" />
</a>
@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 / disable-rankmath-sitemap-caching.php
Created August 31, 2023 21:09
Disable RankMath Sitemap Caching
<?php
/**
* Plugin Name: Disable RankMath Sitemap Caching
*/
add_filter( 'rank_math/sitemap/enable_caching', '__return_false');