Skip to content

Instantly share code, notes, and snippets.

@grola
grola / config.php
Last active January 21, 2018 17:24
Cloudflare Flexible SSL for Wordpress
$https_options = array( 'HTTP_CF_VISITOR', 'HTTP_X_FORWARDED_PROTO' );
foreach ( $https_options as $https_option ) {
if ( isset( $_SERVER[ $https_option ] )
&& ( strpos( $_SERVER[ $https_option ], 'https' ) !== false )
) {
$_SERVER[ 'HTTPS' ] = 'on';
}
}
@grola
grola / functions.php
Created January 19, 2018 16:37
YOAST https:// cannonical URLs
function swp_wpseo_canonical_ssl( $url ) {
$url = str_replace( 'http://', 'https://', $url );
return $url;
}
add_filter( 'wpseo_canonical', 'swp_wpseo_canonical_ssl' );
@grola
grola / functions.php
Last active January 19, 2018 17:27
Leading zeros in WooCommerce order number
add_filter( 'woocommerce_order_number', 'swp_woocommerce_order_number_leading_zeros', 10 );
function swp_woocommerce_order_number_leading_zeros( $order_id ) {
$max_length = 8;
$new_order_id = str_pad( $order_id, $max_length, '0', STR_PAD_LEFT );
return $new_order_id;
}
@grola
grola / functions.php
Last active January 19, 2018 19:23
Shortcodes in Wordpress Text Widget
add_filter( 'widget_text', 'do_shortcode' );
@grola
grola / functions.php
Created January 20, 2018 10:43
Remove generator meta from header
remove_action('wp_head', 'wp_generator');
@grola
grola / functions.php
Last active January 22, 2018 10:58
Display WooCommerce variable product price
add_filter( 'woocommerce_variable_price_html', 'studiowp_woocommerce_variable_price_html', 10, 2 );
function studiowp_woocommerce_variable_price_html( $price, $product ) {
if ( !is_singular( 'product' ) ) {
$prices = $product->get_variation_prices( true );
if ( empty( $prices['price'] ) ) {
$price = apply_filters( 'woocommerce_variable_empty_price_html', '', $this );
} else {
$prefix = 'From ';
$suffix = '';
@grola
grola / functions.php
Last active January 28, 2018 14:53
Remove WooCommerce sorting options
<?php
$studiowp_remove_orderby = array( 'rating', 'popularity', 'price', 'price-desc' );
add_action( 'init', 'studiowp_init_orderby' );
function studiowp_init_orderby() {
global $studiowp_remove_orderby;
if ( !is_admin() && isset( $_GET['orderby'] ) ) {
if ( in_array( $_GET['orderby'], $studiowp_remove_orderby ) ) {
wp_redirect( home_url() );
@grola
grola / functions.php
Last active January 22, 2018 20:28
Variable product price
add_filter( 'woocommerce_variable_price_html', 'studiowp_woocommerce_variable_price_html', 10, 2 );
function studiowp_woocommerce_variable_price_html( $price, $product ) {
if ( !is_singular( 'product' ) ) {
$prices = $product->get_variation_prices( true );
if ( empty( $prices['price'] ) ) {
$price = apply_filters( 'woocommerce_variable_empty_price_html', '', $this );
} else {
$prefix = 'From ';
$suffix = '';
@grola
grola / functions.php
Last active January 23, 2018 19:17
Display additional content after "Add to cart" button on product page for declared categories
add_action( 'woocommerce_after_add_to_cart_button', 'studiowp_woocommerce_after_add_to_cart_button' );
function studiowp_woocommerce_after_add_to_cart_button() {
global $product;
$my_categories = array( 15, 16 );
$is_my_category = false;
$product_categories = $product->get_category_ids();
foreach ( $my_categories as $my_category ) {
if ( in_array( $my_category, $product_categories ) ) {
$is_my_category = true;
}
@grola
grola / functions.php
Created January 24, 2018 10:27
Remove downloads from My Account page in Woocommerce
add_action( 'woocommerce_account_menu_items', 'studiowp_woocommerce_account_menu_items' );
function studiowp_woocommerce_account_menu_items( $items ) {
unset( $items['downloads'] );
return $items;
}