Skip to content

Instantly share code, notes, and snippets.

@gaelbillon
gaelbillon / functions.php
Last active September 27, 2021 13:31
Shortcode to display the post content if it is not empty, otherwise it displays a fallback text.
// Shortcode : [post-content-with-fallback-text-if-empty]
function empty_content($str) {
return trim(str_replace(' ','',strip_tags($str))) == '';
}
function my_sc_content() {
@gaelbillon
gaelbillon / functions.php
Last active September 27, 2021 11:52
Wordpress / ACF : Set gallery image from featured image. If featured image is set and gallery field is empty.
add_action( 'save_post', 'set_first_gallery_image_from_featured_image' );
function set_first_gallery_image_from_featured_image() {
$post = get_post();
$featured_image = get_the_post_thumbnail($post->ID);
$acf_gallery = get_field('images', false, false); // Change images to the name of the ACF gallery field you want to update
$attachments_ids = [ 0 => get_post_thumbnail_id($post->ID) ];
if ( $featured_image && !$acf_gallery ) {
@gaelbillon
gaelbillon / style.css
Last active June 16, 2021 11:02
Set responsive min-height on header logo image (on Avada Wordpress theme) to avoid layout shift (CLS) and increase Core Web Vitals
/*
If uploading new logo image and changing logo image aspect ratio
-> also change --logo-aspect-ratio variables below
Tried to calculate aspect ratio automatically with :
--logo-aspect-ratio: calc(var(--logo-width)/6);
But it does not work
*/
@media screen and (max-width: 420px) {
.fusion-logo img {
--cumulated-containers-padding: 60px;
@gaelbillon
gaelbillon / functions.php
Created December 1, 2020 23:03
Hide empty categories in menus
//Hide empty categories in menus
add_filter( 'wp_get_nav_menu_items', 'nav_remove_empty_category_menu_item',10, 3 );
function nav_remove_empty_category_menu_item ( $items, $menu, $args ) {
if ( ! is_admin() ) {
global $wpdb;
$nopost = $wpdb->get_col( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE count = 0" );
foreach ( $items as $key => $item ) {
if ( ( 'taxonomy' == $item->type ) && ( in_array( $item->object_id, $nopost ) ) ) {
unset( $items[$key] );
}
@gaelbillon
gaelbillon / functions.php
Created November 18, 2020 13:35
Displays a message "Delivery is free for orders of 6 items or more!" next to add to cart button when quantity goes over 1. Only for user who do not have a membership plan. Only for Europe (Only work on Cloudways)
// Need enable Cloudways geolocation
// Displays a message "Delivery is free for orders of 6 items or more!" next to add to cart button when quantity goes over 1. Only for user who do not have a membership plan. Only for Europe
add_action( 'woocommerce_after_add_to_cart_button', 'suggest_box_when_qty_over_six' );
function suggest_box_when_qty_over_six() {
if (!class_exists('WooCommerce')) return;
if (is_product()) {
$productId = get_the_ID();
$product = wc_get_product( $productId );
// If not in Europe -> abort
@gaelbillon
gaelbillon / functions.php
Created November 18, 2020 13:29
Get user continent with Woocommerce
// Need enable geolocation in Woocomerce settings
// Check if user is in Europe
function get_user_geo_continent () {
$location = WC_Geolocation::geolocate_ip();
$country = $location['country']; // Get the country code
$WC_Countries = new WC_Countries(); // Get WC_Countries instance object
$continent = $WC_Countries->get_continent_code_for_country( $country ); // Get continent
return $continent;
}
@gaelbillon
gaelbillon / functions.php
Created November 18, 2020 13:26
Wordpress custom admin notice
/*************************/
/* Custom admin notice */
/*************************/
// Add temporary dashboard message about plugin_name for team
function plugin_name_admin_notices() {
echo '<div class="notice notice-warning is-dismissible"><p><strong>/!\ @website team /!\</strong>: Please do not use plugin_name for the moment. It is not fully configurated yet. Thanks</p></div>';
}
add_action('admin_notices', 'plugin_name_admin_notices');
@gaelbillon
gaelbillon / functions.php
Created November 18, 2020 13:21
Forbid free shipping (with plugin Advanced Free Shipping ) for Woocomerce Membership members
// Forbid free shipping for members on any Woocomerce membership plan plan
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
function filter_woocommerce_package_rates( $rates, $package ) {
if ( function_exists( 'wc_memberships' ) ) {
if ( wc_memberships_is_user_member() ) {
foreach ( $rates as $rate_key => $rate ) {
if ( 'advanced_free_shipping' === $rate->method_id ) {
unset($rates[$rate_key]);
return $rates;
}
@gaelbillon
gaelbillon / ContentSingleExportForm.php
Last active July 29, 2020 07:33
Modification of Drupal [Content Sync](https://www.drupal.org/project/content_sync) module. It replace the single item export tab. On which it is heavily based. When an an entity is selected, it can be exported as a tar file. Along with the entity's dependencies and attached medias saved as files (not base64) with extensions. Entity type and bund…
/*
* Modification of Drupal [Content Sync](https://www.drupal.org/project/content_sync) module.
* It replace the single item export tab. On which it is heavily based. When an an entity is
* selected, it can be exported as a tar file. Along with the entity's dependencies and attached
* medias saved as files (not base64) with extensions. Entity type and bundle/sub-type are
* hardcoded and are set to "node" and "poi_collection". Quick and dirty but does the job.
*/
<?php
@gaelbillon
gaelbillon / functions.php
Created May 8, 2020 11:55
Wordpress with Avada Theme : Add login/logout link and my account link in main menu.
// functions.php
// Add login/logout link to main menu
// Displays as :
// Bonjour username
// (not username ? Logout)
add_action( 'avada_after_header_wrapper', 'add_loginout_link_in_header' );
function add_loginout_link_in_header( ) {
global $current_user; wp_get_current_user();
$display_name = esc_html($current_user->display_name);