• Skip to main content
  • Skip to primary sidebar

WordPress, Genesis Framework and Storefront customization tutorials

  • Archive
    • Free
    • Premium
  • Blog
  • About
  • Contact
  • Newsletter
  • Login
Home » Free » Page 2

Free

Add notification to specific shipping method in WooComerce

// =========================================================================
// ADD NOTIFICATION TO SPECIFIC SHIPPING METHOD
// =========================================================================
add_action('woocommerce_review_order_before_payment', function () {
    ?>
    <div id="mpl-warning" style="display:none;">
        Figyelem! Amennyiben postával rendelne, 500 Ft kezelési költséget számítunk fel és a kiszállítási időt csak 20 napon belül tudjuk vállalni.
    </div>
    <script type="text/javascript">
        jQuery(function($) {
            function updateHelloWorldVisibility() {
                var chosen_shipping = $('input[name^="shipping_method"]:checked').val();
                
                if (chosen_shipping === 'flat_rate:5') {
                    $("#mpl-warning").show();
                } else {
                    $("#mpl-warning").hide();
                }
            }

            // Frissítjük a "Hello World" szöveg láthatóságát szállítási mód változásakor
            $(document.body).on('change', 'input[name^="shipping_method"]', updateHelloWorldVisibility);
            
            // Az oldal betöltésekor is ellenőrizzük a szállítási módot
            updateHelloWorldVisibility();
        });
    </script>
    <?php
}, 10, 2);

Filed Under: Free

How to use wp_kses in ACF?

functions.php

// =============================================================
// ALLOWED HTML IN ACF
// =============================================================
function wpflames_allowed_html() {
    return array(
        'a' => array(
            'href' => array(),
            'title' => array(),
            'class' => array(),
        ),
        'br' => array(),
        'em' => array(),
        'strong' => array(),
    );
}
<?php echo wp_kses(get_field('text'), wpflames_allowed_html()); ?>

Filed Under: Free Tagged With: ACF

Delete all WordPress posts from phpMyAdmin

DELETE FROM wp_posts WHERE post_type = 'post';

Filed Under: Free

Create a Gutenberg Block

npx @wordpress/create-block static-block
npm install --save-dev style-loader css-loader sass-loader

Filed Under: Free, Other

Redirect home if user is not logged in

if (!is_user_logged_in()){
    wp_redirect(esc_url(site_url('/')));
    exit;
}

Filed Under: Free, Other

wp_localize_script

function wpflames_localize_files() {
  wp_localize_script('main-js', 'wpflamesData', array(
    'root_url' => get_site_url(),
    'nonce' => wp_create_nonce('wp_rest')
  ));
}
add_action('wp_enqueue_scripts', 'wpflames_localize_files');

This code is a WordPress function named wpflames_localize_files. Its purpose is to provide data to the scripts associated with a WordPress theme or plugin. Here’s a detailed explanation of what it does:

  1. wp_localize_script function: This function allows sending data from PHP to JavaScript scripts. In this case, the script named ‘main-js’ receives the data. The wpflamesData is an object that contains two pieces of data:
  • 'root_url': Receives the result of get_site_url(), which returns the base URL of the WordPress installation.
  • 'nonce': A unique security token created by the wp_create_nonce('wp_rest') function, which can be used in WordPress REST API calls to enhance the security of the site.
  1. add_action call: This is part of the WordPress hook system. The wp_enqueue_scripts is a specific event that WordPress triggers when loading scripts and stylesheets. We register our wpflames_localize_files function with this event, so the function runs on every page load.

In summary, this code can be used in a WordPress theme or plugin to make certain PHP-based data accessible to JavaScript, as well as to provide secure methods for using the WordPress REST API.

Filed Under: Free, Other

Webpack and @wordpress/scripts

Initialize package.json

npm init -y

Install wp-cli

npm install wp-cli --save-dev

Install WordPress Scripts for Webpack

npm install @wordpress/scripts --save-dev

Install Sass Loader

npm install sass-loader sass webpack --save-dev

Install MiniCssExtractPlugin

npm install --save-dev mini-css-extract-plugin

webpack.config.js

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
mode: 'development',
entry: './assets/src/js/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'assets/dist'),
},
module: {
rules: [
{
test: /\.scss$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: 'style.css',
}),
],
};

package.json

