Skip to content

Instantly share code, notes, and snippets.

@nathaningram
nathaningram / sort-by-meta.php
Created March 28, 2024 18:10
Sort Kadence Post Grid by Post Meta
add_filter( 'kadence_blocks_pro_posts_grid_query_args', 'sort_by_date_post_grid_order',10, 2 );
function sort_by_date_post_grid_order( $args, $attributes ) {
if ( 'POST_TYPE_SLUG_HERE' === $args['post_type'] && isset($attributes['className']) && strpos($attributes['className'], 'BLOCK_CLASS_HERE') !== false ) {
$args['meta_key'] = 'start_date';
$args['orderby'] = 'meta_value';
$args['order'] = 'ASC';
}
return $args;
}
@nathaningram
nathaningram / cfsecurity.php
Last active April 14, 2024 09:22
Creating a Starter Site - Custom Functions - Security
<?php
/*
Plugin Name: Custom Security Functions
Plugin URI: https://nathaningram.com
Description: Security Hardening for Core WordPress
Version: 2023.11
Author: Nathan Ingram
Author URI: https://nathaningram.com
License: GPL2
*/
@nathaningram
nathaningram / append-me.php
Created August 30, 2023 16:27
Append WooCommerce Product SKU to Short Description (including multiple variations)
// ADD SKUS
// Runs on update - use Quick Edit
function ni_append_sku_to_short_description($post_id) {
if ('product' !== get_post_type($post_id)) {
return;
}
$product = wc_get_product($post_id);
@nathaningram
nathaningram / cleanup-blanks.php
Last active August 14, 2023 18:32
Remove 2+ empty lines in WooCommerce Product Descriptions
// Removes empty lines in Woo Product Long + Short Descr when 2 or more occur together
// Removes empty spaces at beginning or end of descriptions
// Fires on Post Update (works with Quick Edit)
add_action('save_post_product', 'ni_clean_up_product_content', 10, 3);
function ni_clean_up_product_content($post_ID, $post, $update) {
// Check if it's an update
if ($update) {
// Get the content and short description
@nathaningram
nathaningram / sue-magic-code-from-tanya.css
Created August 3, 2023 18:20
Align Buttons to Bottom of Post Grid in Kadence
/* looped archive pages align elements */
.custom-archive-loop-item {display: flex; flex-direction: column; background-color:#fff;}
.custom-archive-loop-item .kb-row-layout-wrap {flex: 1; display: flex; flex-direction: column;}
.custom-archive-loop-item .wp-block-kadence-accordion, .custom-archive-loop-item .wp-block-kadence-advancedbtn {margin-top: auto;}
@nathaningram
nathaningram / thirdtues.php
Created July 27, 2023 19:05
Third Tuesday from Office Hours
function find_next_third_tuesday() {
$date = new DateTime();
$date->modify('first day of this month');
while ($date->format('D') != 'Tue') {
$date->modify('next day');
}
$date->modify('+2 weeks');
if($date < new DateTime()) {
$date->modify('first day of next month');
while ($date->format('D') != 'Tue') {
@nathaningram
nathaningram / sanitize-woo-descriptions.php
Created July 18, 2023 20:12
Sanitizes HMTL from Woocommerce Product Descriptions upon post Update
// Sanitizes HMTL from Woocommerce Product Descriptions upon post Update
function sanitize_product_description( $post_id ) {
// check if post is a product
if ( 'product' === get_post_type( $post_id ) ) {
// get the post object
$product = wc_get_product( $post_id );
// sanitize the descriptions
foreach ( ['description', 'short_description'] as $desc_type ) {
@nathaningram
nathaningram / alpha-settings.php
Created May 2, 2023 17:28
Sort WP Admin Settings Menu Alphabetically
// Function to sort settings menu items alphabetically
function ni_sort_settings_menu_items_alphabetically() {
global $submenu;
// Check if the settings menu exists
if (isset($submenu['options-general.php']) && is_array($submenu['options-general.php'])) {
// Sort the items under the settings menu alphabetically
usort($submenu['options-general.php'], function ($a, $b) {
// Ensure both array elements have the 0-index set and are strings
if (isset($a[0]) && isset($b[0]) && is_string($a[0]) && is_string($b[0])) {
@nathaningram
nathaningram / hidecat.php
Created April 26, 2023 19:42
Hide Categories from WooCommerce Loop
// Add the code to your theme's functions.php file
function exclude_categories_from_shop_page( $query ) {
if ( ! is_admin() && is_shop() && $query->is_main_query() ) {
$tax_query = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array('unlimited-power', 'weapons'),
'operator' => 'NOT IN'
@nathaningram
nathaningram / noshow.php
Created April 25, 2023 19:47
No Show WooCommerce Tag
//Hide Products with tag noshow from Shop Page
add_action( 'woocommerce_product_query', 'exclude_noshow_products' );
function exclude_noshow_products( $query ) {
if ( $query->is_main_query() && is_shop() ) {
$tax_query = array(
array(
'taxonomy' => 'product_tag',
'field' => 'slug',