Skip to content

Instantly share code, notes, and snippets.

🏠
Working from home

Donna McMaster donnamcmaster

🏠
Working from home
Block or report user

Report or block donnamcmaster

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@donnamcmaster
donnamcmaster / first_leaf.php
Created Jul 9, 2019
PHP utility: find the first non-array element
View first_leaf.php
/**
* First Leaf
* Returns the first non-array element (singular or object).
* Handy when you're not sure whether information has been stored
as a singular value ('item') or 1-element array (0=>'item').
* The key() function gives us the first key in the array.
* Uses recursion to go deeper if necessary; if that's not
* what you want, you may need a different solution.
*/
public static function first_leaf ( $value ) {
@donnamcmaster
donnamcmaster / wp-filter-shortcode-content.php
Created May 1, 2019
WordPress: filter for the_content to remove excess tags around shortcodes
View wp-filter-shortcode-content.php
// get rid of junk <p> and <br> tags TinyMCE inserts around shortcodes
add_filter( 'the_content', function ( $content ) {
$array = [
'<p>[' => '[',
']</p>' => ']',
']<br />' => ']',
']<br>' => ']',
'<p>&nbsp;</p>' => '',
];
$content = strtr( $content, $array );
@donnamcmaster
donnamcmaster / wp-revision-filter.php
Last active Apr 21, 2019
WordPress filter to set number of revisions based on post type
View wp-revision-filter.php
/**
* minimize unnecessary revision storage (based on post type)
*/
add_filter( 'wp_revisions_to_keep', function ( $nr_revisions, $post ) {
switch ( $post->post_type ) {
case 'page':
case 'movie':
case 'article':
case 'story':
$nr_revisions = 3;
View wp-timestamp-stylesheets.php
$css_timestamp = filemtime( get_stylesheet_directory() . '/assets/css/app.css' );
wp_enqueue_style( 'child_app', get_stylesheet_directory_uri() . '/assets/css/app.css', null, $css_timestamp );
@donnamcmaster
donnamcmaster / strip-shortcode-ptags.php
Last active May 1, 2019
WordPress: process content outside the loop; get rid of extra <p> tags around shortcodes
View strip-shortcode-ptags.php
/* props to Thomas Griffin
https://thomasgriffin.io/remove-empty-paragraph-tags-shortcodes-wordpress/
*/
$extra_junk = array(
'<p>[' => '[',
']</p>' => ']',
']<br />' => ']',
']<br>' => ']',
'<p>&nbsp;</p>' => '',
@donnamcmaster
donnamcmaster / icon-from-svg-code.sccs
Created Jul 29, 2018
use Open Iconic SVG definition to define an icon
View icon-from-svg-code.sccs
/* ==========================================================================
SVG to CSS Example: Breadcrumbs "Home Icon"
- an example of using the Open Iconic SVG code to create a "home" icon
- this is the same method as used in the Bootstrap 4 _variables.sccs file
- started by downloading the distro from https://useiconic.com/open
- found the 'home' icon SVG and copied its path code
- then pasted the path code into one of the Bootstrap icon definitions &
changed the fill color
========================================================================== */
$link-color: blue;
@donnamcmaster
donnamcmaster / wp-woocommerce-utility-urls.php
Last active Apr 21, 2019
WordPress: URLs for WooCommerce utility pages (cart, checkout, etc)
View wp-woocommerce-utility-urls.php
<!-- Get and display the My Account page url -->
<?php echo get_permalink( wc_get_page_id( 'checkout' ) ); ?>
<!-- Get and display the Cart page url -->
<?php echo get_permalink( wc_get_page_id( 'cart' ) ); ?>
<!-- Get and display the Shop page url -->
<?php echo get_permalink( wc_get_page_id( 'shop' ) ); ?>
<!-- Get and display the Checkout page url -->
@donnamcmaster
donnamcmaster / wp-allow-view-post-editing.php
Last active Apr 22, 2018
WordPress: when a post is being edited, give a warning but allow others to view edit screen. Useful for remote training situations.
View wp-allow-view-post-editing.php
if ( is_admin() ) {
// soften post lock to post warning
add_filter( 'show_post_locked_dialog', '__return_false' );
add_filter( 'wp_check_post_lock_window', '__return_false' );
}
@donnamcmaster
donnamcmaster / wp-widget-shortcode.php
Last active Apr 7, 2017
WordPress: allows you to insert a widget into any content area using a shortcode
View wp-widget-shortcode.php
<?php
/**
* Widget Shortcode
* - allows you to insert a widget into any content area using a shortcode
* - credit: http://wp.smashingmagazine.com/2012/12/11/inserting-widgets-with-shortcodes/
* - read the comments on the Smashing Magazine article for pros and cons of this function
*/
add_shortcode( 'mcw_widget', function ( $atts ) {
// configure defaults and extract the attributes into variables
extract( shortcode_atts(
@donnamcmaster
donnamcmaster / wp-insert-custom-media-sizes.php
Created Apr 7, 2017
WordPress: add custom image sizes to the list of choices in Insert Media.
View wp-insert-custom-media-sizes.php
<?php
/**
* Custom Image Sizes
* - adds all custom image sizes to the list of choices in Insert Media
*/
add_filter( 'image_size_names_choose', function ( $sizes ) {
global $_wp_additional_image_sizes;
if ( !empty( $_wp_additional_image_sizes ) ) {
foreach ( $_wp_additional_image_sizes as $id => $data ) {
if ( !isset( $sizes[$id] ) )
You can’t perform that action at this time.