Skip to content

Instantly share code, notes, and snippets.

Avatar

Christian Behrends cubehrends

View GitHub Profile
View functions.php
<?php
/* Shortcode to Display Post Tags in UnorderedList /w specific CSS-Class and Link to Archive
=============================================================== */
function display_tags( $atts, $content = null ) {
$post_tags = get_the_tags();
if ( $post_tags ) {
$html = '<ul class="tag-list">';
foreach( $post_tags as $tag ) {
$html .= '<li><a class="' . $tag->slug . '" href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a></li>';
View functions.php
<?php
/* adding og:image for Facebook shares
=============================================================== */
add_action( 'wp_head', function(){
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' );
if ($featured_image) {
echo '<meta property="og:image" content="'.$featured_image[0].'">';
}
@cubehrends
cubehrends / functions.php
Last active Jun 17, 2019
Addin Brand Name to Item Title in WooCommerce Product Lists
View functions.php
<?php
add_action('woocommerce_shop_loop_item_title','brands_to_item_title', 11);
function brands_to_item_title( ) {
$brands = wp_get_post_terms( get_the_ID(), 'product_brand' );
if ( count( $brands ) > 0 ) {
foreach( $brands as $brand )
echo '<span class="brand-name">' . $brand->name . '</span>';
}
@cubehrends
cubehrends / functions.php
Last active Jun 12, 2019
Adding Brand Archive Link to Single Product Summary in WooCommerce
View functions.php
<?php
add_action( 'woocommerce_single_product_summary', 'add_brand_to_single_product_summary', 6 );
function add_brand_to_single_product_summary() {
global $post;
if ( ! $post_id && ! $post )
return;
if ( ! $post_id )
$post_id = $post->ID;
@cubehrends
cubehrends / functions.php
Last active Jun 7, 2020
301 Moved Permanently via WordPress Child Theme functions.php
View functions.php
<?php
function redirect_to_url() {
switch ( $_SERVER['REQUEST_URI'] ) {
case '/an-old-uri/':
$redirect_to = '/the-new-uri/';
break;
case '/another-old-uri/':
$redirect_to = '/another-new-uri/';
break;
@cubehrends
cubehrends / header.php
Last active Nov 7, 2018
301 Moved Permanently vie PHP header
View header.php
<?php
function redirectTo( $location ) {
header( 'HTTP/1.1 301 Moved Permanently' );
header( 'Location: ' . $location );
header( 'Connection: close' );
}
switch ( $_SERVER['REQUEST_URI'] ) {
case '/post/an-uri-on-old-domain':
redirectTo( 'https://new-domain.com/a-new-uri/' );
break;
@cubehrends
cubehrends / functions.php
Last active Sep 28, 2018
WordPress GDPR Comments w/ Polylang via Child Theme
View functions.php
<?php
function register_pll_strings() {
if( !function_exists( 'pll_register_string' ) ) return; // go away if polylang not active
pll_register_string('Policy Accept', 'I read your', 'CommentForm');
pll_register_string('Privacy Policy', 'Privacy Policy', 'CommentForm');
pll_register_string('Policy Page', 'privacy', 'CommentForm');
pll_register_string('Error', 'ERROR', 'CommentForm');
View lang.php
<?php
if ( $_GET ) {
$lang = set_lang( key($_GET) );
} else if ( isset($_COOKIE['lang']) ) {
$lang = $_COOKIE['lang'];
} else {
$lang = set_lang( substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) );
}
function set_lang( $l ) {
@cubehrends
cubehrends / functions.php
Last active May 18, 2018
How to customize the Comment Form Cookies Consent Label in WordPress 4.9.6
View functions.php
<?php
/* the code below goes into your child themes functions.php
=============================================================== */
function customize_comment_form_cookies_consent_label( $translated, $original, $domain ) {
$strings = array(
'Save my name, email, and website in this browser for the next time I comment.' => '=> YOUR CUSTOMIZED TEXT GOES HERE <='
);
if ( ! empty( $strings[$original] ) ) {
@cubehrends
cubehrends / script.js
Last active May 11, 2018
GDPR Consented Loading from GitHub for WordPress Page Builders like Divi, Elementor, Gutenberg etc. Learn more at https://webdevtrust.com/gdpr-consent-loading-page-builder/
View script.js
<script id="gist0">
(function( $ ) {
if ( has_consent( 'github' ) ){
$.getJSON( 'https://gist.github.com/cubehrends/1f316da01358e85f3592742327f0d83a.json?callback=?', function( data ) {
$('#gist0').parent().html( $( data.div ) ).append('<link rel="stylesheet" href="' + data.stylesheet + '">')
} )
} else {
You can’t perform that action at this time.