6 Great PHP code snippets for WordPress
When creating a WordPress website (like this one), I like to have some parts to work differently from default. For instance, you can’t use Gutenberg in WooCommerce, which means a description has the same boring thing as the next WooCommerce website. But there’s a way to enable this. Therefore you’ll need code snippets.
Code snippets are (mostly small) pieces of code that can be copied and pasted to work on any website. These usually go into your child themes function.php. Now the issue I’ve with this, is that you’ve to create a child theme every time you change the theme (or that’s how it was) and you don’t have easy access to different kinds of code snippets to enable/disable them temporarily. No, you have to (un)comment them in the php code.
A plugin called code snippets fixes those issues and the best thing is, it’s free!
Useful WordPress admin PHP code snippets
Infinite media library scrolling
<?php
add_filter( 'media_library_infinite_scrolling', '__return_true' );
Useful WooCommerce PHP code snippets
Gutenberg for Woocommerce product pages
function activate_gutenberg_product( $can_edit, $post_type ) {
if ( $post_type == 'product' ) {
$can_edit = true;
}
return $can_edit;
}
add_filter( 'use_block_editor_for_post_type', 'activate_gutenberg_product', 10, 2 );
Move add to card button underneath the price for Woocommerce product pages
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 10 );
Make Woocommerce product not purchasable
add_filter( 'woocommerce_is_purchasable', '__return_false' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
Disable description/editor for Woocommerce products
function reset_editor()
{
global $_wp_post_type_features;
$post_type="product";
$feature = "editor";
if ( !isset($_wp_post_type_features[$post_type]) )
{
}
elseif ( isset($_wp_post_type_features[$post_type][$feature]) )
unset($_wp_post_type_features[$post_type][$feature]);
}
add_action("init","reset_editor");