Skip to content

Instantly share code, notes, and snippets.

View how-to-add-where-clause-to-wp-query-1.php
<?php
// Hook the filter before calling the query. Note that we are using an anonymous
// function here and saving a reference to the `$fn` variable for use further down.
add_filter( 'posts_where', $fn = function ( $where, WP_Query $wp_query ){
global $wpdb;
// Add a clause that ensures we only get posts with an ID greater than 200.
$where .= " AND {$wpdb->posts}.ID > 200";
View use-acf-field-as-fallback-for-excerpt.php
<?php
add_filter( 'get_the_excerpt', function ( $excerpt, $post ) {
if ( ! empty( $excerpt ) ) {
return $excerpt;
}
// On a specific post type, use an ACF field value as the excerpt.
if ( $post->post_type === 'my_custom_post_type' ) {
$excerpt = get_field( 'product_description', $post->ID );
View woocommerce-trim-zeros-from-price.php
<?php
// Need to trim zeros from a price on the fly?
$price = 49.00; // Can be numeric or a string.
$price = wc_trim_zeros( $price ); // === '49'
View disable-deprecation-notice-logging.php
<?php
// Disable deprecation notices so we can get a better idea of what's going on in our log.
// These hooks are all in wp-includes/functions.php.
// Note that these hooks don't stop WooCommerce from logging deprecation notices on AJAX
// or REST API calls as it makes its own calls to `error_log()` from within
// woocommerce/includes/wc-deprecated-functions.php.
add_filter( 'deprecated_constructor_trigger_error', '__return_false' );
add_filter( 'deprecated_function_trigger_error', '__return_false' );
add_filter( 'deprecated_file_trigger_error', '__return_false' );
View pluralise-string-based-on-variable-with-sprintf-and-acf.php
<?php
// Use an ACF field value to pull the number from the database.
$number_of_items = (int) get_field('my_numerical_field', $post_id );
$string = sprintf(
_n(
'%d item in stock',
'%d items in stock',
$number_of_items
@mishterk
mishterk / .gitignore
Last active Jan 20, 2022
This is a boilerplate for a .gitignore file when tracking at the WordPress wp-content directory level. It allows tracking of custom plugins and themes whilst ignoring everything else.
View .gitignore
.DS_Store
.sass-cache
node_modules
/upgrade
/cache
/uploads
/advanced-cache.php
/debug.log
/languages
/db.php
@mishterk
mishterk / override-woocommerce-menu-item-url.php
Last active Jan 20, 2022
These snippets work together to allow us to add custom WooCommerce account menu links to any URL. See https://hookturn.io/add-custom-woocommerce-account-menu-links/ for more detail.
View override-woocommerce-menu-item-url.php
<?php
add_filter( 'woocommerce_get_endpoint_url', function ( $url, $endpoint, $value, $permalink ) {
if ( $endpoint === 'my-courses' ) {
$url = home_url( 'my-courses' );
}
return $url;
}, 10, 4 );
View 0-woocommerce-snippets.md

WooCommerce Snippets

Just a handy collection of small snippets for customising WooCommerce.

View localize-script-variables-for-wordpress.php
<?php
add_action('wp_enqueue_scripts', function(){
// Assuming our previous example code is in a script enqueued with the handle `my-script` e.g;
// wp_enqueue_script( 'my-script', … );
// Localise an object containing our nonce:
wp_localize_script( 'my-script', 'myvars', [
'nonce' => wp_create_nonce( 'wp_rest' )
@mishterk
mishterk / 0-readme.md
Last active Jan 6, 2022
How to ensure post data from the Gravity Forms Advanced Post Creation Add-on is stored in ACF Custom Database Tables correctly.
View 0-readme.md

Using ACF Custom Database Tables in conjunction with the Gravity Forms Advanced Post Creation Add-on

The Gravity Forms post creation add-on doesn't appear to use ACF's API functions in order to save post meta on newly created posts. It's likely using core meta functions such as update_post_meta() instead of ACF's update_field() and in doing so, the ACF Custom Database Tables plugin doesn't have a chance to intercept the call and store the data in the appropriate custom database table.

In order to make the plugins work together, it's necessary to hook into the post creation process and save the field values using ACF's update_field() function. This will allow ACF Custom Database Tables to locate the appropriate custom DB table and store data there.

An example of this is provided in the below snippets and is based on the Gravity Forms documented approach [here](https://docs.gra