Skip to content

Instantly share code, notes, and snippets.

Avatar

Willy Bahuaud willybahuaud

View GitHub Profile
View order-by-taxonomy.php
<?php
add_action( 'pre_get_posts', 'w_filter_query' );
function w_filter_query( $q ) {
if ( ! is_admin() && $q->is_main_query() && is_post_type_archive( 'recipe' ) ) {
$q->set( 'posts_per_page', -1 );
$q->set( 'need_special_order', 'type-recette' );
}
}
add_filter('posts_clauses', 'w_orderby_tax_clauses', 10, 2 );
@willybahuaud
willybahuaud / filtre.php
Last active Apr 3, 2018
Préfixer les articles par l’URL du page for posts (avec WPML)
View filtre.php
<?php
add_filter( 'post_link', 'w_post_link', 10, 3 );
function w_post_link( $url, $post, $leavename=false ) {
if ( $post->post_type == 'post' ) {
global $sitepress;
$lang = apply_filters( 'wpml_post_language_details', 'fr', $post->ID );
$base = get_permalink( icl_object_id( get_option( 'page_for_posts' ), 'page', true, $lang['language_code'] ) );
$url = str_replace( $sitepress->language_url( $lang['language_code'] ), $base, $url );
}
View filtre-typo.php
<?php
add_filter( 'the_title', 'willy_insert_missing_unsecable' );
add_filter( 'the_content', 'willy_insert_missing_unsecable' );
function willy_insert_missing_unsecable( $text ) {
$patterns = array(
'« ' => '«&nbsp;',
' »' => '&nbsp;»',
' :' => '&nbsp;:',
' !' => '&nbsp;!',
@willybahuaud
willybahuaud / filter-menu-item-by-user-status.php
Last active Oct 16, 2016
Filtrer les éléments d’un menu selon que l’utilisateur est connecté
View filter-menu-item-by-user-status.php
<?php
add_filter( 'wp_nav_menu_objects', 'willy_filter_wp_nav_menu_object_by_user_status' );
function willy_filter_wp_nav_menu_object_by_user_status( $items ) {
if ( ! is_admin() ) {
foreach ( $items as $k => $item ) {
if ( is_array( $item->classes )
&& ( ( in_array( 'logged-only', $item->classes ) && ! is_user_logged_in() )
|| ( in_array( 'not-logged-only', $item->classes ) && is_user_logged_in() ) ) ) {
unset( $items[ $k ] );
@willybahuaud
willybahuaud / exemple.php
Last active Aug 10, 2020
wp_dropdown_category multiple
View exemple.php
<?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 / functions.php
Last active Oct 7, 2016
faire un mur d’adhérents
View functions.php
<?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 Sep 22, 2016
Update post_modified on _bbp_last_active_time update, to improve order efficiency
View bbpress-improvment.php
<?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 Sep 11, 2020
Set a default term for custom taxonomy and custom post type
View default-term.php
<?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 Aug 30, 2016
Launch Prism for heavy pages
View pastacode-async.php
<?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 Oct 18, 2016
Set a default thumbnail
View default-thumbail.php
<?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 );
You can’t perform that action at this time.