
Bài viết tổng hợp những Function hay và được sử dụng nhiều trong WordPress, thay thế phần nào việc sử dụng thêm Plugin, giúp tối ưu tốc độ tải trang. . .
Kiểm tra định dạng số điện thoại 10 số Việt Nam
function check_wpcf7_tel( $result, $tel ) {
$result = preg_match( '/^(0|\+84)(\s|\.)?((3[2-9])|(5[689])|(7[06-9])|(8[1-689])|(9[0-46-9]))(\d)(\s|\.)?(\d{3})(\s|\.)?(\d{3})$/', $tel );
return $result;
}
add_filter( 'wpcf7_is_tel', 'check_wpcf7_tel', 10, 2 );
Code phân trang cho bài viết
//Code phan trang
function wiki_wp_corenavi($custom_query = null, $paged = null) {
global $wp_query;
if($custom_query) $main_query = $custom_query;
else $main_query = $wp_query;
$paged = ($paged) ? $paged : get_query_var('paged');
$big = 999999999;
$total = isset($main_query->max_num_pages)?$main_query->max_num_pages:'';
if($total > 1) echo '<div class="pagenavi">';
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, $paged ),
'total' => $total,
'mid_size' => '10', // Số trang hiển thị khi có nhiều trang trước khi hiển thị ...
'prev_text' => __('<i class="icon-angle-left"></i>','wiki'),
'next_text' => __('<i class="icon-angle-right"></i>','wiki'),
) );
if($total > 1) echo '</div>';
}
Thêm Panel trong Dashboard
add_action( 'admin_footer', 'rv_custom_dashboard_widget' );
function rv_custom_dashboard_widget() {
// Bail if not viewing the main dashboard page
if ( get_current_screen()->base !== 'dashboard' ) {
return;
}
?>
<div id="custom-id" style="display: none;border: 2px solid #52accc; padding: 0 20px">
<h2 style="margin-top:20px; text-transform:uppercase; color: #dd3333; margin-bottom:-10px">Về chúng tôi</h2>
<h3>CÔNG TY TNHH TM & DỊCH VỤ FOREST JUMP VIỆT NAM</h3>
<p style="color: #000"><strong>Địa chỉ: </strong>P3432 HH4B Linh Đàm - Hoàng Liệt - Hoàng Mai - Hà Nội</p>
<p style="color: #000"><strong>Hotline: </strong><a style="text-decoration:none" href="tel:0888805078">0888 805 078</a></p>
<p style="color: #000"><strong>Email: </strong><a style="text-decoration:none" href="mailto:lienhe@forestjump.com.vn">lienhe@forestjump.com.vn</a></p>
<p style="color: #000"><strong>Website: </strong><a href="https://forestjump.com.vn" style="text-decoration:none">Forestjump.com.vn</a></p>
</div>
<script>
jQuery(document).ready(function($) {
$('#welcome-panel').after($('#custom-id').show());
});
</script>
<?php }
Đổi font chữ từ pt sang px
function flatsome_editor_text_sizes($initArray){
$initArray['fontsize_formats'] = "9px 10px 12px 13px 14px 16px 17px 18px 19px 20px 21px 24px 28px 32px 36px";
return $initArray;
};
Thêm Page Options trong quản trị
// Page Options
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'Cài đặt chung', // Title hiển thị khi truy cập vào Options page
'menu_title' => 'Cài đặt chung', // Tên menu hiển thị ở khu vực admin
'menu_slug' => 'cai-dat-chung', // Url hiển thị trên đường dẫn của options page
'capability' => 'edit_posts',
'redirect' => false
));
}
Xóa thông báo kích hoạt Flatsome trong quản trị
add_action( 'init', 'hide_notice' );
function hide_notice() {
remove_action( 'admin_notices', 'flatsome_maintenance_admin_notice' );
}
Xóa trường thông tin trong form đặt hàng
add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1
);
function wc_npr_filter_phone( $address_fields ) {
$address_fields['billing_phone']['required'] = true;
$address_fields['billing_country']['required'] = false;
$address_fields['billing_last_name']['required'] = false;
$address_fields['billing_city']['required'] = false;
$address_fields['billing_postcode']['required'] = false;
$address_fields['billing_email']['required'] = false;
$address_fields['billing_state']['required'] = false;
$address_fields['billing_address_1']['required'] = true;
$address_fields['billing_address_2']['required'] = false;
return $address_fields;
}
//make shipping fields not required in checkout
add_filter( 'woocommerce_shipping_fields',
'wc_npr_filter_shipping_fields', 10, 1 );
function wc_npr_filter_shipping_fields( $address_fields ) {
$address_fields['shipping_first_name']['required'] = true;
$address_fields['shipping_last_name']['required'] = false;
$address_fields['shipping_phone']['required'] = true;
$address_fields['shipping_address_1']['required'] = true;
$address_fields['shipping_address_2']['required'] = false;
$address_fields['shipping_city']['required'] = false;
$address_fields['shipping_country']['required'] = false;
$address_fields['shipping_postcode']['required'] = false;
$address_fields['shipping_state']['required'] = false;
return $address_fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
unset($fields['billing']['billing_company']);
//unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_last_name']);
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_country']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_state']);
unset($fields['billing']['billing_postcode']);
//unset($fields['billing']['billing_email']);
unset($fields['shipping']['shipping_company']);
//unset($fields['billing']['billing_address_1']);
unset($fields['shipping']['shipping_last_name']);
unset($fields['shipping']['shipping_address_2']);
unset($fields['shipping']['shipping_country']);
unset($fields['shipping']['shipping_city']);
unset($fields['shipping']['shipping_state']);
unset($fields['shipping']['shipping_postcode']);
//unset($fields['shipping']['shipping_email']);
return $fields;
}
Tạo mã đơn Woocommer tùy chỉnh
//Custom Order Number
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
$prefix = 'BEAR';
$suffix = 'ONL';
$new_order_id = $prefix . $order_id . $suffix;
return $new_order_id;
}
Xóa thẻ H3 trong tiêu đề bình luận
add_filter( 'comment_form_defaults', 'custom_reply_title' );
function custom_reply_title( $defaults ){
$defaults['title_reply_before'] = '<span id="reply-title" class="h4 comment-reply-title">';
$defaults['title_reply_after'] = '</span>';
return $defaults;
}
Tắt Gutenberg Editor & Gutenberg Widget
// Disables the block editor from managing widgets in the Gutenberg plugin. add_filter( 'gutenberg_use_widgets_block_editor', '__return_false', 100 ); // Disables the block editor from managing widgets. renamed from wp_use_widgets_block_editor add_filter( 'use_widgets_block_editor', '__return_false' );
Dịch ngôn ngữ
//Dịch ngôn ngữ
function multi_change_translate_text( $translated ) {
$text = array(
'Thêm vào giỏ hàng' => 'Đặt mua',
'Add to cart' => 'Đặt mua',
'Mô tả' => 'Thông tin sản phẩm',
'Lựa chọn các tùy chọn' => 'Xem thêm',
'Cảm ơn bạn. Đơn hàng của bạn đã được nhận.' => 'Cảm ơn bạn! Chúng tôi đã nhận được đơn hàng của bạn.',
);
$translated = str_ireplace( array_keys( $text ), $text, $translated );
return $translated;
}
add_filter( 'gettext', 'multi_change_translate_text', 20 );
Thay khoảng giá bằng giá của biến thể
//Replace Price Range by Variation Price
add_action( 'woocommerce_before_single_product', 'move_variations_single_price', 1 );
function move_variations_single_price(){
global $product, $post;
if ( $product->is_type( 'variable' ) ) {
add_action( 'woocommerce_single_product_summary', 'replace_variation_single_price', 10 );
}
}
function replace_variation_single_price() {
?>
<style>
.woocommerce-variation-price {
display: none;
}
</style>
<script>
jQuery(document).ready(function($) {
var priceselector = '.product p.price';
var originalprice = $(priceselector).html();
$( document ).on('show_variation', function() {
$(priceselector).html($('.single_variation .woocommerce-variation-price').html());
});
$( document ).on('hide_variation', function() {
$(priceselector).html(originalprice);
});
});
</script>
<?php
}
//Save For simple products
add_action( 'woocommerce_single_product_summary', 'simple_product_saving_amount', 11 );
function simple_product_saving_amount() {
global $product;
if( $product->is_type('simple') && $product->is_on_sale() ) {
$regular_price = (float) wc_get_price_to_display( $product, array('price' => $product->get_regular_price() ) );
$active_price = (float) wc_get_price_to_display( $product, array('price' => $product->get_sale_price() ) );
$saved_amount = $regular_price - $active_price;
$percentage = round( $saved_amount / $regular_price * 100 );
echo '<p id="saving_rpice">'. __("Tiết kiệm") .': ' . wc_price($saved_amount) . ' ('.$percentage.'%)</p>';
}
}
//Save For product variations (on variable products)
add_filter( 'woocommerce_available_variation', 'variable_product_saving_amount', 10, 3 );
function variable_product_saving_amount( $data, $product, $variation ) {
if( $variation->is_on_sale() ) {
$saved_amount = $data['display_regular_price'] - $data['display_price'];
$percentage = round( $saved_amount / $data['display_regular_price'] * 100 );
$data['price_html'] .= '<p id="saving_rpice">'. __("Tiết kiệm") .': ' . wc_price($saved_amount) . ' ('.$percentage.'%)</p>';
}
return $data;
}
Thay 0đ bằng Liên hệ
// Thay 0đ bằng Liên hệ
function wiki_wc_custom_get_price_html( $price, $product ) {
if ( $product->get_price() == 0 ) {
if ( $product->is_on_sale() && $product->get_regular_price() ) {
$regular_price = wc_get_price_to_display( $product, array( 'qty' => 1, 'price' => $product->get_regular_price() ) );
$price = wc_format_price_range( $regular_price, __( 'Free!', 'woocommerce' ) );
} else {
$price = '<span class="amount">' . __( 'Liên hệ', 'woocommerce' ) . '</span>';
}
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'wiki_wc_custom_get_price_html', 10, 2 );
Thêm Widget Area
register_sidebar( array(
'name' => 'Contact Menu',
'id' => 'contactmenu',
'description' => 'Widget for Contact Menu',
'before_widget' => '<div class="contact-menu">',
'after_widget' => '</div>',
'before_title' => '<span class="widget-title">',
'after_title' => '</span>',
) );
Xóa Menu iTem trong trang quản trị
// admin_init action works better than admin_menu in modern wordpress (at least v5+)
add_action( 'admin_init', 'my_remove_menu_pages' );
function my_remove_menu_pages() {
global $user_ID;
if ( $user_ID != 1 ) { //your user id
remove_menu_page('edit.php'); // Posts
remove_menu_page('upload.php'); // Media
remove_menu_page('link-manager.php'); // Links
remove_menu_page('edit-comments.php'); // Comments
remove_menu_page('edit.php?post_type=page'); // Pages
remove_menu_page('plugins.php'); // Plugins
remove_menu_page('themes.php'); // Appearance
remove_menu_page('users.php'); // Users
remove_menu_page('tools.php'); // Tools
remove_menu_page('options-general.php'); // Settings
}
}
Ẩn thông báo trong trang quản trị
add_action('admin_head', 'bc_disable_notice'); function bc_disable_notice() { ?> <style> #message { display: none;} </style> <?php }
Tắt cập nhật tự động Theme và Plugin
Thêm vào functions.php
// Disable Auto Update Themes add_filter( 'auto_update_theme', '__return_false' ); // Disable Auto Update Plugins add_filter( 'auto_update_plugin', '__return_false' );
Tắt cài đặt Themes – Plugins
define('DISALLOW_FILE_MODS',true);











