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 / rss-to-json.php
Last active January 29, 2021 01:41
A simple must-use plugin for WordPress that will provide a `/feed/json` endpoint for converting RSS feeds to JSON.
<?php
/**
* A simple must-use plugin for WordPress that will provide a `/feed/json` endpoint for converting RSS feeds to JSON.
* In the absence of any URL parameters, it will convert the main feed to JSON. If a URL is set, it will convert the
* feed from that URL into JSON. If a callback is set, a JSONP implementation will be returned.
*
* Note: Be sure to flush rewrite rules manually by visiting the 'General'->'Permalinks' page in the WordPress admin. If
* you are using this in a plugin, be sure to flush the rewrite rules on activation using the 'flush_rewrite_rules()' function.
*
@wpscholar
wpscholar / set-value.php
Created October 4, 2016 14:52
Set a value on an multidimensional array using dot notation in PHP
<?php
/**
* Set a value on an multidimensional array using dot notation
*
* @param array $data
* @param string $key
* @param mixed $value
*
* @return mixed
{
"audits": [
{
"id": 1,
"facility": "ST-CD-Mbandaka",
"bottler": "BGI",
"country": "Ivory coast",
"businessUnit": "Central East and West Africa",
"bottlerGroup": "Eurasia Africa",
"status": "completed",
@wpscholar
wpscholar / smooth-scroll.jquery.js
Created March 15, 2016 15:50
Implement smooth scroll for all internal page links.
// Smooth Scroll
$('a[href*="#"]:not([href="#"])').click(function () {
var target = $(this.hash);
if (target.length) {
var isInternalLink = new RegExp('/' + window.location.host + '/');
if (isInternalLink.test(this.href)) {
$('html, body').animate({scrollTop: target.offset().top}, 1000);
return false;
}
}
@wpscholar
wpscholar / redirect-root-posts.php
Last active February 1, 2016 17:49
Redirect all root-level URL paths for posts to the new permalink paths.
<?php
add_action( 'template_redirect', function () {
/**
* @var WP $wp
*/
global $wp;
/**
@wpscholar
wpscholar / defer-async-scripts.php
Last active November 18, 2022 02:41
Class that allows you to async and defer scripts in WordPress by adding data.
<?php
class WP_Scholar_Defer_Scripts {
public static function initialize() {
add_filter( 'script_loader_tag', array( __CLASS__, 'defer_scripts' ), 10, 2 );
add_filter( 'script_loader_tag', array( __CLASS__, 'async_scripts' ), 10, 2 );
}
public static function defer_scripts( $tag, $handle ) {
@wpscholar
wpscholar / user-registration-field.php
Created December 22, 2015 17:30
A class for handling the addition of custom user registration fields in WordPress.
<?php
/**
* Class WP_Scholar_User_Registration_Field
*/
class WP_Scholar_User_Registration_Field {
/**
* Field name
*
@wpscholar
wpscholar / cached-query.php
Last active April 14, 2020 06:32
Example of a cached query
<?php
$cache_key = '%cache_key%';
$query = get_transient( $cache_key );
if ( ! $query ) {
$query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
@wpscholar
wpscholar / post-content-appender.php
Created November 15, 2015 15:29
Sample class to demonstrate how classes can be used in WordPress.
<?php
/**
* Class PostContentAppender
*/
class PostContentAppender {
protected $append = '';
public function __construct( $append ) {