Skip to content

Instantly share code, notes, and snippets.

View kjbenk's full-sized avatar

Kyle Benk kjbenk

  • PMC
  • Vermont
View GitHub Profile
@kjbenk
kjbenk / wp-is-image-url-crop.php
Last active May 17, 2019 12:08
WordPress: Check if an image URL is a crop
<?php
function is_image_url_crop( string $image_url ) : bool {
// Check if this is an intermediate size image.
preg_match( '/-(\d+)x(\d+)\./', $image_url, $matches );
if ( ! empty( $matches[0] ) ) {
return true;
}
@kjbenk
kjbenk / class-primary-category-post-url.php
Last active February 16, 2018 14:36
WordPress: Post Permalink using primary category
<?php
/**
* Alters the default post permalink structure to be:
*
* `/{primary-category}/{post-slug}/`
*/
/**
* The Primary_Category_Post_URL class.
*/
@kjbenk
kjbenk / class-user-roles.php
Last active February 8, 2018 13:29
WordPress: Custom User Roles
<?php
/**
* Customize user roles.
*/
/**
* Class User_Roles.
*/
class User_Roles {
@kjbenk
kjbenk / class-taxonomy-top-level-url.php
Last active November 26, 2019 09:27
WordPress: Taxonomy Top Level URL
<?php
/**
* The class used to make certain taxonomies have a top level URL.
*/
/**
* The Taxonomy_Top_Level_URL class.
*/
class Taxonomy_Top_Level_URL {
/**
@kjbenk
kjbenk / admin.css
Created February 5, 2018 15:08
WordPress: Remove Shortcake Add Post Element button
// Globally hide the TinyMCE media buttons so that we can alter them and show them
// once updated.
.wp-media-buttons {
display: none;
}
@kjbenk
kjbenk / trait-singleton.php
Created January 29, 2018 16:32
WordPress Singleton Trait
<?php
/**
* Trait file for Singletons.
*/
/**
* Make a class into a singleton.
*/
trait Singleton {
/**
@kjbenk
kjbenk / wp-get-local-date.php
Last active August 3, 2017 20:18
WordPress: Get local date
<?php
/**
* Get the local date with a specific format.
*
* @param string $timestamp UNIX timestamp.
* @param string $format Date format.
* @return string Local date string.
*/
function get_local_date( $timestamp, $format = null ) {
if ( empty( $format ) ) {
@kjbenk
kjbenk / add-other-menu-items.php
Last active June 12, 2017 12:13
WordPress: Add other menu items to existing menu
<?php
/**
* Adds menu items to existing menu. This supports adding complete menus to existings menus
* as well.
*
* @param array $sorted_menu_items The menu items, sorted by each menu item's menu order.
* @param stdClass $args An object containing wp_nav_menu() arguments.
*/
function add_other_menu_items( $sorted_menu_items, $args ) {
// Do nothing if there is no menu given.
@kjbenk
kjbenk / all-post-filters.php
Created May 30, 2017 12:31
WP: All Post Filters Class
<?php
/**
* Add custom post filters on the All Posts page. This is best used with an Elastic
* Search powered site since this can create a very expensive query.
*
* Simple Example:
*
* add_filter( 'all_post_filters', function ( $filters, $post_type ) {
* // Perform post type validiation.
@kjbenk
kjbenk / get-post-gallery-image-ids.php
Created May 2, 2017 15:02
WordPress: Get Post Gallery Image IDs
<?php
/**
* Retrieves the image ids from a gallery.
*
* @param int|WP_Post $post Post ID or object.
* @return array An array of image ids.
*/
function get_gallery_image_ids( $post ) {
$post = get_post( $post );
if ( ! $post ) {