Skip to content

Instantly share code, notes, and snippets.

View kellenmace's full-sized avatar

Kellen Mace kellenmace

View GitHub Profile
@kellenmace
kellenmace / km-remove-slug-from-custom-post-type.php
Last active March 26, 2024 00:49
Remove Slug from Custom Post Type URL in WordPress
<?php
/**
* Plugin Name: Remove Slug from Custom Post Type
* Description: Remove slug from custom post type URLs.
* Version: 0.1.0
* Author: Kellen Mace
* Author URI: https://kellenmace.com/
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
<?php
/**
* Delete all transients from the database whose keys have a specific prefix.
*
* @param string $prefix The prefix. Example: 'my_cool_transient_'.
*/
function delete_transients_with_prefix( $prefix ) {
foreach ( get_transient_keys_with_prefix( $prefix ) as $key ) {
delete_transient( $key );
import { formatDistance, parseISO } from 'date-fns';
// Example string date
const dateString: string = "2023-05-23";
// Parse the string date into a JavaScript Date object
const date: Date = parseISO(dateString);
// Get the current date
const currentDate: Date = new Date();
@kellenmace
kellenmace / wpgraphql-prev-next-pagination.php
Last active December 6, 2023 02:02
Example of a previous/next WPGraphQL pagination plugin
<?php
/**
* Plugin Name: WPGraphQL Previous / Next Pagination
* Description: Enables Previous / Next Pagination via WPGraphQL
* Version: 0.1.0
* Author: Kellen Mace
* Author URI: https://kellenmace.com/
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
@kellenmace
kellenmace / get-the-excerpt-by-post-id-in-wordpress.php
Created September 6, 2016 18:13
Get the Excerpt by Post ID in WordPress
<?php
/**
* Return the post excerpt, if one is set, else generate it using the
* post content. If original text exceeds $num_of_words, the text is
* trimmed and an ellipsis (…) is added to the end.
*
* @param int|string|WP_Post $post_id Post ID or object. Default is current post.
* @param int $num_words Number of words. Default is 55.
* @return string The generated excerpt.
@kellenmace
kellenmace / get-beaver-builder-modules-on-page.php
Last active October 21, 2023 04:17
Get a list of all the Beaver Builder modules on a page
<?php
/**
* Class for getting a list of Beaver Builder modules.
*/
class KM_Beaver_Builder_Module_List {
/**
* Get the list of Beaver Builder modules.
*
* @param int $post_id The post ID. Default is the current post being
@kellenmace
kellenmace / store-transient-list.php
Last active October 9, 2023 03:03
Keep track of all transients that could exist in an option so that you can delete them by prefix. Useful for deleting dynamically generated keys on sites with a persistent object cache. Note: be careful with garbage collection - only use this when there is a finite number of possible transient keys.
<?php
/**
* Set/update the value of a transient.
*
* Transients set using this function can later be deleted in bulk
* based on their prefix using rth_delete_transients().
*
* @param string $key Transient key.
* @param mixed $value Transient value.
@kellenmace
kellenmace / get-root-domain-in-wordpress.php
Created August 19, 2016 13:20
Get Root Domain in WordPress
<?php
/**
* Get the root domain of the site/network.
*
* @return string|bool The root domain or false on failure.
*/
function km_get_root_domain() {
$url_parts = parse_url( km_get_main_site_url() );
@kellenmace
kellenmace / index.js
Last active October 2, 2023 16:20
Get YouTube Video Transcript in JavaScript
const fetch = require("node-fetch");
const xml2js = require("xml2js");
const he = require("he");
const TRANSCRIPTION_CHAR_KEY = "transcription";
// Test
(async () => {
const videoId = "ht14hTTDklA"; // HWPR Pagination video
// const videoId = "rB9ql0L0cUQ"; // Video with manually added captions
@kellenmace
kellenmace / get-users-first-last-name-wordpress.php
Created September 20, 2016 20:43
Get User's First and Last Name in WordPress
<?php
/**
* Get user's first and last name, else just their first name, else their
* display name. Defalts to the current user if $user_id is not provided.
*
* @param mixed $user_id The user ID or object. Default is current user.
* @return string The user's name.
*/
function km_get_users_name( $user_id = null ) {