Skip to content

Instantly share code, notes, and snippets.

View brandonkramer's full-sized avatar
🏠
Working from home

Brandon Kramer brandonkramer

🏠
Working from home
View GitHub Profile
@brandonkramer
brandonkramer / woo-product-attributes-bulk-modifier.php
Created March 2, 2020 14:30 — forked from birgire/woo-product-attributes-bulk-modifier.php
WordPress: Woocommerce Product Attributes - Bulk Modifier ( from custom to taxonomy attributes)
<?php
/**
* Plugin Name: Product Attributes - Bulk Modifier
* Description: Bulk update 'custom product attributes' to 'taxonomy product attributes'. Supports the GET variables, like: wpq_mode=run&wpq_from=colour&wpq_to=pa_colour&wpq_ppp=10&wpq_offset=0&wpq_post_type=product&wpq_post_status=any. WARNING: Backup DB first!!!
* Plugin Author: birgire
* Plugin URI: https://gist.github.com/birgire/0ed300ae4436fcaf508c
* Version: 0.0.2
*/
/**
@brandonkramer
brandonkramer / lando.yml
Last active June 21, 2024 13:44
Lando config file for WordPress with BrowserSync, MailHog, PHPMyAdmin, NGINX and XDebug
# Replace all {project} with project name
name: wp-{project}
recipe: wordpress
config:
php: '7.4'
via: nginx # Nginx or apache
webroot: .
database: mariadb # Mariadb or mysql
xdebug: true
config:
@brandonkramer
brandonkramer / .env
Last active May 16, 2020 21:16
Lando config file for Laravel with NGINX, PHP 7.4, XDebug, Node, NPM, MailHog and PHPMyAdmin
// In your .env file change MAIL configurations to the following to make mailhog work
MAIL_MAILER=smtp
MAIL_HOST=sendmailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=test@test.nl
MAIL_FROM_NAME="${APP_NAME}"
@brandonkramer
brandonkramer / dotenv.php
Last active January 31, 2022 11:37
PHP read .env file into an readable array to load system environment configuration $_ENV, $_SERVER, env, dotenv, getenv
<?php
try {
/**
* Throw exception if not found or is unreadable
*/
if ( !file_exists( __DIR__ . '/.env' ) || !is_readable( __DIR__ . '/.env' ) ) {
throw new Exception( 'The .env file is not found or unreadable.' );
}
/**
* Get the .env file, ignore new and empty lines.
@brandonkramer
brandonkramer / woocommerce-move-input-fields-labels-for-pseudo-classes.php
Last active September 1, 2021 16:47
Move labels after input fields in WooCommerce to make pseudo class possible to create custom checkboxes in CSS. Input fields are placed in labels by default by WooCommerce. We want to make sure the label is placed after the input field so :before pseudo class for +checked labels can be attached to create custom checkboxes in CSS.
<?php
add_filter( 'woocommerce_form_field_checkbox', 'changeCheckboxFormField', 10, 4 );
/**
* Input fields are placed in labels by default by WooCommerce.
* We want to make sure the label is placed after the input field so :before pseudo class for +checked labels
* can be attached to create custom checkboxes in CSS. For this to happen within the filter,
* a part of the function from woocommerce_form_field needs to be copied so we can
* re-create the Field HTML with the necessary variables and change the order in the $field
@brandonkramer
brandonkramer / remove-billing-word-from-validation-message.php
Last active February 15, 2024 16:51
Remove the 'Billing' word from the error validation message in WooCommerce. When using WooCommerce, the checkout billing validation has the word 'Billing' automatically prefixed. Use the following filter to remove this.
add_filter( 'woocommerce_add_error', 'woocommerceAddError' );
/**
* Remove billing from the validation message
* @see wc_add_notice
*
* @param $error
* @return string|string[]
*/
function woocommerceAddError ( $error )
@brandonkramer
brandonkramer / bulk-edit-yoast-canonical-url.php
Created October 24, 2020 00:16
Change Yoast canonical URL in bulk for all posts for consolidation/migration by updating the meta field
add_action( 'wp_head', function () {
if ( isset( $_GET[ 'bulk-edit-canonical-url' ] ) ) {
$args = [
'post_type' => 'post',
'suppress_filters' => true,
'posts_per_page' => -1,
'post_status' => [ 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit' ],
];
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
@brandonkramer
brandonkramer / woocommerce-add-text-under-checkout-field.php
Created November 10, 2020 14:09
Add description, (footnote) text or html under a checkout field in WooCommerce
// Text field
add_filter('woocommerce_form_field_text', function ($field, $key, $args, $value) {
if ($key === 'billing_first_name') {
$field .= 'here text <a href="/">with link</a>';
}
return $field;
}, 10, 4);
// Phone field
@brandonkramer
brandonkramer / add-buddypress-xprofile-fields-to-woocommerce-checkout.php
Last active March 22, 2023 23:40
Add and integrate your BuddyPress profile / xProfile fields into the WooCommerce checkout page. With these hooks you can add custom fields to your checkout page based on BuddyPress xProfile fields and let them get saved to the user after the checkout is complete. We use the xprofile_get_field function and field ID to get and save data.
/**
* Add BudyPress fields to the WooCommerce checkout page by using the xprofile_get_field function from BudyPress
*
* @see xprofile_get_field()
*/
add_filter( 'woocommerce_checkout_fields', function ( $checkout_fields ) {
// text field example
$bp_field = xprofile_get_field( '211' ); // Get BudyPress field with Field ID
$bp_field_wc_id = sanitize_title( 'bp_field_' . $bp_field->name ); // Field ID to identify through the WooCommerce checkout process
@brandonkramer
brandonkramer / woocommerce-single-category-redirect.php
Last active September 1, 2021 16:47
WooCommerce: If a parent category has only one subcategory then it will automatically redirect to that subcategory.
<?php
add_action( 'template_redirect', function () {
global $wp_query;
if ( is_archive() ) {
$queried_object = get_queried_object();
if (
isset( $queried_object->taxonomy ) &&
isset( $queried_object->term_id ) &&
$queried_object->taxonomy === 'product_cat' &&
count( get_term_children( $queried_object->term_id, 'product_cat' ) ) === 1 ) {