Skip to content

Instantly share code, notes, and snippets.

Avatar
😀
Happy

Micah Wood wpscholar

😀
Happy
View GitHub Profile
@wpscholar
wpscholar / cypress.yml
Created Apr 29, 2022
Sample GitHub Action workflow for running Cypress tests with @wordpress/env
View cypress.yml
name: Cypress Tests
on:
push:
branches:
- master
pull_request:
types: [opened, edited, synchronize, reopened, ready_for_review]
pull_request_review:
types: [submitted, edited]
@wpscholar
wpscholar / commands.js
Created Apr 29, 2022
Cypress commands for conditionally logging into WordPress. Add to cypress/support/commands.js
View commands.js
Cypress.Commands.add('login', () => {
// Fetch username and password from the cypress.env.json file.
const username = Cypress.env('wpUsername');
const password = Cypress.env('wpPassword');
cy
.getCookies()
.then(cookies => {
let hasMatch = false;
@wpscholar
wpscholar / trimByCharacterAndWordCount.php
Created Mar 1, 2022
Truncate a string to a certain character length and make sure to only break at words.
View trimByCharacterAndWordCount.php
<?php
$strings = [
'One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua',
'Bacon ipsum dolor amet burgdoggen biltong pastrami, kielbasa sirloin strip steak cupim andouille tenderloin.',
'Hodor. Hodor hodor, hodor. Hodor hodor hodor hodor hodor. Hodor. Hodor! Hodor hodor, hodor; hodor hodor hodor.',
'Lorem Ipsum is the single greatest threat. We are not - we are not keeping up with other websites.',
'Cupcake ipsum dolor. Sit amet marshmallow topping cheesecake muffin.',
'This test is short.',
@wpscholar
wpscholar / maintenance-mode.php
Last active Jan 29, 2022
Enable maintenance mode via code
View maintenance-mode.php
<?php
/**
* Maintenance Mode
*
* @package MaintenanceMode
* @author Micah Wood
* @copyright Copyright 2022 by Micah Wood - All rights reserved.
* @license GPL2.0-or-later
*
* @wordpress-plugin
@wpscholar
wpscholar / menus.sh
Created Aug 25, 2021
WP-CLI Script menu creation
View menus.sh
wp menu create "Footer Primary Menu"
wp menu item add-custom footer-primary-menu "Item 1" "#"
wp menu item add-custom footer-primary-menu "Item 2" "#"
wp menu item add-custom footer-primary-menu "Item 3" "#"
wp menu location assign footer-primary-menu footer-primary
@wpscholar
wpscholar / remove-empty-p-tags.php
Created Mar 31, 2021
WordPress plugin to remove empty paragraph tags from shortcodes in WordPress.
View remove-empty-p-tags.php
<?php
/**
* Remove Empty Paragraph Tags
*
* @package RemoveEmptyParagraphTags
* @author Micah Wood
* @copyright Copyright 2021 by Micah Wood - All rights reserved.
* @license GPL2.0-or-later
*
@wpscholar
wpscholar / deploy-on-push.yml
Created Jan 2, 2021
A GitHub Action to build, then deploy a website using rsync.
View deploy-on-push.yml
name: Deploy Website
on:
push:
branches:
- master
jobs:
deploy:
name: Deploy
@wpscholar
wpscholar / rest-url.php
Last active Dec 28, 2020
A WordPress plugin that updates the REST URL to use the site URL instead of the home URL.
View rest-url.php
<?php
/**
* Custom Rest URL
*
* @package CustomRestUrl
* @author Micah Wood
* @copyright Copyright 2021 by Micah Wood - All rights reserved.
* @license GPL2.0-or-later
*
@wpscholar
wpscholar / api-caching-example.php
Created Jun 9, 2020
An example of how to make an external API call in WordPress and cache the response.
View api-caching-example.php
<?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
A simple plugin providing a shortcode that will output the post content for a specific post ID.
View post-content-shortcode.php
<?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