Skip to content

Instantly share code, notes, and snippets.

// Start of Selection
// Initialize the arguments for the WP query to fetch various post types
$args = array(
'post_type' => ['post', 'session', 'resource', 'video'], // Define post types to query
'posts_per_page' => -1, // Set to '-1' to fetch all posts. Replace with a specific number if needed
'order' => 'DESC', // Order by descending to get the most recent or viewed first
// You can add more arguments here as per your query requirements
);
// Retrieve the most viewed posts using a function provided by the Post Views Counter plugin
@rabinkumarpal
rabinkumarpal / gist:9143ec6fb0bcca86de2631c965ccf437
Created January 14, 2024 05:54 — forked from kosinix/gist:5354637
Get month names using a for loop and PHP date function.
<?php
for($m=1; $m<=12; ++$m){
echo date('F', mktime(0, 0, 0, $m, 1)).'<br>';
}
@rabinkumarpal
rabinkumarpal / change-sender-name.php
Created January 28, 2023 08:33 — forked from sarvar/change-sender-name.php
How to Change Sender Name in Outgoing WordPress Email
// Function to change email address
function wpb_sender_email( $original_email_address ) {
return '[email protected]';
}
// Function to change sender name
function wpb_sender_name( $original_email_from ) {
return 'Tim Smith';
}
@rabinkumarpal
rabinkumarpal / functions.php
Created January 24, 2023 19:04 — forked from maddisondesigns/functions.php
WooCommerce Custom Fields for Simple & Variable Products
/*
* Add our Custom Fields to simple products
*/
function mytheme_woo_add_custom_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Text Field
@rabinkumarpal
rabinkumarpal / create-admin-user.php
Created January 14, 2023 19:04 — forked from wpscholar/create-admin-user.php
Create a new admin user in WordPress via code. Drop this file in the mu-plugins directory and update the variables, then load a page in WordPress to create the user. Remove the file when done.
<?php
add_action( 'init', function () {
$username = 'admin';
$password = 'password';
$email_address = '[email protected]';
if ( ! username_exists( $username ) ) {
$user_id = wp_create_user( $username, $password, $email_address );
@rabinkumarpal
rabinkumarpal / custom_plugin_WC_templates.php
Last active November 22, 2022 11:20 — forked from Dilden/custom_plugin_WC_templates.php
Useful snippet for overriding WooCommerce templates in your custom WordPress plugin. Just follow the same directory structure as if you were overriding the templates from a theme.
// Override woocommerce templates with the templates in my plugin
add_filter( 'woocommerce_locate_template', 'custom_plugin_templates');
function custom_plugin_templates ( $template, $template_name, $template_path ) {
$check_dis = str_replace('woocommerce', 'your-plugin-name-goes-here/woocommerce', $template);
if(file_exists($check_dis)) {
$template = $check_dis;
}
return $template;
};

Woocommerce Javascript events
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' );
$( document.body ).trigger( 'applied_coupon_in_checkout' );
@rabinkumarpal
rabinkumarpal / add-nav-parent-count.php
Created October 13, 2022 23:38 — forked from jessepearson/add-nav-parent-count.php
Functions to add a parent count to WordPress nav menus. This is useful if you need to change nav element size based on the number of parent/top level elements.
<?php
/**
* Gets the count of the parent items in a nav menu based upon the id specified.
*
* @param string $nav_id The id of the nav item to get the parent count for.
* @return bool|int False on fail, or the count of how many parent items there are.
* @uses wp_get_nav_menu_items
*/
function count_nav_parent_items( $nav_id = null ) {
@rabinkumarpal
rabinkumarpal / .htaccess
Created October 12, 2022 15:14 — forked from seoagentur-hamburg/.htaccess
UPDATE 2022/10: Perfect .htaccess file for highspeed and security. You can use it for every WordPress-Website without problems. Highspeed and Security - testet on hundreds of Websites. If you are using a WordPress Multisite, change the last part of this file.
########################################################################
# OPTIMAL .htaccess FILE FOR SPEED AND SECURITY @Version 2.0.6 - 10/2022
# ----------------------------------------------------------------------
# @Author: Andreas Hecht
# @Author URI: https://seoagentur-hamburg.com
# License: GNU General Public License v2 or later
# License URI: http://www.gnu.org/licenses/gpl-2.0.html
########################################################################
@rabinkumarpal
rabinkumarpal / functions.php
Created September 25, 2022 16:06 — forked from MWDelaney/functions.php
WooCommerce: Prevent multiple coupons of the same "type" on a single order. This will, for instance, prevent coupon stacking if a customer or user has access to multiple Product % Discount or Cart % Discount coupons. Tested working with WooCommerce, WooCommerce Smart Coupons, and WooCommerce Points & Rewards
<?php
// Hook when a coupon is applied to a cart
add_action( 'woocommerce_applied_coupon', 'mwd_get_applied_coupons' );
// Get the current applied coupon and compare it with other applied coupons
function mwd_get_applied_coupons() {
// Get the currently applied coupon's type using the $_POST global to retrieve the current coupon's code
foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
if($coupon->code == $_POST['coupon_code']) {