Skip to content

Instantly share code, notes, and snippets.

/*
* Since Google Analytics only allows integers as event values, Monster Insights multiplies the value by 100.
* This makes it difficult to use sale events for goal tracking and adding their values to other goals.
* This function adds a custom event with the sales value rounded, which should be close enough in most cases.
*
* Requires WooCommerce (version >= 3.0) and Monster Insights (free or premium) to be active.
*/
add_filter( 'monsterinsights_mp_api_call', 'gbol_add_rounded_sale_event' );
function gbol_add_rounded_sale_event( $body ) {
/* Allow Easy Testimonials and Visual Composer shortcodes in excerpts */
add_filter( 'strip_shortcodes_tagnames', 'gbol_allow_shortcodes' );
function gbol_allow_shortcodes( $tags_to_remove ) {
$allowed_shortcodes = array( 'random_testimonial', 'single_testimonial', 'testimonials', 'testimonials_cycle', 'testimonials_grid',
'vc_row', 'vc_column', 'vc_column_text' );
foreach( $allowed_shortcodes as $tag ) {
if ( $key = array_search( $tag, $tags_to_remove ) ) {
@galbaras
galbaras / insert_ad_after_block.php
Last active November 22, 2017 01:55
Insert Google ad after specific top-level text block in WordPress post
/*
* This code is for WordPress and should be used in functions.php of the active theme.
*
* Before using it, replace "ADVERTISER ID" with your own advertiser ID and "SLOT ID" with your own slot ID
* (you can get both in Google AdSense). For best results, use a reponsive ad unit.
*
* If your posts contain "pre", "noscript" or some other type of HTML block, add them to $block_tags.
*
* You can change the insertion point by changing $after_block.
*
@galbaras
galbaras / functions.php
Created September 1, 2015 00:46
Add a WooCommerce attribute (minimum quantity) to a Contact Form 7 form. Can be easily adapted to other attributes, as well as product meta, terms, etc
// Save product_quantity_field.php in your active theme directory, then
// add this line to your functions.php file to include it in the theme
include( get_stylesheet_directory() . '/product_quantity_field.php' );
@galbaras
galbaras / no_urls_allowed.php
Last active August 8, 2022 23:06
Function to invalidate Contact Form 7 input, unless the fields is of type "url" or has "url" in its name, i.e. it is meant for URLs
add_filter( 'wpcf7_validate_text', 'no_urls_allowed', 10, 3 );
add_filter( 'wpcf7_validate_text*', 'no_urls_allowed', 10, 3 );
add_filter( 'wpcf7_validate_textarea', 'no_urls_allowed', 10, 3 );
add_filter( 'wpcf7_validate_textarea*', 'no_urls_allowed', 10, 3 );
function no_urls_allowed( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );
$type = $tag->type;
$name = $tag->name;
@galbaras
galbaras / adminbartoggle
Last active August 29, 2015 14:13
Toggling the display of the WordPress Admin Bar (wpadminbar)
The WordPress admin bar can sometimes get in the way of checking what a site looks like
while being logged in, so it's convenient to hide it occasionally.
To do this, we need a CSS class and some jQuery code, which toggles the admin bar display
when pressing Shift-A.
The CSS can be added to the theme's normal stylesheet. The PHP/jQuery code can be added
into footer.php or included within an action in functions.php.
* The code was adapted from the Bottom Admin Bar plugin.
@galbaras
galbaras / gist:efec7adc266db3b40699
Created July 2, 2014 02:40
Embedded video notice for emailed feeds
function add_rss_video_embed_notice( $html, $url, $attr, $post_id ) {
if (!is_feed()) { // No need to change anything
return $html;
}
$permalink = get_permalink( $post_id );
$notice = '<br /><small>If you cannot see a video, <a href="' . $permalink . '">view this post online</a></small>';
return $html . $notice;
}
@galbaras
galbaras / gist:6ad566fa9fd7dd8de1d5
Last active May 29, 2019 10:34
Make special WooCommerce pages "noindex,follow" when using Yoast SEO
function woo_seo_noindex_special_pages () {
global $post;
$woocommerce_pages = array('cart', 'checkout', 'order-received', 'order-tracking',
'my-account', 'logout', 'lost-password', 'mijireh-secure-checkout');
$slug = get_post($post)->post_name;
if (in_array($slug, $woocommerce_pages)) {
echo '<meta name="robots" content="noindex,follow"/>' . "\n";
}