"scripts": {
    "build": "wp-scripts build",
    "start": "wp-scripts start",
    "dev": "wp-scripts start",
    "devFast": "wp-scripts start",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

Enqueue compiled JavaScript in WordPress

function enqueue_theme_scripts() {
wp_enqueue_script('main-js', get_template_directory_uri() . '/assets/dist/main.js', array(), '1.0.0', true);
wp_enqueue_style('style-css', get_template_directory_uri() . '/assets/dist/style.css');
}
add_action('wp_enqueue_scripts', 'enqueue_theme_scripts');

Filed Under: Free, Other

Add Flag Icon to WordPress Nav Menu

// =========================================================================
// ADD FLAG ICON TO THE WORDPRESS NAV MENU
// =========================================================================
function add_custom_menu_item ($items, $args) {
    ob_start();
    $items .= '<li class="menu-item flag">';
        $items .= '<a href="#" target="_blank"><img class="flag" src="'.THEME.'/assets/images/flags/flag-hu.svg" alt=""/></a>';
    $items .= '</li>';
    return $items;
}
add_filter( 'wp_nav_menu_items', 'add_custom_menu_item', 10, 2 );
flag-hu.svg
<svg version="1.1" id="flag-hu" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 viewBox="0 0 50 34" style="enable-background:new 0 0 50 34;" xml:space="preserve">
<style type="text/css">
	.st0{fill:#CE2939;}
	.st1{fill:#FFFFFF;}
	.st2{fill:#477050;}
</style>
<rect id="black_stripe" y="0" class="st0" width="50" height="34"/>
<rect id="red_stripe" y="11.3" class="st1" width="50" height="22.7"/>
<rect id="gold_stripe" y="22.7" class="st2" width="50" height="11.3"/>
</svg>
flag-de.svg
<svg version="1.1" id="flag-de" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 viewBox="0 0 50 34" style="enable-background:new 0 0 50 34;" xml:space="preserve">
<style type="text/css">
	.st0{fill:#DD0000;}
	.st1{fill:#FFCE00;}
</style>
<rect id="black_stripe" y="0" width="50" height="34"/>
<rect id="red_stripe" y="11.4" class="st0" width="50" height="22.6"/>
<rect id="gold_stripe" y="22.6" class="st1" width="50" height="11.4"/>
</svg>

Filed Under: Free, Other

Add chevron to nav menu has children

// =========================================================================
// Chevron to nav menu has children
// =========================================================================
function parent_menu_dropdown( $item_output, $item, $depth, $args ) {
    
    if ( is_nav_menu( 'Header Menu' ) && ! empty( $item->classes ) && in_array( 'menu-item-has-children', $item->classes ) ) {
        return $item_output . '<svg class="chevron-down" fill="#ffffff" xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 512 512"><path fill="#ffffff" d="M233.4 406.6c12.5 12.5 32.8 12.5 45.3 0l192-192c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L256 338.7 86.6 169.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l192 192z"/></svg>';
    }
    
    return $item_output;
}
add_filter( 'walker_nav_menu_start_el', 'parent_menu_dropdown', 10, 4 );

Filed Under: Free, Other

Nav Walker – custom sub-menu class

1st level sub menu

<ul class="sub-menu-1">

2nd level sub menu

<ul class="sub-menu-2">
<?php

class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {

    function start_lvl(&$output, $depth = 0, $args = array()) {
        $indent = str_repeat("\t", $depth);
        $submenu_class = 'sub-menu-' . ($depth + 1); 
        $output .= "\n$indent<ul class=\"$submenu_class\">\n";
    }
}

wp_nav_menu(array(
    'theme_location' => 'desktop_menu',
    'menu_class'     => 'desktop-menu',
    'walker'         => new Custom_Walker_Nav_Menu(),
));
<

Filed Under: Free, Other

GTM4WP

// Add Google Tag code which is supposed to be placed after opening body tag.
function wpdoc_add_custom_body_open_code() {
	if ( function_exists( 'gtm4wp_the_gtm_tag' ) ) { 
		gtm4wp_the_gtm_tag(); 
	} 
}
add_action( 'wp_body_open', 'wpdoc_add_custom_body_open_code' );

Filed Under: Free, Other

Masonry layout with SCSS

View Demo
View GitHub

Filed Under: Free, Other

How to use Mailgun as SMTP without plugin

If You want to replace WP Mail SMTP plugin use this snippet.

Filed Under: Free

Add discount checkbox field to WooCommerce checkout, save meta data, display in admin and in email

Filed Under: Free Tagged With: WooCommerce

Add extra fee to COD payment gateway

Filed Under: Free Tagged With: WooCommerce

Prettier doesn’t format the code

  • Go to Settings and search for formatter.
  • Set the default formatter to Prettier

Filed Under: Free

Use SVG Icons

.icon {
	display: inline-block;
	min-width: 5px;
	min-height: 5px;
	background-position: center;
	background-size: contain;
	background-repeat: no-repeat;
}

.icon.icon-print {
	background-image: url('../images/icons/print.svg');
        min-width: 20px;
        min-height: 20px;
}
<i class="icon icon-print"></i>

Filed Under: Free Tagged With: CSS, SVG

Insert page in to another page

// =========================================================================
// INSERT PAGE TO ANOTHER PAGE
// =========================================================================
function page_partners() {
    $args = new WP_Query("page_id=285"); 
    while($args->have_posts()) : $args->the_post();
        the_content();
    endwhile;
}

Filed Under: Free

Tab Panel with JavaScript

Filed Under: Free Tagged With: JavaScript

ACF Option Pages with Repeater Group and File

SCSS

Filed Under: Free

  • « Go to Previous Page
  • Page 1
  • Page 2
  • Page 3
  • Page 4
  • Interim pages omitted …
  • Page 19
  • Go to Next Page »

Primary Sidebar

Gabor Flamich

Hi! I'm Gabor.
I write tutorials on WordPress and WooCommerce.

MacBook

12 Essential Snippets for Genesis Developers

Subscribe to my Newsletter to view my basic collection of Genesis snippets that I use for my projects!

Sign Up for Free
  • Facebook
  • GitHub
  • Instagram
  • LinkedIn
  • Twitter
  • YouTube
UpdraftPlus Premium

Tags

ACF Ajax Analytics API Bootstrap Breadcrumb category CPT CSS fetch FSE Genesis Google Maps Gutenberg HTML Isotope JavaScript jQuery loop Map Menu Parallax PHP Rest API SASS SEO SQL Storefront SVG tab tag manager tags Taxonomy Tool upsell Webpack Wholesale WooCommerce WordPress WPML

Disclosure: Some of the links in this site are affiliate links. I will be paid a commission if you use this link to make a purchase.

  • Privacy Policy / Terms of Service
© 2025 WP Flames - All Right Reserved