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 June 26, 2025 12:26
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 / random-post-on-refresh-polylang.php
Last active June 3, 2025 15:30
Integrates the Random Post on Refresh plugin with Polylang to restrict queries to the current language.
<?php
/**
* Plugin Name: Random Post on Refresh – Polylang Integration
* Plugin URI: https://gist.github.com/wpscholar/215695a02a495194d7f7f8da2f46d519
* Description: Integrates the Random Post on Refresh plugin with Polylang to restrict queries to the current language if none is explicitly provided.
* Version: 1.0
* Author: Micah Wood
* Author URI: https://wpscholar.com
* License: GPLv2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
@wpscholar
wpscholar / createInterpolateElement.jsx
Created February 14, 2025 14:20
An example of how to use the createInterpolateElement in React to properly translate a string containing HTML or a React component.
// See https://developer.wordpress.org/block-editor/reference-guides/packages/packages-element/#createinterpolateelement
import { __ } from '@wordpress/i18n';
import { createInterpolateElement } from '@wordpress/element';
function MyComponent() {
return createInterpolateElement(
__( 'Please <Link>visit our site</Link> for more info.', 'text-domain' ),
{
Link: <a href="https://example.com" />,
@wpscholar
wpscholar / phpcs.xml
Created February 12, 2025 17:31
PHPCS I18N
<rule ref="WordPress.WP.I18n">
<properties>
<property name="text_domain" type="array" value="newfold-module-performance" />
</properties>
</rule>
@wpscholar
wpscholar / create-admin-user.php
Last active January 30, 2025 17:31
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 / wpdb-prepare.php
Last active January 15, 2025 21:08
$wpdb->prepare() example
<?php
global $wpdb;
$query = $wpdb->prepare(
"SELECT * FROM {$wpdb->posts} WHERE post_type = %s",
$post->post_type
);
$results = $wpdb->get_results( $query );
@wpscholar
wpscholar / replace-wp-dashboard.php
Last active December 3, 2024 23:58
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 / functions.php
Last active October 20, 2024 14:01
Enqueueing IE conditional stylesheets in WordPress the right way
<?php
add_action( 'wp_enqueue_scripts', 'enqueue_my_styles' );
/**
* Example callback function that demonstrates how to properly enqueue conditional stylesheets in WordPress for IE.
* IE10 and up does not support conditional comments in standards mode.
*
* @uses wp_style_add_data() WordPress function to add the conditional data.
* @link https://developer.wordpress.org/reference/functions/wp_style_add_data/
@wpscholar
wpscholar / order-posts-by-taxonomy.php
Last active September 28, 2024 22:45
Order WordPress posts based on their taxonomy name.
<?php
use WP_Query;
/**
* Class OrderPostsByTaxonomy
*
* @link https://scribu.net/wordpress/sortable-taxonomy-columns.html
*
* Handles the ordering of posts based on their associated taxonomy.
@wpscholar
wpscholar / is_plugin_active.php
Created May 14, 2012 18:01
Test if a WordPress plugin is active
<?php
/**
* Test if a WordPress plugin is active
*/
if ( is_plugin_active('plugin-directory/plugin-file.php') ) {
// the plugin is active
}