Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrii-marushchak/2a4e164d646c5d44fc9fa801415dd88e to your computer and use it in GitHub Desktop.
Save andrii-marushchak/2a4e164d646c5d44fc9fa801415dd88e to your computer and use it in GitHub Desktop.
PHP & WordPress Snippets
define('BROWSER_AVIF', strpos($_SERVER['HTTP_ACCEPT'], 'image/avif') !== false);
define('BROWSER_WEBP', strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') !== false);
add_action('admin_init', function () {
// Redirect any user trying to access comments page
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url());
exit;
}
// Remove comments metabox from dashboard
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
// Disable support for comments and trackbacks in post types
foreach (get_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');
}
}
});
// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);
// Remove comments page in menu
add_action('admin_menu', function () {
remove_menu_page('edit-comments.php');
});
// Remove comments links from admin bar
add_action('init', function () {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
});
// Complete disable
add_filter('use_block_editor_for_post', '__return_false', 10);
add_filter('use_block_editor_for_post_type', '__return_false', 10);
// Enable only for posts
add_filter( 'use_block_editor_for_post', '__return_false' );
add_filter( 'use_block_editor_for_post', 'my_disable_gutenberg', 10, 2 );
function my_disable_gutenberg( $can_edit, $post ) {
if( $post->post_type == 'post' ) {
return true;
}
return false;
}
// Enable for certain template
add_filter( 'use_block_editor_for_post', 'my_disable_gutenberg', 10, 2 );
function my_disable_gutenberg( $can_edit, $post ) {
if( $post->post_type == 'page' &&
get_page_template_slug( $post->ID ) == 'page-gutenberg.php' ) {
return true;
}
return false;
}
// Keep GET params for Polylang links
function pll_url_query_string( $url ) {
if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
return $url . '?' . $_SERVER['QUERY_STRING'];
}
return $url;
}
add_filter( 'pll_the_language_link', 'pll_url_query_string' );
function emlaw_remove_cpt_slug_from_url( $post_link, $post ) {
if ( 'practice-areas' === $post->post_type && 'publish' === $post->post_status ) {
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'emlaw_remove_cpt_slug_from_url', 10, 2 );
function emlaw_parse_cpt_page_on_url_without_slug( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'practice-areas', 'page' ) );
}
}
add_action( 'pre_get_posts', 'emlaw_parse_cpt_page_on_url_without_slug' );
// Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php)
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
ob_start();
?>
<a class="cart-contents" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
// Get The Page ID You Need
get_option( 'woocommerce_shop_page_id' );
get_option( 'woocommerce_cart_page_id' );
get_option( 'woocommerce_checkout_page_id' );
get_option( 'woocommerce_pay_page_id' );
get_option( 'woocommerce_thanks_page_id' );
get_option( 'woocommerce_myaccount_page_id' );
get_option( 'woocommerce_edit_address_page_id' );
get_option( 'woocommerce_view_order_page_id' );
get_option( 'woocommerce_terms_page_id' );
function gg_exclude_tablets_from_wp_is_mobile( $is_mobile ) {
if (
strpos($_SERVER['HTTP_USER_AGENT'], 'iPad') !== false ||
strpos($_SERVER['HTTP_USER_AGENT'], 'Tablet') !== false
) {
$is_mobile = false;
}
return $is_mobile ;
}
add_filter( 'wp_is_mobile', 'gg_exclude_tablets_from_wp_is_mobile' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment