Skip to content

Instantly share code, notes, and snippets.

View yanknudtskov's full-sized avatar

Yan Knudtskov yanknudtskov

View GitHub Profile
@yanknudtskov
yanknudtskov / functions.php
Created May 19, 2020 14:50
Change the label for the ACF Post Title field when used in acf_form( $args )
<?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';
}
@yanknudtskov
yanknudtskov / functions.php
Created May 10, 2020 13:20
Pretty print_r
<?php
$the_object = array( 'your' => 'object' );
print( '<pre>' . print_r( $the_object, true ) . '</pre>' );
@yanknudtskov
yanknudtskov / functions.php
Created May 5, 2020 09:10
WooCommerce Cart Validation. How to validate products being added to cart, before they are added.
<?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 April 23, 2020 15:16
Set Title case for all products in WooCommerce using 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';
@yanknudtskov
yanknudtskov / functions.php
Created April 17, 2020 08:15
In some cases autoptimize JS scripts will be cached in Google and return 404 results if the autoptimize cache has been cleared. To get around that problem this piece of code returns a HTTP 410 Gone for all those scripts that return a 404 because they have been cleared from the Autoptimize cache.
<?php
add_action( 'template_redirect', 'yanco_404_redirect_to_410_for_autoptimize' );
function yanco_404_redirect_to_410_for_autoptimize() {
if( is_404() ) {
$search = 'cache/autoptimize/js/autoptimize_';
if( strpos( $_SERVER['REQUEST_URI'], $search ) !== false ) {
wp_redirect( $_SERVER['REQUEST_URI'], 410 );
exit;
@yanknudtskov
yanknudtskov / functions.php
Last active October 7, 2021 20:11
How to defer parsing of scripts loaded with WordPress wp_enqueue_script
<?php
function yanco_defer_script_loader_tag( $tag, $handle, $src ) {
$defer = array(
'magnific-popup',
'cycle',
'script'
);
if ( in_array( $handle, $defer ) ) {
return '<script src="' . $src . '" defer="defer" type="text/javascript"></script>' . "\n";
@yanknudtskov
yanknudtskov / functions.php
Created March 12, 2020 12:32
WooCommerce Products per page dropdown
<?php
add_action( 'woocommerce_before_shop_loop', 'yanco_products_per_page_selectbox', 25 );
function yanco_products_per_page_selectbox() {
$html = '';
$style = 'font-size: 16px;
font-weight: 300;
font-family: inherit;
letter-spacing: 1px;
@yanknudtskov
yanknudtskov / functions.php
Created March 10, 2020 14:08
Modify WooCommerce Checkout Fields Template
<?php
add_filter( 'woocommerce_checkout_fields' , 'yanco_remove_woo_checkout_fields' );
function yanco_remove_woo_checkout_fields( $fields ) {
// remove billing fields
unset( $fields['billing']['billing_company'] );
unset( $fields['billing']['billing_address_2'] );
// remove order comment fields
// unset($fields['order']['order_comments']);
return $fields;
@yanknudtskov
yanknudtskov / currency-formatting.js
Created March 4, 2020 14:41
Formatting currency in JavaScript
total.toLocaleString("da-DK", {
style: "currency",
currency: "DKK"
});
@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 ) {