Skip to content

Instantly share code, notes, and snippets.

View gnikolopoulos's full-sized avatar
🏠
Working from home

George Nikolopoulos gnikolopoulos

🏠
Working from home
View GitHub Profile
@gnikolopoulos
gnikolopoulos / functions.php
Last active August 27, 2015 10:51
[Wordpress] Remove query strings from javascript and css files
<?php
function remove_cssjs_querystring( $src ) {
if( strpos( $src, '?rev=' ) ) // copy/paste this line and the next one to take away what you want from the end of your css/js
$src = remove_query_arg( 'rev', $src );
if( strpos( $src, '?ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_querystring', 10, 2 );
@gnikolopoulos
gnikolopoulos / functions.php
Created August 27, 2015 10:47
Add Defer and Async Attributes to Render Blocking Javascript in WordPress
<?php
function defer_js_async($tag){
// scripts to defer.
$scripts_to_defer = array('script-name1.js', 'script-name2.js', 'script-name3.js');
// scripts to async.
$scripts_to_async = array('script-name1.js', 'script-name2.js', 'script-name3.js');
foreach($scripts_to_defer as $defer_script){
@gnikolopoulos
gnikolopoulos / combine.php
Created August 27, 2015 10:49
[Wordpress] Combile all your CSS into a single file
<?php
header('Content-type: text/css');
ob_start("compress");
function compress( $minify )
{
/* remove comments */
$minify = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $minify );
@gnikolopoulos
gnikolopoulos / functions.php
Created August 27, 2015 10:51
[Wordpress] Different homepage for logged in users
<?php
function switch_homepage() {
if ( is_user_logged_in() ) {
$page = 2516; // for logged in users
update_option( 'page_on_front', $page );
update_option( 'show_on_front', 'page' );
} else {
$page = 2; // for logged out users
update_option( 'page_on_front', $page );
@gnikolopoulos
gnikolopoulos / functions.php
Last active January 10, 2020 12:54
[Woocommerce] Rule Based add-to-cart functions
/**
* Gets Variation ID if available otherwise it will get the Product ID
* @param $product Product
* @param bool $check_variations Whether or not to check for variation IDs
* @return mixed
*/
function get_id_from_product( $product, $check_variations = false ) {
if( $check_variations ) {
return ( isset( $product['variation_id'] )
&& ! empty( $product['variation_id'])
@gnikolopoulos
gnikolopoulos / functions.php
Last active September 14, 2015 17:37
[WooCommerce] Add surcharge to your cart / checkout
add_action( 'woocommerce_cart_calculate_fees','woocommerce_surcharge' );
function woocommerce_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.01;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
@gnikolopoulos
gnikolopoulos / function.php
Created September 14, 2015 17:36
[Wordpress] Make featured image required
add_action('save_post', 'custom_check_thumbnail');
add_action('admin_notices', 'custom_thumbnail_error');
function custom_check_thumbnail($post_id) {
// change to any custom post type
if(get_post_type($post_id) != 'post')
return;
if ( !has_post_thumbnail( $post_id ) ) {
// set a transient to show the users an admin message
@gnikolopoulos
gnikolopoulos / functions.php
Last active August 7, 2018 23:25
[Woocommerce] Add video as featured image for products
<?php
// Display product video instead of image/slider
function woocommerce_show_product_images() {
// Get video and display
if ( $video = get_post_meta( get_the_ID(), 'product_post_video', true ) ) {
// Sanitize video URL
$video = esc_url( $video );
@gnikolopoulos
gnikolopoulos / function.php
Created January 7, 2016 16:22
[Wordpress] Responsive embed videos without plugins
<?php
/* Add responsive container to embeds
/* ------------------------------------ */
function id_embed_html( $html ) {
return '<div class="video-container">' . $html . '</div>';
}
add_filter( 'embed_oembed_html', 'id_embed_html', 10, 3 );
add_filter( 'video_embed_html', 'id_embed_html' ); // Jetpack
@gnikolopoulos
gnikolopoulos / functions.php
Created January 19, 2016 18:45
[Wordpress] Remove p and br tags from custom shortcodes
<?php
if( !function_exists('id_fix_shortcodes') ) {
function id_fix_shortcodes($content){
$array = array (
'<p>[' => '[',
']</p>' => ']',
']<br />' => ']'
);
$content = strtr($content, $array);