Skip to content

Instantly share code, notes, and snippets.

@chrismademe
chrismademe / appearance-access.php
Created March 4, 2020 13:17
WordPress: Give Editor role access to the Appearance menu
<?php
add_action( 'init', function() {
$role = get_role('editor');
$role->add_cap('edit_theme_options');
} );
@chrismademe
chrismademe / order-has-product.php
Created January 15, 2020 11:05
WooCommerce - order_has_product
<?php
/**
* Order Has Product
*
* Checks a WooCommerce order to see if it contains
* a given product ID
*
* @param object $order
* @param int $product_id
@chrismademe
chrismademe / container-query.js
Created October 10, 2019 07:59
Container Query using ResizeObserver
/**
* Written by Philip Walton
* @see https://philipwalton.com/articles/responsive-components-a-solution-to-the-container-queries-problem/
*/
if ('ResizeObserver' in self) {
// Create a single ResizeObserver instance to handle all
// container elements. The instance is created with a callback,
// which is invoked as soon as an element is observed as well
@chrismademe
chrismademe / woocommerce-custom-fields.php
Created October 9, 2019 08:37
WooCommerce Custom Checkout Fields
<?php
/**
* Add Fields to Order
*
* @param $fields (array) WooCommerce Checkout Fields
*/
add_action( 'woocommerce_checkout_before_customer_details', function( $fields ) {
// Define new Field
@chrismademe
chrismademe / data.php
Created July 25, 2019 15:25
get-post-with-fields.php
/**
* Get Post with Fields
*
* Returns a post with it's ACF fields
* attached to it
*
* @NOTE We don't use $post as a variable
* inside here so we don't mess with the
* WordPress global variable
*
@chrismademe
chrismademe / wp-post-list.php
Created June 13, 2019 08:06
WordPress Post List (Admin)
<?php
/*
* Manage Vacancies Columns
*/
add_filter( 'manage_vacancies_posts_columns', function ( $columns ) {
// Checkbox column
$cb = $columns['cb'];
@chrismademe
chrismademe / get-date-range.php
Created April 9, 2019 08:26
Get range of dates in an array
<?php
/**
* Get Date Range
*
* @param string $start Start date in unix timestamp format
* @param string $end End date in unix timestamp format
* @return array Range of dates in an array
*/
function get_date_range( $start, $end ) {
@chrismademe
chrismademe / disable-days.js
Last active April 2, 2019 08:18
Disable days on Pikaday instance
var picker = new Pikaday({
field: document.getElementById('datpicker'),
minDate: moment().toDate(), // Today
disableDayFn: function(date) {
var currentDate, today, yearFromNow, dateFormat;
// Set Date Format
dateFormat = 'YYYY-MM-DD';
// Create Moment Objects
@chrismademe
chrismademe / random-string.php
Created November 22, 2018 09:12
Random String in PHP
<?php
$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyz';
// Output: 54esmdr0qf
echo substr(str_shuffle($permitted_chars), 0, 10);
$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
// Output: video-g6swmAP8X5VG4jCi.mp4
echo 'video-'.substr(str_shuffle($permitted_chars), 0, 16).'.mp4';
@chrismademe
chrismademe / posts-by-month.php
Created February 12, 2018 13:17
Order posts by year and month
<?php
/**
* Get Posts by Year
*
* Returns an array of posts
* sorted by year and month.
*
* @param string $post_type Post Type
* @param array $options Options array for get_posts()