Function to disable comments in WordPress
// Disable comments on all post types function df_disable_comments_post_types_support() { $post_types = get_post_types(); foreach ($post_types as $post_type) { if(post_type_supports($post_type, 'comments')) { remove_post_type_support($post_type, 'comments'); remove_post_type_support($post_type, 'trackbacks'); } } } add_action('admin_init', 'df_disable_comments_post_types_support'); // Close comments on the front-end function df_disable_comments_status() { return false; } add_filter('comments_open', 'df_disable_comments_status', 20, 2); add_filter('pings_open', 'df_disable_comments_status', 20, 2); // Hide existing comments function df_disable_comments_hide_existing_comments($comments) { $comments = array(); return $comments; } add_filter('comments_array', 'df_disable_comments_hide_existing_comments', 10, 2); // Remove comments page in menu function df_disable_comments_admin_menu() { remove_menu_page('edit-comments.php'); } add_action('admin_menu', 'df_disable_comments_admin_menu'); // Redirect any user trying to access comments page function df_disable_comments_admin_menu_redirect() { global $pagenow; if ($pagenow === 'edit-comments.php') { wp_redirect(admin_url()); exit; } } add_action('admin_init', 'df_disable_comments_admin_menu_redirect');
Function to remove Divi Projects in WordPress
add_filter( 'et_project_posttype_args', 'mytheme_et_project_posttype_args', 10, 1 ); function mytheme_et_project_posttype_args( $args ) { return array_merge( $args, array( 'public' => false, 'exclude_from_search' => false, 'publicly_queryable' => false, 'show_in_nav_menus' => false, 'show_ui' => false )); }
Show remaining cost on WooCommerce site to get free shipping
Add this to the functions.php file and then add the shortcode of [shippingleft] to get it to show up.
function how_much_shipping_is_left() { $min_amount = 100; //change this to your free shipping threshold $current = WC()->cart->subtotal; if ( $current < $min_amount ) { $added_text = 'Get free shipping if you order ' . wc_price( $min_amount - $current ) . ' more!'; $notice = sprintf( '%s', $added_text ); return $notice; } else { $notice = "Order total is over £100 and shipping is free"; return $notice; } } add_shortcode('shippingleft', 'how_much_shipping_is_left');
WooCommerce hide Klarna payment method if order total is under £35
Add this to the functions.php to hide Klarna payment method if the order total is under £35
//hide klarna if order total is under £35 add_filter( 'woocommerce_available_payment_gateways', 'rudr_turn_off_cod' ); function rudr_turn_off_cod( $available_gateways ) { if( is_admin() ) { return $available_gateways; } // STEP 1: Get order/cart total if( is_wc_endpoint_url( 'order-pay' ) ) { // Pay for order page $order_id = wc_get_order_id_by_order_key( $_GET[ 'key' ] ); $order = wc_get_order( $order_id ); $order_total = $order->get_total(); } else { // Cart/Checkout page $order_total = WC()->cart->total; } // STEP 2: Disable payment gateway if order/cart total is less than 35 if ( $order_total < 35 ) { unset( $available_gateways[ 'klarna_payments' ] ); // unset klarna } return $available_gateways; }
Show users how much they need to spend to qualify for Klarna on a WooCommerce site
Add this to the functions.php file and then add the shortcode of [dividebyklarna] to get it to show up.
// function that runs when shortcode is called function wpb_nick_klarna() { global $product; $klarnaprice = get_post_meta( get_the_ID(), '_regular_price', true); $klarnasaleprice = get_post_meta( get_the_ID(), '_sale_price', true); $y = $klarnaprice; $x = $klarnasaleprice; $number = ($y) / 3; $numbersale = ($x) / 3; $rounded = round($number,2); $roundedsale = round($numbersale,2); if ( $product->is_type( 'simple' ) ) { if($klarnaprice >=35 && $klarnaprice <=2000000 ) { if(empty($klarnasaleprice)){ $message = '<div class="makepaymentsblock klarnablock">Make 3 payments of ' .$message = '£'.$rounded .$message = ' using <strong>Klarna</strong> with no fees and 0% interest. <a class="sg-popup-id-24192" href="">Read More</a></div>'; } else { $message = '<div class="makepaymentsblock klarnablock">Make 3 payments of ' .$message = '£'.$roundedsale .$message = ' using <strong>Klarna</strong> with no fees and 0% interest. <a class="sg-popup-id-24192" href="">Read More</a></div>'; } } else { if(empty($klarnasaleprice)){ $message = '<div class="makepaymentsblock klarnablock">The minimum amount for Klarna is £35 <br/><a class="sg-popup-id-24192" href="">Read More</a></div>'; } else { $message = '<div class="makepaymentsblock klarnablock">The minimum amount for Klarna is £35 <br/><a class="sg-popup-id-24192" href="">Read More</a></div>'; } } // Output needs to be return return $message; } } // register shortcode add_shortcode('dividebyklarna', 'wpb_nick_klarna');
Show users how much they need to spend to qualify for PayPal on a WooCommerce site
Add this to the functions.php file and then add the shortcode of [dividebypaypal] to get it to show up.
// function that runs when shortcode is called function wpb_nick_paypal() { global $product; $paypalprice = get_post_meta( get_the_ID(), '_regular_price', true); $paypalsaleprice = get_post_meta( get_the_ID(), '_sale_price', true); $y = $paypalprice; $x = $paypalsaleprice; $number = ($y) / 4; $numbersale = ($x) / 4; $rounded = round($number,2); $roundedsale = round($numbersale,2); if ( $product->is_type( 'simple' ) ) { if($paypalprice >=99 && $paypalprice <=2000000 ) { if(empty($paypalsaleprice)){ $message = '<div class="makepaymentsblock paypalblock">Make 4 payments of ' .$message = '£'.$rounded .$message = ' using <strong>PayPal Credit</strong> with no fees and 0% interest. <a class="sg-popup-id-24194" href="">Read More</a></div>'; } else { $message = '<div class="makepaymentsblock paypalblock">Make 4 payments of ' .$message = '£'.$roundedsale .$message = ' using <strong>PayPal Credit</strong> with no fees and 0% interest. <a class="sg-popup-id-24194" href="">Read More</a></div>'; } } else { if(empty($paypalsaleprice)){ $message = '<div class="makepaymentsblock paypalblock">The minimum amount for PayPal Credit is £99 <br/><a class="sg-popup-id-24194" href="">Read More</a></div>'; } else { $message = '<div class="makepaymentsblock paypalblock">The minimum amount for PayPal Credit is £99 <br/><a class="sg-popup-id-24194" href="">Read More</a></div>'; } } // Output needs to be return return $message; } } // register shortcode add_shortcode('dividebypaypal', 'wpb_nick_paypal');
Output additional image sizes on the frontend of WordPress
Use this to find all the additional image sizes your site is using
/* Output all image sizes on the frontend ---------------------------------------------------------------------------------------------------- */ add_action('init', 'get_all_image_sizes'); function get_all_image_sizes(){ global $_wp_additional_image_sizes; print '<pre>'; print_r( $_wp_additional_image_sizes ); print '</pre>'; }
Disable Divi Custom Post Type CSS file
Add this to your functions files to disable/remove the Divi CPT CSS file from being used.
/* Disable Divi CPT CSS */ function disable_cptdivi() { remove_action( 'wp_enqueue_scripts', 'et_divi_replace_stylesheet', 99999998 ); } add_action('init', 'disable_cptdivi'); add_filter( 'woocommerce_product_tabs', 'bbloomer_remove_product_tabs', 9999 );
How to enable WordPress debug mode and log errors
To enable error logging, create an empty file named “php-errors.log”. Place it in a directory that is not publicly accessible (preferably outside your web root) and ensure it is writable by the web server. Then add the following code to wp-config.php:
@ini_set('log_errors', 'On'); @ini_set('error_log', '/full/path/to/php-errors.log');
By default, only fatal errors and warnings will be logged. To also log notices and other messages, enable the WP_DEBUG option by adding this code:
//Report all types of errors. define('WP_DEBUG', true); //Don't show errors to site visitors. define('WP_DEBUG_DISPLAY', false);
Remove query strings from static resources in WordPress
Use this to remove query strings from static resources in WordPress
// Remove query strings from static resources function _remove_script_version( $src ){ $parts = explode( '?ver', $src ); return $parts[0]; } add_filter( 'script_loader_src', '_remove_script_version', 15, 1 ); add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
Recent Comments