Skip to content

Instantly share code, notes, and snippets.

@bagerathan
bagerathan / woo-events.js
Last active April 26, 2024 18:43
[Woocommerce Javascript events] #woo
//Woocommerce Checkout JS events
$( document.body ).trigger( 'init_checkout' );
$( document.body ).trigger( 'payment_method_selected' );
$( document.body ).trigger( 'update_checkout' );
$( document.body ).trigger( 'updated_checkout' );
$( document.body ).trigger( 'checkout_error' );
//Woocommerce cart page JS events
$( document.body ).trigger( 'wc_cart_emptied' );
$( document.body ).trigger( 'update_checkout' );
@bagerathan
bagerathan / featured-image-in-quick-edit.php
Created August 22, 2021 04:36
[Featured image in quickedit] #wp
@bagerathan
bagerathan / share-slug.php
Created November 9, 2023 04:00
[Share slug between sub page and post type]
add_action( 'parse_request', 'my_parse_request' );
function my_parse_request( $wp ) {
$post_type = 'whatever'; // set the post type slug
// and the "whatever" below is the Page slug
// This code checks if the path of the current request/page URL begins with
// "whatever/" as in https://example.com/whatever/child-page-name and also
// https://example.com/whatever/child-page-name/page/2 (paginated request).
// We also check if the post_type var is the post type set above.
if ( preg_match( '#^whatever/#', $wp->request ) &&
@bagerathan
bagerathan / cpt.html
Created May 17, 2023 05:08
[Ajax search] Search custom post type
<form role="search" method="post" class="search-form padding-4" action="<?php echo home_url( '/' ); ?>">
<label>
<span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span>
<input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search...', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
<input class="post_type" type="hidden" name="post_type" value="frequent" />
</label>
<input type="submit" class="search-submit button brand" id="searchsubmit" value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" />
</form>
<script type="text/javascript">
@bagerathan
bagerathan / add-custom-fee-to-cart.php
Last active December 11, 2022 08:13
[WooCommerce Snippets] helpful snippets for WooCommerce development #woo
function woo_add_cart_fee() {
global $woocommerce;
if ( is_cart() ) {
$woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 );
}
}
add_action( 'woocommerce_before_cart_table', 'woo_add_cart_fee' );
@bagerathan
bagerathan / wordpress-conditions.php
Last active June 16, 2022 06:04
[WordPress conditions] #wp
<?php
is_home() //Checks if the blog post index is being displayed. This may or may not be your home page as well.
is_front_page() //Checks if your home page is being displayed. This works whether your front page settings are set up to display blog posts (i.e. blog index) or a static page.
is_single() //Checks to see whether any type of single post is being displayed (excluding attachments).
is_attachment() //Checks if an attachment is displayed.
@bagerathan
bagerathan / change-permalinks.php
Created June 16, 2022 06:03
[Change permalinks]
@bagerathan
bagerathan / protect-media-files.htaccess
Created November 29, 2021 03:13
[Protect WP media files] #htaccess
# Protect WP Media Files
# https://m0n.co/protect-media-files
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} /wp-content/uploads/ [NC]
RewriteCond %{HTTP_REFERER} !^https://example.com [NC]
RewriteRule .* - [F,L]
</IfModule>
@bagerathan
bagerathan / add-search-to-wordpress-menu.php
Created August 27, 2021 07:19
[Add anything to menu] #wp
//add search to menu
function add_last_nav_item($items, $args) {
if ('menu' == $args->theme_location) {
$searchblock = get_search_form(false);
$items .= '<li><svg class="menu-search" xmlns="http://www.w3.org/2000/svg" width="21.472" height="20.171" viewBox="0 0 21.472 20.171"><path d="M29.053,20.3a8.766,8.766,0,0,0-8.525,6.033,8.089,8.089,0,0,0,3.877,9.351,9.252,9.252,0,0,0,10.613-1l6.144,5.782.5-.471-6.108-5.782a7.933,7.933,0,0,0,1.576-8.956A8.861,8.861,0,0,0,29.053,20.3Zm0,15.935A7.881,7.881,0,0,1,20.944,28.6a7.881,7.881,0,0,1,8.109-7.631A7.881,7.881,0,0,1,37.162,28.6,7.881,7.881,0,0,1,29.053,36.235Z" transform="translate(-20.191 -20.3)" fill="#fff"/></svg>'.$searchblock.'</li>';
return $items;
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'add_last_nav_item', 10, 2 );
@bagerathan
bagerathan / svg-background.css
Last active August 24, 2021 05:22
[SVG in background] #css #svg
body {
background-image:
url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><linearGradient id='gradient'><stop offset='10%' stop-color='%23F00'/><stop offset='90%' stop-color='%23fcc'/> </linearGradient><rect fill='url(%23gradient)' x='0' y='0' width='100%' height='100%'/></svg>");
}
/*
Note that the SVG content needs to be url-escaped for this to work, e.g. # gets replaced with %23.
*/