Skip to content

Instantly share code, notes, and snippets.

// Add Math CAPTCHA to WooCommerce Checkout
add_action('woocommerce_after_order_notes', 'custom_math_captcha_checkout');
function custom_math_captcha_checkout($checkout) {
$num1 = rand(1, 10); // Generate first random number
$num2 = rand(1, 10); // Generate second random number
$sum = $num1 + $num2; // Calculate the sum
// Store the sum in a session for later validation
WC()->session->set('math_captcha_sum', $sum);
@mohsinworld
mohsinworld / Display Current Year in WordPress Using Shortcode
Created September 27, 2024 22:15
[year] shorcode will show the current year
function currentYear( $atts ){
return date('Y');
}
add_shortcode( 'year', 'currentYear' );
@mohsinworld
mohsinworld / Shrink Sticky Menu (shrink the logo) Using Elementor
Created September 15, 2024 13:32
This code should add in Elementor cutom CSS & logoimage should add into custom CSS classes box
.logoimage {
max-width: 180px;
width: 180px;
transition: 0.5s all ease-in-out;
}
.elementor-sticky--effects .logoimage {
max-width: 90px;
width: 90px;
transition: 0.5s all ease-in-out;
/wp-admin/admin.php?page=wpcf7&action=validate
@mohsinworld
mohsinworld / Roatating Image Like Vinyl
Created June 15, 2024 07:26
Have to add this as custom CSS on that widget
selector {
display: inline-block;
animation: rotate 5s linear infinite;
}
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
@mohsinworld
mohsinworld / WooCommerce Custom Delivery Days
Last active June 12, 2024 10:21
Add as PHP Snippet by WP Code Snippet Plugin. Insert Method: Auto Insert, Location: Run Everywhere.
// Add a custom delivery days range field to the product editing page
add_action('woocommerce_product_options_general_product_data', 'add_custom_delivery_days_range_field');
function add_custom_delivery_days_range_field() {
global $post;
// Get the current value, if it exists
$custom_delivery_days = get_post_meta($post->ID, '_custom_delivery_days', true);
echo '<div class="options_group">';
<!-- Meta Pixel Code -->
<script>
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
function wc_billing_field_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Billing details' :
$translated_text = __( 'Billing Info', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'wc_billing_field_strings', 20, 3 );
Code Snippet
// Automatically shortens WooCommerce product titles on the main shop, category, and tag pages
// to a set number of characters
function short_woocommerce_product_titles_chars( $title, $id ) {
if ( ( is_shop() || is_product_tag() || is_product_category() ) && get_post_type( $id ) === 'product' ) {
// Kicks in if the product title is longer than 60 characters
if ( strlen( $title ) > 40) {
// Shortens it to 60 characters and adds ellipsis at the end
return substr( $title, 0, 40 ) . '...';