Hướng dẫn ghi đè (override) folder INC trong child-theme Flatsome

Trong WordPress nói chung và theme Flatsome nói riêng, có cơ chế tạo folder child-theme để chỉnh sửa được thuận tiện, không bị mất code khi update theme. Trong theme Flatsome chúng ta có thể ghi đè (override) được gần như toàn bộ các file, ngoại trừ chỉ có folder INC.

Hướng Dẫn Ghi đè (override) Folder Inc Trong Child Theme Flatsome
Hướng Dẫn Ghi đè (override) Folder Inc Trong Child Theme Flatsome

Để có thể ghi đè (override) inc trong theme Flatsome, các bạn áp dụng các bước sau, ở đây tôi sẽ ví dụ với file /inc/ux_products.php:

Hướng Dẫn Ghi đè (override) Folder Inc Trong Child Theme Flatsome
Hướng Dẫn Ghi đè (override) Folder Inc Trong Child Theme Flatsome

Bước 1. Sao chép file & folder

Sao chép /inc từ theme Flatsome sang folder child-theme Flatsome (file nào cần thì sao chép, không thì copy tất cả cho nhanh)

Bước 2. Đổi tên function

Mở file /inc/ux_products.php (hoặc file cần sửa) và đổi tên function từ như này

function ux_products($atts, $content = null, $tag) {}

sang như này

function child_ux_products($atts, $content = null, $tag) {}

Bước 3. Đổi lại shortcode

Kéo xuống cuối file và chỉnh sửa từ như này

add_shortcode("ux_bestseller_products", "ux_products");
add_shortcode("ux_featured_products", "ux_products");
add_shortcode("ux_sale_products", "ux_products");
add_shortcode("ux_latest_products", "ux_products");
add_shortcode("ux_custom_products", "ux_products");
add_shortcode("product_lookbook", "ux_products");
add_shortcode("products_pinterest_style", "ux_products");
add_shortcode("ux_products", "ux_products");

sang như này

add_shortcode("ux_bestseller_products", "child_ux_products");
add_shortcode("ux_featured_products", "child_ux_products");
add_shortcode("ux_sale_products", "child_ux_products");
add_shortcode("ux_latest_products", "child_ux_products");
add_shortcode("ux_custom_products", "child_ux_products");
add_shortcode("product_lookbook", "child_ux_products");
add_shortcode("products_pinterest_style", "child_ux_products");
add_shortcode("ux_products", "child_ux_products");

Bước 4. Khai báo ghi đè đường dẫn file

Mở functions.php và copy code sau

function vdh_override_shortcodes()
{
    if(is_woocommerce_activated())
    {
        require get_stylesheet_directory() . '/inc/shortcodes/ux_products.php';
        // Thêm file tương tự cần sửa vào đây
    }
}
add_action('wp_loaded', 'vdh_override_shortcodes', 10);

Lưu ý rằng, khi theme cha được cập nhật, các thay đổi của trong child-theme sẽ không bị mất. Tuy nhiên, nếu các thay đổi trong theme cha liên quan đến các file trong thư mục /inc, bạn có thể cần phải cập nhật lại các file tương ứng trong child-theme của bạn để đảm bảo tính tương thích của theme và child-theme.

Nguồn : https://vuduchong.com

Đánh giá post

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 *

Hotline: 0974.0707.83(Zalo/Viber)