Các đoạn code functions.php hữu dụng rất thường dùng cho website WordPress (có cập nhật)

Tháng Tám 9, 2021

1. Ẩn đòi bản quyền Flatsome

// Ẩn đòi bản quyền Flatsome
add_action(‘admin_head’, ‘vietcoders_flatsome_issues’);function vietcoders_flatsome_issues() {echo ‘<style>div#flatsome-notice {display: none;}</style>’;}

2. Tự động đổi ALT ảnh theo TITLE bài đăng

//Tự động đổi ALT ảnh theo TITLE bài đăng
add_filter(‘wp_get_attachment_image_attributes’, ‘change_attachement_image_attributes’, 20, 2);
function change_attachement_image_attributes( $attr, $attachment ){
$parent = get_post_field( ‘post_parent’, $attachment);
$type = get_post_field( ‘post_type’, $parent);
if( $type != ‘product’ ){
return $attr;
}
$title = get_post_field( ‘post_title’, $parent);
$attr[‘alt’] = $title;
$attr[‘title’] = $title;
return $attr;
}

3. Tạo Sku Woocommerce Tự Động

//Tạo Sku Woocommerce Tự Động
function create_sku_from_product_id($product_id){
$sku = ”;
if(strlen($product_id) == 1){
$sku = ‘A00’.$product_id;
}elseif(strlen($product_id) == 2){
$sku = ‘A0’.$product_id;
}else{
$sku = ‘A’.$product_id;
}
return $sku;
}
function auto_create_sku_after_post_product( $post_id, $post ) {
if($post->post_type == “product”){
$sku = create_sku_from_product_id($post_id);
update_post_meta($post_id,’_sku’,$sku);
}
}
add_action( ‘save_post’,’auto_create_sku_after_post_product’, 20, 2 );

4. Ẩn thanh Header trong Woocomercer giúp cho sửa sản phẩm dễ dàng hơn

// Disable Woocommerce Header in WP Admin
add_action(‘admin_head’, ‘Hide_WooCommerce_Breadcrumb’);
function Hide_WooCommerce_Breadcrumb() {
  echo ‘<style>
    .woocommerce-layout__header {
        display: none;
    }
    .woocommerce-layout__activity-panel-tabs {
        display: none;
    }
    .woocommerce-layout__header-breadcrumbs {
        display: none;
    }
    .woocommerce-embed-page .woocommerce-layout__primary{
        display: none;
    }
    .woocommerce-embed-page #screen-meta, .woocommerce-embed-page #screen-meta-links{top:0;}
    </style>’;
}
5/5 - (1 bình chọn)

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Contact Me on Zalo
0972.78.22.55