Skip to content

Instantly share code, notes, and snippets.

View younes-dro's full-sized avatar
🏡
Working from home

WordPress Dev Pro younes-dro

🏡
Working from home
View GitHub Profile
@younes-dro
younes-dro / gist:77b527d662cb809b574e01423667f457
Created November 6, 2020 02:49
Exclude the top level parents Taxonomy
add_filter('terms_clauses', function ( $pieces, $taxonomies, $args ) {
if (!isset($args['wpse_exclude_top']) || 1 !== $args['wpse_exclude_top']
)
return $pieces;
$pieces['where'] .= ' AND tt.parent > 0';
return $pieces;
}, 10, 3);
@younes-dro
younes-dro / gist:da8a3c06b3c48144ba89a256eabe6d17
Created June 19, 2020 15:59
Display the chosen shipping method label (and other related data, if needed) in cart page (or in checkout page)
foreach (WC()->session->get('shipping_for_package_0')['rates'] as $method_id => $rate) {
if (WC()->session->get('chosen_shipping_methods')[0] == $method_id) {
$rate_label = $rate->label; // The shipping method label name
$rate_cost_excl_tax = floatval($rate->cost); // The cost excluding tax
// The taxes cost
$rate_taxes = 0;
foreach ($rate->taxes as $rate_tax)
$rate_taxes += floatval($rate_tax);
// The cost including tax
@younes-dro
younes-dro / gist:8e952d5d16da874f3741b40387326c4f
Created January 29, 2020 19:28
Generate variable max-width for children based on loop
$columns: (
1: 100%,
2: 50%,
3: 33.33%,
4: 25%,
5: 20%,
6: 16.66%,
7: 14.28%,
8: 12.5%,
9: 11.11%
@younes-dro
younes-dro / gist:bce90e1b23a1e82f722315576e3eefd6
Created January 24, 2020 08:39
Custom Variable Product Price Range In WooCommerce
function wc_varb_price_range( $wcv_price, $product ) {
$prefix = sprintf('%s: ', __('From', 'wcvp_range'));
$wcv_reg_min_price = $product->get_variation_regular_price( 'min', true );
$wcv_min_sale_price = $product->get_variation_sale_price( 'min', true );
$wcv_max_price = $product->get_variation_price( 'max', true );
$wcv_min_price = $product->get_variation_price( 'min', true );
$wcv_price = ( $wcv_min_sale_price == $wcv_reg_min_price ) ?
@younes-dro
younes-dro / gist:a9f6ad5afdf39070b2463cafd5f45b82
Created January 23, 2020 09:25
Catch Errors when activating a Plugin on register_activation_hook()
function tl_save_error() {
update_option( 'plugin_error', ob_get_contents() );
}
add_action( 'activated_plugin', 'tl_save_error' );
/* Then to display the error message: */
echo get_option( 'plugin_error' );
@younes-dro
younes-dro / archive-product
Created January 18, 2020 16:52
Custom archive CPT
<?php
/**
* The template for displaying archive pages
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package WordPress
* @subpackage Twenty_Nineteen
* @since 1.0.0
*/
@younes-dro
younes-dro / sale-badge-woocommerce
Created January 17, 2020 04:25
Change the default Sale badge woocommerce
add_filter( 'woocommerce_sale_flash', 'wc_custom_replace_sale_text' );
function wc_custom_replace_sale_text( $html ) {
return str_replace( __( 'Sale!', 'woocommerce' ), __( 'My sale text!', 'woocommerce' ), $html );
}
@younes-dro
younes-dro / wp walker manu
Created January 15, 2020 01:37
Customize the wordpres menu with nav walker
// functions.php
class themeslug_walker_nav_menu extends Walker_Nav_Menu {
// add classes to ul sub-menus
function start_lvl(&$output, $depth) {
// depth dependent classes
$indent = ( $depth > 0 ? str_repeat("\t", $depth) : '' ); // code indent
$display_depth = ( $depth + 1); // because it counts the first submenu as 0
$classes = array(
'sub-menu',
@younes-dro
younes-dro / remove action from global $wp_filter
Created January 10, 2020 04:06
2 In cases like this the Wordpress adds a hash (unique id) to the function name and stores it in the global $wp_filter variable. So if you use remove_filter function nothing will happen
function my_remove_filter($tag, $function_name, $priority = 10){
global $wp_filter;
if( isset($wp_filter[$tag]->callbacks[$priority]) and !empty($wp_filter[$tag]->callbacks[$priority]) ){
$wp_filter[$tag]->callbacks[$priority] = array_filter($wp_filter[$tag]->callbacks[$priority], function($v, $k) use ($function_name){
return ( stripos($k, $function_name) === false );
@younes-dro
younes-dro / regex-3-lettre-3-number
Created January 9, 2020 05:37
Verify a pattern : 6 char lenght ( 3 string and 3 number)
if(isset($_POST['log']) && !empty($_POST['log']) && $_POST['log']==1){
$login = trim($_POST['login']);
$pw = trim($_POST['pw']);
if(strlen($login) == 6 && strlen($pw) == 6){
$n_log=0;