Skip to content

Instantly share code, notes, and snippets.

Avatar

Yan Knudtskov yanknudtskov

View GitHub Profile
@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/
*/
@yanknudtskov
yanknudtskov / functions.php
Created Sep 8, 2020
Adjust Facebook for WooCommerce Feed Regeneration Interval
View functions.php
<?php
/*
* Adjust the interval that the feed file is generated.
*/
add_filter( 'wc_facebook_feed_generation_interval', 'yanco_wc_facebook_feed_generation_interval', 10, 1 );
function yanco_wc_facebook_feed_generation_interval( $interval ) {
return 60 * 30;
}
@yanknudtskov
yanknudtskov / functions.php
Created Aug 13, 2020
Add query string if failed login with Elementor Pro Login Form
View functions.php
<?php
add_action( 'wp_login_failed', 'yanco_elementor_form_login_fail' );
function yanco_elementor_form_login_fail( $username ) {
// where did the post submission come from?
$referrer = $_SERVER['HTTP_REFERER'];
// if there's a valid referrer, and it's not the default log-in screen
if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
@yanknudtskov
yanknudtskov / functions.php
Created Jun 29, 2020
List all enqueued scripts and styles
View functions.php
<?php
function yanco_list_styles() {
global $wp_styles;
echo "<h2>Enqueued CSS Stylesheets</h2><ul>";
foreach( $wp_styles->queue as $handle ) :
echo "<li>" . $handle . "</li>";
endforeach;
echo "</ul>";
}
@yanknudtskov
yanknudtskov / functions.php
Created Jun 25, 2020
Remove ACF validation for administrators
View functions.php
<?php
// Disable Ready Only for Administrators
// add_filter( 'acf/load_field', 'yanco_acf_disable_read_only', 10000 );
function yanco_acf_disable_read_only( $field ) {
// Applies to administrators
if( is_admin() && get_post_type() == 'afrapportering' ) {
global $pagenow;
@yanknudtskov
yanknudtskov / functions.php
Created Jun 9, 2020
Example on how to meta query ACF repeater fields
View functions.php
<?php
add_shortcode( 'user_company_link', 'yanco_user_company_link' );
function yanco_user_company_link() {
if( ! is_user_logged_in() ) {
return;
}
$html = '';
@yanknudtskov
yanknudtskov / getqueryparameter.js
Created Jun 5, 2020
Get Query parameter with JavaScript (JS)
View getqueryparameter.js
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
@yanknudtskov
yanknudtskov / functions.php
Created May 25, 2020
WooCommerce Add Custom Fields to Products
View functions.php
<?php
// For variations
add_action( 'woocommerce_variation_options_pricing', 'yanco_add_custom_field_to_variations', 10, 3 );
function yanco_add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'custom_field[' . $loop . ']',
'class' => 'short',
'label' => __( 'Custom Field', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'custom_field', true )
@yanknudtskov
yanknudtskov / functions.php
Created May 23, 2020
Extend the output of the WooCommerce REST API for order line items
View functions.php
<?php
/**
* Example: Add order meta to the REST API
* WC 2.6+
*
* @param \WP_REST_Response $response The response object.
* @param \WP_Post $post Post object.
* @param \WP_REST_Request $request Request object.
* @return object updated response object
@yanknudtskov
yanknudtskov / remove-spam-users.sql
Created May 23, 2020
Delete SPAM users from WordPress/WooCommerce. They usually never have a first name set
View remove-spam-users.sql
# Use with EXTREME CAUTION
DELETE FROM wp_users WHERE ID IN ( SELECT user_id FROM wp_usermeta
WHERE meta_key = 'wp_capabilities' AND meta_value LIKE '%subscriber%'
AND user_id NOT IN ( SELECT user_id FROM wp_usermeta
WHERE meta_key = 'billing_first_name' AND meta_value != ''
AND user_id IN ( SELECT user_id FROM wp_usermeta
WHERE meta_key = 'wp_capabilities' AND meta_value LIKE '%subscriber%' ) ) )
DELETE FROM wp_usermeta WHERE user_id IN ( SELECT user_id FROM wp_usermeta
You can’t perform that action at this time.