Skip to content

Instantly share code, notes, and snippets.

View yanknudtskov's full-sized avatar

Yan Knudtskov yanknudtskov

View GitHub Profile
@yanknudtskov
yanknudtskov / remove-spam-users.sql
Created May 23, 2020 23:41
Delete SPAM users from WordPress/WooCommerce. They usually never have a first name set
# 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 / disallow-file-mods.php
Created January 4, 2014 13:15
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.
define('DISALLOW_FILE_MODS', true);
@yanknudtskov
yanknudtskov / woocommerce-duplicate-skus.sql
Last active July 9, 2024 03:28
Select Duplicate SKUs from WooCommerce Database #woocommerce #mysql
# SELECT any post_status
SELECT meta_value,COUNT(meta_value),GROUP_CONCAT(DISTINCT post_id ORDER BY post_id SEPARATOR ',') post_id
FROM wp_postmeta
WHERE meta_key = '_sku'
AND meta_value != ''
GROUP BY meta_value HAVING COUNT(meta_value) > 1
# SELECT only from products that are already published or in draft
SELECT meta_value,COUNT(meta_value),GROUP_CONCAT(DISTINCT post_id ORDER BY post_id SEPARATOR ',') post_id
FROM wp_postmeta
@yanknudtskov
yanknudtskov / functions.php
Created June 9, 2020 14:06
Example on how to meta query ACF repeater fields
<?php
add_shortcode( 'user_company_link', 'yanco_user_company_link' );
function yanco_user_company_link() {
if( ! is_user_logged_in() ) {
return;
}
$html = '';
@yanknudtskov
yanknudtskov / functions.php
Created June 4, 2019 07:31
get_posts meta_query example
<?php
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => 'student',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'acf_student_group',
@yanknudtskov
yanknudtskov / functions.php
Created February 22, 2020 10:54
Apparently SearchWP/Relevanss and Elementor doesn't play well together with Elementor, Searches and post grid. This is the outline of handling searching in post_meta.
<?php
// Modify the fields to search in
add_action( 'elementor/query/searched_query', function( $query ) {
$s = get_search_query();
if ( is_search() ) {
$query->set( 'post_type', 'leverandor' );
if( strlen($s) >= 3 ) {
@yanknudtskov
yanknudtskov / functions.php
Created May 21, 2020 09:35
Adding Custom WooCommerce MyAccount Endpoints
<?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 25, 2020 20:45
WooCommerce Add Custom Fields to Products
<?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 / getqueryparameter.js
Created June 5, 2020 09:06
Get Query parameter with JavaScript (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 August 13, 2020 06:00
Add query string if failed login with Elementor Pro Login Form
<?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') ) {