WordPress终极优化指南–禁用WooCommerce样式表、购物车刷新、WooCommerce小部件、删除WooCommerce Meta Boxes

教程大全WooCommerce Meta Boxes,WooCommerce小部件,WooCommerce购物车刷新,WordPress优化,禁用WooCommerce样式表

WordPress终极优化指南–禁用WooCommerce样式表、购物车刷新、WooCommerce小部件、删除WooCommerce Meta Boxes

教程总目录:WordPress终极优化指南

WooCommerce是一个知名的购物系统主题

禁用WooCommerce样式表

当您安装WooCommerce时,它会为您的网站添加一些样式表,这些样式表随页面一起加载。如果您使用自定义主题(可能),则可以停止加载这些样式表。

add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );

写入functions.php保存即可。

如果要将自己的样式表排入队列,使用下方代码

/**  Enqueue your own stylesheet */
function wp_enqueue_woocommerce_style(){
  wp_register_style( 'mytheme-woocommerce', get_template_directory_uri().'/css/woocommerce.css' );
  if ( class_exists( 'woocommerce' ) ) {
    wp_enqueue_style( 'mytheme-woocommerce' );
  }
}
add_action( 'wp_enqueue_scripts', 'wp_enqueue_woocommerce_style' );

写入functions.php保存即可。

购物车刷新

WooCommerce内置功能可以在不刷新页面的情况下用户添加了新的商品后自动刷新购物车。

这个体验不错,但是加载的时间比较长。所以说各有利弊。在大型网站上,WooCommerce的购物车刷新可能需要好几秒钟。这个功能不是必要的,我们可以禁用他。

add_action( 'wp_print_scripts', 'nuke_cart_fragments', 100 );
function nuke_cart_fragments() {
  wp_dequeue_script( 'wc-cart-fragments' );
  return true;
}
add_action( 'wp_enqueue_scripts', 'dequeue_woocommerce_cart_fragments', 11);
function dequeue_woocommerce_cart_fragments() {
  if (is_front_page()) wp_dequeue_script('wc-cart-fragments');
}

写入functions.php保存即可。

注意:禁用后购物车不会自动刷新,但是刷新页面时购物车会自动更新到最新的商品列表。

WooCommerce小部件

WooCommerce默认预先加载了许多小部件。大多数主题都有自己的功能来替换这些小部件,并且保持它们的启用会增加加载时间,同时不提供任何其他价值。使用下方代码来禁用

add_action( 'widgets_init', 'remove_woo_widgets' );
function remove_woo_widgets() {
  unregister_widget( 'WC_Widget_Recent_Products' );
  unregister_widget( 'WC_Widget_Featured_Products' );
  unregister_widget( 'WC_Widget_Product_Categories' );
  unregister_widget( 'WC_Widget_Product_Tag_Cloud' );
  unregister_widget( 'WC_Widget_Cart' );
  unregister_widget( 'WC_Widget_Layered_Nav' );
  unregister_widget( 'WC_Widget_Layered_Nav_Filters' );
  unregister_widget( 'WC_Widget_Price_Filter' );
  unregister_widget( 'WC_Widget_Product_Search' );
  unregister_widget( 'WC_Widget_Top_Rated_Products' );
  unregister_widget( 'WC_Widget_Recent_Reviews' );
  unregister_widget( 'WC_Widget_Recently_Viewed' );
  unregister_widget( 'WC_Widget_Best_Sellers' );
  unregister_widget( 'WC_Widget_Onsale' );
  unregister_widget( 'WC_Widget_Random_Products' );
}

写入functions.php保存即可。

删除WooCommerce Meta Boxes

当您安装WooCommerce时,它会在您网站的后端添加许多元框。这些元框用途很少,您可以禁用它们以改善后端加载时间。否则的话我们后台页面大概时间将会较长

add_action('edit_form_after_title', 'my_custom_post_edit_form', 100);
/**
* remove all meta boxes, and display the form
*/
function my_custom_post_edit_form($post) {
  global $wp_meta_boxes;
  // remove all meta boxes
  $wp_meta_boxes = array('my_custom_post_type' => array(
    'advanced' => array(),
    'side' => array(),
    'normal' => array(),
  ));
  // show my admin form
  require dirname(__FILE__) . '/views/my-custom-post-edit-form.php';
}

写入functions.php保存即可。

Posted by 柴郡猫