Skip to content

Instantly share code, notes, and snippets.

View yanknudtskov's full-sized avatar

Yan Knudtskov yanknudtskov

View GitHub Profile
@yanknudtskov
yanknudtskov / .htaccess
Created August 12, 2019 05:16
Common redirects for htaccess. Credit: https://gist.github.com/ScottPhillips/1721489
#301 Redirects for .htaccess
#Redirect a single page:
Redirect 301 /pagename.php http://www.domain.com/pagename.html
#Redirect an entire site:
Redirect 301 / http://www.domain.com/
#Redirect an entire site to a sub folder
Redirect 301 / http://www.domain.com/subfolder/
@yanknudtskov
yanknudtskov / function.php
Created August 6, 2019 15:15
Adding custom fields to woocommerce products
<?php
// From https://remicorson.com/mastering-woocommerce-products-custom-fields/
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
@yanknudtskov
yanknudtskov / functions.php
Created July 5, 2019 11:46
Forcibly disable ajax add to cart in WooCommerce
<?php
/**
* Forcibly disable ajax add to cart
*/
add_filter( 'woocommerce_product_supports', 'yanco_disable_ajax_add_to_cart_supports', 10, 3 );
function yanco_disable_ajax_add_to_cart_supports( $supports_ajax, $feature, $product ) {
if ( 'ajax_add_to_cart' == $feature ) {
$supports_ajax = false;
}
@yanknudtskov
yanknudtskov / products_with_out_category.sql
Created June 25, 2019 07:17
An easy way to get all products in WooCommerce which doesn't have a category, using SQL
# Gets all product category term IDs
SELECT wp_terms.term_id
FROM wp_terms
LEFT JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
WHERE wp_term_taxonomy.taxonomy = 'product_cat'
# Use the list of IDs from above into IN ( ) using commaseparation
# This will return all the product IDs which are associated to product categories
SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN( )
@yanknudtskov
yanknudtskov / functions.php
Created June 18, 2019 12:44
WooCommerce Sensei LMS - How to change slugs of /course and /lesson
<?php
add_filter( 'sensei_course_slug', 'yanco_sensei_course_slug', 1000 );
function yanco_sensei_course_slug( ) {
return 'kursus';
}
add_filter( 'sensei_lesson_slug', 'yanco_sensei_lesson_slug', 1000 );
function yanco_sensei_lesson_slug() {
return 'lektion';
@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 June 4, 2019 07:26
Check if an Advanced Custom Field (ACF field) value changed.
<?php
// Remember to insert the {$field_name}
add_filter('acf/update_value/name={$field_name}', 'yanco_check_change_of_field_value', 10, 3);
function yanco_check_change_of_field_value( $value, $post_id, $field) {
$old_value = get_post_meta($post_id, $field['name'], true);
if ($old_value != $value) {
@yanknudtskov
yanknudtskov / functions.php
Created May 3, 2019 20:28
Overriding specific language/translations in WordPress
<?php
add_filter( 'gettext', 'yanco_filter_gettext', 10, 3 );
function yanco_filter_gettext( $translated, $original, $domain ) {
// Use the text string exactly as it is in the translation file
if ( $translated == '' ) {
$translated = '';
}
@yanknudtskov
yanknudtskov / functions.php
Last active June 20, 2023 19:30
WooCommerce Subscriptions check if the current user has an active subscription
<?php
function yanco_has_active_subscription( $user_id = '' ) {
if( function_exists( 'wcs_user_has_subscription' ) ) {
// When a $user_id is not specified, get the current user Id
if( '' == $user_id && is_user_logged_in() ) {
$user_id = get_current_user_id();
}
// User not logged in we return false
@yanknudtskov
yanknudtskov / sftp.json
Last active March 22, 2019 12:25
Visual Studio Code SFTP config template
{
"name": "SERVER",
"profiles": {
"development": {
"host": "",
"username": "",
"password": "",
"remotePath": "",
"uploadOnSave": true,