Skip to content

Instantly share code, notes, and snippets.

Avatar

Yan Knudtskov yanknudtskov

View GitHub Profile
@yanknudtskov
yanknudtskov / wp-admin-select2.php
Created Mar 7, 2017
Add select2 to all select fields in WordPress Admin
View wp-admin-select2.php
<?php
function enqueue_select2_jquery() {
wp_register_style( 'select2css', '//cdnjs.cloudflare.com/ajax/libs/select2/3.4.8/select2.css', false, '1.0', 'all' );
wp_register_script( 'select2', '//cdnjs.cloudflare.com/ajax/libs/select2/3.4.8/select2.js', array( 'jquery' ), '1.0', true );
wp_enqueue_style( 'select2css' );
wp_enqueue_script( 'select2' );
}
add_action( 'admin_enqueue_scripts', 'enqueue_select2_jquery' );
@yanknudtskov
yanknudtskov / functions.php
Created Nov 25, 2020
Example code to migrate from ACF label based index of choice fields to a value : label based index
View functions.php
<?php
add_shortcode( 'migrate_acf_choice_data', 'migrate_acf_choice_data' );
function migrate_acf_choice_data() {
global $wpdb;
$meta_key_to_migrate = ''; // Set the name of the acf_field to migrate the data for
$dry_run = true; // Set to false to enable live run
$html = '';
@yanknudtskov
yanknudtskov / functions.php
Created Nov 20, 2020
Add a WooCommerce Checkbox at Checkout to accept privacy policy.
View functions.php
<?php
/**
* Add privacy policy tick box at checkout
*/
add_action( 'woocommerce_review_order_before_submit', 'yanco_add_checkout_privacy_policy', 9 );
function yanco_add_checkout_privacy_policy() {
woocommerce_form_field( 'privacy_policy', array(
'type' => 'checkbox',
'class' => array('form-row privacy'),
@yanknudtskov
yanknudtskov / woocommerce-update-prices.sql
Last active Nov 16, 2020
Queries for updating all prices including variations in WooCommerceIn this instance all prices are subtracted 20% (0.8)#woocommerce #mysql
View woocommerce-update-prices.sql
UPDATE wp_postmeta SET meta_value = meta_value*0.8 WHERE meta_key = '_regular_price' AND meta_value != ''
UPDATE wp_postmeta SET meta_value = meta_value*0.8 WHERE meta_key = '_sale_price' AND meta_value != ''
UPDATE wp_postmeta SET meta_value = meta_value*0.8 WHERE meta_key = '_price' AND meta_value != ''
UPDATE wp_postmeta SET meta_value = meta_value*0.8 WHERE meta_key = '_regular_price_tmp' AND meta_value != ''
UPDATE wp_postmeta SET meta_value = meta_value*0.8 WHERE meta_key = '_sale_price_tmp' AND meta_value != ''
UPDATE wp_postmeta SET meta_value = meta_value*0.8 WHERE meta_key = '_price_tmp' AND meta_value != ''
UPDATE wp_postmeta SET meta_value = meta_value*0.8 WHERE meta_key = '_min_variation_price' AND meta_value != ''
UPDATE wp_postmeta SET meta_value = meta_value*0.8 WHERE meta_key = '_max_variation_price' AND meta_value != ''
UPDATE wp_postmeta SET meta_value = meta_value*0.8 WHERE meta_key = '_min_variation_regular_price' AND meta_value != ''
UPDATE wp_postmeta SET meta_value = meta_value*0.8 WHE
@yanknudtskov
yanknudtskov / functions.php
Created Nov 5, 2020
A relatively simple way to replace URL for media files in WordPress when running a Staging environment
View functions.php
<?php
define( 'STAGING_URL', 'https://staging.mysiteurl.com');
define( 'PRODUCTION_URL', 'https://www.mysiteurl.com');
add_filter( 'wp_get_attachment_url', 'yanco_staging_wp_get_attachment_url', 1000000, 2 );
function yanco_staging_wp_get_attachment_url( $url, $attachment )
{
if( strpos( $url, STAGING_URL ) !== false ) {
$url = str_replace( STAGING_URL, PRODUCTION_URL, $url );
@yanknudtskov
yanknudtskov / functions.php
Created Nov 4, 2020
WooCommerce Subscriptions: Also send Subscription Cancelled email to customer
View functions.php
<?php
add_action('woocommerce_subscription_status_pending-cancel', 'yanco_woocommerce_subscription_status_pending_cancel', 10, 3 );
function yanco_woocommerce_subscription_status_pending_cancel( $subscription ) {
$customer_email = $subscription->get_billing_email();
$wc_emails = WC()->mailer()->get_emails();
$admin_email = $wc_emails['WCS_Email_Cancelled_Subscription']->recipient;
$wc_emails['WCS_Email_Cancelled_Subscription']->trigger( $subscription );
@yanknudtskov
yanknudtskov / functions.php
Created Nov 4, 2020
Modyfying the WooCommerce Thank You Page Texts
View functions.php
<?php
add_filter( 'the_title', 'woo_personalize_order_received_title', 10, 2 );
function woo_personalize_order_received_title( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
// Get the order. Line 9 to 17 are present in order_received() in includes/shortcodes/class-wc-shortcode-checkout.php file
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
@yanknudtskov
yanknudtskov / change-allowed-html-tags.php
Created Apr 21, 2014
Redefining Allowed HTML Tags From Scratch With CUSTOM_TAGS
View change-allowed-html-tags.php
<?php
function my_filter( $string )
{
global $allowedtags;
$allowedtags['img'] = array( 'src' => array () );
return $string;
}
add_filter( 'pre_kses', 'my_filter' );
@yanknudtskov
yanknudtskov / disallow-file-mods.php
Created Jan 4, 2014
Disallow File Mods Be careful using this, as it will disable the ability to update both core, plugins and themes in the Wordpress Admin. It's very helpful for security or locking a website into a static version.
View disallow-file-mods.php
define('DISALLOW_FILE_MODS', true);
@yanknudtskov
yanknudtskov / cli-complete-orders.php
Created Sep 13, 2020
Creating a custom WP CLI command
View cli-complete-orders.php
<?php
/*
Plugin Name: Yan&Co complete orders CLI command
Plugin URI: https://www.yanco.dk/
Description: Complete virtual orders CLI command
Author: Yan&Co
Version: 1.0.0
Author URI: https://www.yanco.dk/
*/
You can’t perform that action at this time.