Skip to content

Instantly share code, notes, and snippets.

Avatar

Yan Knudtskov yanknudtskov

View GitHub Profile
@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
@yanknudtskov
yanknudtskov / functions.php
Created May 21, 2020
Adding Custom WooCommerce MyAccount Endpoints
View functions.php
<?php
// Add the endpoints
add_action( 'init', 'yanco_add_my_account_endpoints' );
function yanco_add_my_account_endpoints() {
add_rewrite_endpoint( 'my-custom-endpoint', EP_ROOT | EP_PAGES );
add_rewrite_endpoint( 'my-custom-endpoint-2', EP_ROOT | EP_PAGES );
}
// Make custom endpoints available to query vars
@yanknudtskov
yanknudtskov / functions.php
Created May 19, 2020
Change the label for the ACF Post Title field when used in acf_form( $args )
View functions.php
<?php
add_filter( 'enter_title_here', 'yanco_change_title_text' );
function yanco_change_title_text( $title ){
$screen = get_current_screen();
if ( 'leverandor' == $screen->post_type ) {
$title = 'Virksomhedsnavn';
}
View functions.php
<?php
$the_object = array( 'your' => 'object' );
print( '<pre>' . print_r( $the_object, true ) . '</pre>' );
@yanknudtskov
yanknudtskov / functions.php
Created May 5, 2020
WooCommerce Cart Validation. How to validate products being added to cart, before they are added.
View functions.php
<?php
add_filter( 'woocommerce_add_to_cart_validation', 'yanco_validate_add_cart_item', 10, 5 );
function yanco_validate_add_cart_item( $passed, $product_id, $quantity, $variation_id = '', $variations = '' ) {
$post_title = get_the_title( $product_id );
// Items in cart, test if the product has already been added to the cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
@yanknudtskov
yanknudtskov / titlecase.sql
Created Apr 23, 2020
Set Title case for all products in WooCommerce using SQL
View titlecase.sql
UPDATE wp_posts SET post_title = LCASE( post_title ) WHERE post_type = 'product';
UPDATE wp_posts SET post_title = CONCAT( UCASE( LEFT(post_title, 1) ), SUBSTRING(post_title, 2) ) WHERE post_type = 'product';
You can’t perform that action at this time.