Skip to content

Instantly share code, notes, and snippets.

View willybahuaud's full-sized avatar

Willy Bahuaud willybahuaud

View GitHub Profile
@willybahuaud
willybahuaud / cart-to-quote.php
Created January 7, 2022 00:43
Yith Request a quote : add a button to transfer the cart content into the quote request form
<?php
add_action( 'admin_post_cart_to_quote', 'do_cart_to_quote' );
add_action( 'admin_post_nopriv_cart_to_quote', 'do_cart_to_quote' );
function w_cart_to_quote() {
$inst = YITH_Request_Quote::get_instance();
foreach( WC()->cart->get_cart() as $k => $data ) {
$product_id = $data['product_id'];
$obj = array(
'context' => 'frontend',
@willybahuaud
willybahuaud / adminconnect.php
Created January 7, 2022 00:29
Bootstrap to connect on WP without passwd
<?php
/* NE PAS OUBLIER DE SUPPRIMER CE FICHIER */
require_once( 'wp-load.php' );
require_once( './wp-admin/includes/user.php' );
if ( is_multisite() && function_exists('get_super_admins') && $admins = get_super_admins() ) {
$user = get_user_by( 'login', $admins[0] );
$user_id = $user->ID;
} else {
$admins = get_users( array( 'role' => 'administrator' ) );
@willybahuaud
willybahuaud / debug-hooks.php
Last active January 6, 2022 23:58
debug hooks and filters
<?php
function trace_hooks() {
global $wp_filter;
foreach ( $wp_filter[current_filter()]->callbacks as $f ) {
var_dump( current_filter(), $f );
}
}
add_action( 'all', 'trace_hooks' );
@willybahuaud
willybahuaud / duplicate-auto.php
Last active January 6, 2022 23:54
Automatic posts duplication across languages (polylang pro)
<?php
/**
* Cette action permet lors d'une sauvegarde d'article, de le dupliquer automatiquement dans toutes les langues
*/
add_action( 'pll_save_post', 'w_force_duplicate_post', 10, 3 );
function w_force_duplicate_post( $post_id, $post, $translations ) {
remove_filter( 'pll_save_post', 'w_force_duplicate_post' );
if ( 'event' != get_post_type( $post ) ) {
return false;
}
@willybahuaud
willybahuaud / filter.php
Last active January 6, 2022 23:46
filter wpastra custom layout conditions
<?php
add_filter( 'astra_display_on_list', 'w_astra_display_on_list_pro' );
function w_astra_display_on_list_pro( $rules ) {
// look at $rules array to see where you should add your filter
$rules['basic']['value']['pro_only'] = 'Pages professionnels';
return $rules;
}
function my_condition_is_ok_here( $id ) {
@willybahuaud
willybahuaud / exemple.php
Created March 11, 2016 09:49
This is a WordPress filter to retrieve only posts with attachments.
<?php
$with_photos = get_posts( array(
'post_type' => 'posts',
'suppress_filters' => false,
'only_with_attachments' => true,
'posts_per_page' => 10,
) );
@willybahuaud
willybahuaud / filter-search.php
Last active May 24, 2021 14:08
rechercher par mot clé api wordpress
<?php
add_filter('terms_clauses', 'w_term_search_clauses', 10, 3 );
function w_term_search_clauses( $clauses, $taxonomies, $args ) {
if ( ! empty( $taxonomies ) && in_array( 'product_cat', $taxonomies ) ) {
if ( ! empty( $args['search'] ) ) {
global $wpdb;
$like = '%' . $wpdb->esc_like( $args['search'] ) . '%';
$clauses['join'] .= " INNER JOIN {$wpdb->termmeta} AS tm ON t.term_id = tm.term_id";
$clauses['where'] = preg_replace( '/\)$/', $wpdb->prepare(" OR (tm.meta_key = 'analog_search' AND tm.meta_value LIKE %s))", $like ), $clauses['where'] );
@willybahuaud
willybahuaud / filters.php
Last active May 21, 2021 22:00
Facets filters by cookies
<?php
add_shortcode( 'menu-product-filter', 'w_product_filters' );
function w_product_filters() {
$cat = get_queried_object_id(); // si on est sur un ontenu, si c'est une archive on force la valeur (sans tirets)
$current = array();
if ( ! empty( $_COOKIE['w_filters'] ) ) {
$filters = json_decode( stripslashes( $_COOKIE['w_filters'] ) );
if ( ! empty( $filters->$cat ) ) {
@willybahuaud
willybahuaud / exemple.php
Last active August 10, 2020 13:49
wp_dropdown_category multiple
<?php
wp_dropdown_categories( array(
'taxonomy' => 'category',
'multiple' => true,
'walker' => new Willy_Walker_CategoryDropdown(),
'selected' => array( 10, 12 ), // selected terms…
'hide_empty' => false,
) );
@willybahuaud
willybahuaud / translate-taxonomy-slug.php
Last active February 13, 2020 18:38
Translate Taxonomy base slug wpml
<?php
add_action( 'init', 'register_my_taxo' );
function register_my_taxo() {
register_taxonomy( 'immo_cat', 'immobilier', array(
'hierarchical' => false,
'label' => 'Type de biens',
// je déclare le slug de taxo de manière à pouvoir le traduire
'rewrite' => array( 'slug' => icl_t('nebula', 'property-type-slug', 'type-bien-immobilier' ) ),
) );
}