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 / trimByCharacterAndWordCount.php
Created March 1, 2022 18:16
Truncate a string to a certain character length and make sure to only break at words.
<?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 January 29, 2022 01:34
Enable maintenance mode via code
<?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 August 25, 2021 19:21
WP-CLI Script menu creation
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 March 31, 2021 20:13
WordPress plugin to remove empty paragraph tags from shortcodes in WordPress.
<?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 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 / rest-url.php
Last active December 28, 2020 14:10
A WordPress plugin that updates the REST URL to use the site URL instead of the home URL.
<?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 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,
),