Skip to content

Instantly share code, notes, and snippets.

View michaelbourne's full-sized avatar

Michael Bourne michaelbourne

View GitHub Profile
@michaelbourne
michaelbourne / functions.php
Last active May 25, 2022 19:47
Add Themeco's Pro theme colors to Gutenberg
<?php
/**
* Get theme colors from Pro and turn them into classes and CSS variables
* And now add them to Gutenberg for use in blog posts
*
* Created Date: Wednesday May 25th 2022
* Author: Michael Bourne
* -----
* Last Modified: Wednesday, May 25th 2022, 1:36:28 pm
* Modified By: Michael Bourne
@michaelbourne
michaelbourne / functions.php
Last active October 13, 2021 23:00
Change the data presented with Yoast's Enhanced Slack Sharing
<?php
// functions.php in a child theme
/**
* Change Enhanced Slack sharing data labels.
*
* @param array $data The default Slack labels + data.
* @param Indexable_Presentation $presentation The indexable presentation object from Yoast.
*
* @return array $data The new Slack labels + data.
@michaelbourne
michaelbourne / functions.php
Created November 22, 2020 22:48
Disable Yoast's enhanced Slack sharing on pages and CPTs (non-posts)
<?php
// functions.php in a child theme
/**
* Disable Yoast's Enahnced Slack Sharing feature on non-posts
*/
function mb_disable_slack_enhancements() {
if ( 'post' !== get_post_type() ) {
add_filter( 'wpseo_output_enhanced_slack_data', '__return_false' );
}
@michaelbourne
michaelbourne / functions.php
Last active August 18, 2023 20:18
Dynamic OG images for WordPress using Microlink
<?php
/**
* The following goes in your child theme's functions.php file
*/
/**
* Generate custom Open Graph images for your blog posts
*
* @param string $url URL of OG image in HTML Meta.
@michaelbourne
michaelbourne / functions.php
Last active January 19, 2024 16:36
Add support for WOFF and WOFF2 file uploads in WordPress
<?php
// The following code goes in your functions.php file of a Child Theme
function custom_font_mime_types($mimes = array()) {
$mimes['woff'] = 'application/x-font-woff';
$mimes['woff2'] = 'application/x-font-woff2';
// Depending on your server setup, you may need to use these instead:
//$mimes['woff'] = 'font/woff';
//$mimes['woff2'] = 'font/woff2';
@michaelbourne
michaelbourne / functions.php
Last active February 6, 2019 22:58
Swap out iLightBox for newest release (untested)
<?php
add_action( 'wp_enqueue_scripts', 'miro_load_script', 99 );
function miro_load_script() {
wp_deregister_script( 'vendor-ilightbox' );
wp_register_script( 'vendor-ilightbox', get_stylesheet_directory_uri() . '/new-ilightbox.js', array( 'jquery' ), '2.2.4', true );
}
@michaelbourne
michaelbourne / cart-count-shortcode.php
Last active March 20, 2024 17:06
Create a simple shortcode to output the Woocommerce cart count
<?php
// Add cart count shortcode [cart_count]
// =============================================================================
add_shortcode( 'cart_count', 'mb_cart_count' );
function mb_cart_count() {
if ( class_exists( 'WooCommerce' ) && function_exists( 'WC' ) ) { // Check for WooCommerce and WC() function.
if ( ! WC()->cart->is_empty() ) {
return (string) WC()->cart->get_cart_contents_count(); // Cast to string for consistency.
@michaelbourne
michaelbourne / woocommerce-body-class-cart-items.php
Created August 24, 2018 21:54
Add a body class when there are items in the Woocommerce cart
<?php
// Add a body class via the body_class filter in WP
// =============================================================================
add_filter( 'body_class', 'mb_body_class_for_cart_items' );
function mb_body_class_for_cart_items( $classes ) {
if( ! WC()->cart->is_empty() ){
$classes[] = 'cart-has-items';
}
return $classes;
@michaelbourne
michaelbourne / pro-cart-count.php
Last active February 25, 2019 22:58
Add a simple cart count to the Pro header builder cart elements
<?php
// Add cart count and value to Cart Element text in a Pro header
// =============================================================================
add_filter('woocommerce_add_to_cart_fragments', 'mb_cart_count_fragments', 10, 1);
function mb_cart_count_fragments($fragments) {
$count = WC()->cart->get_cart_contents_count();
$fragments['.cartdropdown .x-anchor-text-primary'] = '<span class="x-anchor-text-primary">' . $count . '</span>';
return $fragments;
}
@michaelbourne
michaelbourne / pro-cart-count-total.php
Last active August 24, 2018 21:55
Add the Woocommerce cart count and cart total to a cart element in the Pro header builder.
<?php
// Add cart count and value to Cart Element text in a Pro header
// =============================================================================
add_filter('woocommerce_add_to_cart_fragments', 'mb_cart_count_fragments', 10, 1);
function mb_cart_count_fragments($fragments) {
$count = WC()->cart->get_cart_contents_count();
$value = ($count == 0) ? '$0.00' : WC()->cart->get_cart_total();
$cart = ($count == 0) ? 'Cart' : 'Items: ' . $count;