Skip to content

Instantly share code, notes, and snippets.

Avatar

Kellen Mace kellenmace

View GitHub Profile
@kellenmace
kellenmace / javascript-array-filter-indicies.js
Created Sep 16, 2019
JavaScript Array Filter to get Indices
View javascript-array-filter-indicies.js
const cars = [
{make: "ford", model: "mustang"},
{make: "toyota", model: "camry"},
{make: "ford", model: "fiesta"},
{make: "chevrolet", model: "volt"},
{make: "ford", model: "escape"},
{make: "chrysler", model: "pacifica"},
]
const fordCarIndices = cars.reduce((fordCarIndices, field, index) => {
@kellenmace
kellenmace / km-remove-slug-from-custom-post-type.php
Last active May 6, 2020
Remove Slug from Custom Post Type URL in WordPress
View km-remove-slug-from-custom-post-type.php
<?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
*/
@kellenmace
kellenmace / GravityFormsShortcodeFinder.php
Created Mar 29, 2019
Find all Pages that Contain a Gravity Form
View GravityFormsShortcodeFinder.php
<?php
/**
* Finds pages that contain a particular Gravity Form.
*/
class Gravity_Forms_Shortcode_Finder {
/**
* ID of the Gravity Form to search for.
*
* @var int
View gravity-forms-shortcode-finder-usage.php
<?php
// Require the file that contains the GravityFormsShortcodeFinder class.
require_once plugin_dir_path( __FILE__ ) . 'src/GravityFormsShortcodeFinder.php';
// Find all the pages that contain the Gravity Forms with an ID of 36.
$pages_with_form = ( new Gravity_Forms_Shortcode_Finder( 36 ) )->find();
@kellenmace
kellenmace / wordpress-user-query-not-working-with-ajax-wp-cron.php
Last active Mar 6, 2019
WordPress User Query Not Working with AJAX/WP-Cron
View wordpress-user-query-not-working-with-ajax-wp-cron.php
<?php
private function get_foremen_in_division( $division ) {
$original_user_id = get_current_user_id();
// Set current user to be a Super Admin so the user query below works during the AJAX requests & WP-Cron jobs.
$super_admins = get_super_admins();
if ( $super_admins ) {
$super_admins = array_values( $super_admins );
wp_set_current_user( null, $super_admins[0] );
@kellenmace
kellenmace / get-list-of-wordpress-pages-that-contain-shortcode.php
Created Mar 4, 2019
Get a list of all WordPress pages that contain a shortcode
View get-list-of-wordpress-pages-that-contain-shortcode.php
<?php
/**
* Get a list of all pages that contain a shortcode.
*
* @param string $shortcode The shortcode to look for without square brackets.
*
* @return array List of pages in the format $post_id => $post_title.
*/
function get_pages_with_shortcode( $shortcode ) {
View get-all-gravity-forms-with-css-class.php
<?php
/**
* Get a list of Gravity Forms whose fields have a CSS class.
*
* @param string $css_class The CSS class to search for.
*
* @return array The list of forms in this format: [<form ID> - <form title>] => <array of fields that have CSS class>
*/
function get_gravity_forms_with_css_class( $css_class ) {
View remove-duplicate-objects-from-array-result.php
<?php
Array
(
[0] => Car Object
(
[model:Car:private] => Mustang
)
[1] => Car Object
@kellenmace
kellenmace / remove-duplicate-objects-from-array.php
Created Feb 2, 2019
If you have an indexed array of objects, and you want to remove duplicates by comparing a specific property in each object, a function like the remove_duplicate_models() one below can be used.
View remove-duplicate-objects-from-array.php
<?php
class Car {
private $model;
public function __construct( $model ) {
$this->model = $model;
}
public function get_model() {
@kellenmace
kellenmace / get-all-super-admins.php
Created Jan 7, 2019
Get all Super Admin users in WordPress
View get-all-super-admins.php
<?php
/**
* Get all Super Admin users on the current site.
*
* @return array WP_User objects.
*/
function get_all_superadmin_users() {
return get_users([
'login__in' => get_super_admins()
You can’t perform that action at this time.