Skip to content

Instantly share code, notes, and snippets.

View willybahuaud's full-sized avatar

Willy Bahuaud willybahuaud

View GitHub Profile
@willybahuaud
willybahuaud / functions.php
Last active October 7, 2016 13:56
faire un mur d’adhérents
<?php
function get_adherents() {
if ( function_exists( 'wcs_get_subscriptions' ) ) {
if ( ! $adherents_wpfr = get_transient( 'adherents_wpfr' ) ) {
$subscriptions_pro = wcs_get_subscriptions( array(
'subscriptions_per_page' => 10,
'variation_id' => 11,
'subscription_status' => 'active',
));
@willybahuaud
willybahuaud / bbpress-improvment.php
Created September 22, 2016 21:12
Update post_modified on _bbp_last_active_time update, to improve order efficiency
<?php
add_action( 'updated_post_meta', 'update_post_modified_on__bbp_last_active_time_update', 10, 4 );
function update_post_modified_on__bbp_last_active_time_update( $meta_id, $object_id, $meta_key, $_meta_value ) {
if ( '_bbp_last_active_time' == $meta_key ) {
$obj = get_post( $object_id );
if ( 'topic' == $obj->post_type ) {
$obj->post_modified = $_meta_value;
$obj->post_modified_gmt = gmdate('Y-m-d H:i:s', strtotime( $_meta_value ) );
wp_insert_post( $obj );
}
@willybahuaud
willybahuaud / default-term.php
Last active October 17, 2022 16:45
Set a default term for custom taxonomy and custom post type
<?php
add_action( 'wp_insert_post', 'willy_set_default_term', 10, 3 );
function willy_set_default_term( $post_id, $post, $update ) {
if ( 'cpt' == $post->post_type ) { // replace `cpt` with your custom post type slug
/**
* Replace `taxo` by the taxonomy slug you want to control/set
* … and replace `default-term` by the default term slug (or name)
* (or you can use a `get_option( 'my-default-term', 'default term' )` option instead, which is much better)
*/
if ( empty( wp_get_post_terms( $post_id, 'taxo' ) ) ) {
@willybahuaud
willybahuaud / pastacode-async.php
Created August 30, 2016 07:49
Launch Prism for heavy pages
<?php
// add data-manual attribute, then call Prism in async way
add_filter( 'script_loader_tag', 'pastacode_async', 10, 3 );
function pastacode_async ( $tag, $handle, $src ) {
if ( 'prismjs' == $handle ) {
$tag = str_replace( '<script ', '<script data-manual ', $tag );
$tag .= '<script>jQuery(document).ready(function($){Prism.highlightAll(true);});</script>';
}
return $tag;
}
@willybahuaud
willybahuaud / default-thumbail.php
Last active October 18, 2016 05:11
Set a default thumbnail
<?php
// To work, this function require to bypass has_post_thumbnail() test into templates…
add_filter( 'post_thumbnail_html', 'default_post_thumbnail_html', 10, 5 );
function default_post_thumbnail_html( $html, $post_ID, $post_thumbnail_id, $size, $attr ) {
// If no thumbnail is sendback by get_post_thumbail(), return a default image
if ( '' == $html && in_array( get_post_type( $post_ID ), array( 'post', 'page' ) ) ) {
// Get a default thumbnail ID into a list
$thumbs = get_option( 'options_default_thumbnails', array() );
$thumb = $thumbs[ array_rand( $thumbs ) ];
remove_filter( 'post_thumbnail_html', 'default_post_thumbnail_html', 10, 5 );
<?php
/**
* Whiteliste shortcode
*/
add_filter( 'bbp_get_reply_content', 'strip_shortcodes_whitelist', 9 );
add_filter( 'bbp_get_topic_content', 'strip_shortcodes_whitelist', 9 );
function strip_shortcodes_whitelist( $content ) {
global $shortcode_tags;
@willybahuaud
willybahuaud / functions.php
Created August 26, 2016 14:19
Gestion d'une archive par date (passés/à venir)
<?php
add_filter( 'query_vars', 'agenda_add_query_vars' );
function agenda_add_query_vars( $vars ){
$vars[] = "passe";
return $vars;
}
add_filter( 'pre_get_posts', 'agenda_date_pre_get_posts' );
function agenda_date_pre_get_posts( $q ) {
if ( ! is_admin()
@willybahuaud
willybahuaud / bbpress-search-functions.php
Created August 11, 2016 19:43
Recherche avancée avec bbPress
<?php
/**
* Hook on pre_get_posts to set new query vars
*/
add_filter( 'pre_get_posts', 'willy_dump_search' );
function willy_dump_search( $q ) {
if ( isset( $_GET['search-bb-request'] )
&& $q->is_main_query()
&& $_GET['search-bb-request'] == 1) {
$text = $q->query_vars['s'];
@willybahuaud
willybahuaud / filtres.php
Created May 12, 2016 07:45
Grouper les posts par terms dans un wp_query existant
<?php
add_filter( 'pre_get_posts', 'willy_pre_get_posts' );
function willy_pre_get_posts( $q ) {
$moncpt = 'post';
$mataxo = 'category';
if ( $q->is_main_query() && ! is_admin() && is_archive( $moncpt ) ) {
$q->set( 'tax_query', array(
array(
'taxonomy' => $mataxo,
'terms' => wp_list_pluck( get_terms( $mataxo ), 'term_id' ),
@willybahuaud
willybahuaud / functions-woocommerce.php
Created April 25, 2016 13:20
Moteur de recherche woocommerce
<?php
add_action( 'woocommerce_before_main_content', 'willy_woocommerce_before_main_content_thumbnails', 9 );
function willy_woocommerce_before_main_content_thumbnails() {
if ( is_tax( 'product_cat' ) ) {
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
if ( $thumbnail_id ) {
echo '<div class="image-single thumbnail-home big-thumb">';