File: /home/dkhp464/public_html/wp-content/themes/DaKhoaHongPhat/functions.php
<?php
/* ==========ADD-CSS-THEME======== */
add_action('wp_enqueue_scripts', 'simple_theme_register_style');
function simple_theme_register_style()
{
// $cssUrl = get_template_directory_uri() . '/assets/css';
// wp_register_style('simple-theme-style', $cssUrl . '/style.css', array(), '1.0');
// wp_enqueue_style('simple-theme-style');
}
/* ==========ADD-JS======== */
add_action('wp_enqueue_scripts', 'simple_theme_register_js');
function simple_theme_register_js()
{
$jsUrl = get_template_directory_uri() . '/assets/js';
// wp_register_script('simple-theme-style-bundle-min', $jsUrl . '/bootstrap.bundle.min.js', array(), '1.0', true);
wp_enqueue_script('simple-theme-style-bundle-min');
}
/* ==========ADD-FONT======== */
function enqueue_custom_fonts()
{
wp_enqueue_style('custom-fonts', 'https://fonts.googleapis.com/css2?family=Inter:wght@100;200;300;400;500;600;700;800;900&display=swap" rel="stylesheet');
}
add_action('wp_enqueue_scripts', 'enqueue_custom_fonts');
/* ==========ADD-WIDGET======== */
/* SĐT Email*/
define('PRICE','299K');
define('PHONE','0238 851 5555');
define('ZALO','0785329039');
define('PHONE_NEW', '0238 851 5555');
define('SDT','0238 851 5555');
define('SDT_FM','0238 851 5555');
define('EMAIL','');
define('ADDRESS', 'Số 99, Đường Lê Lợi, Phường Lê Lợi, Thành phố Vinh, Tỉnh Nghệ An');
define('GOOGLE_MAP', 'https://maps.google.com/?q='.ADDRESS);
define('OPEN_HOURS','07:30 - 19:30');
define('FACEBOOK','#');
define('LIVE_CHAT','https://mxv.zoosnet.net/LR/Chatpre.aspx?id=MXV15145056&lng=en');
define('LIVE_CHAT_URL','https://mxv.zoosnet.net/JS/LsJS.aspx?siteid=MXV15145056&float=1&lng=en');
define('TEN_PK', 'Phòng khám đa khoa Lê Lợi');
define('KHUVUC', 'TP Vinh');
define('OPEN_TIME','07:30 - 19:30');
define('IS_PUBLISH', isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? true : false);
function dump($var)
{
echo "<pre>";
var_dump($var);
echo "</pre>";
}
if (!function_exists('theme_setup')) {
function theme_setup()
{
add_theme_support('title-tag');
/* Them post thumbnail */
add_theme_support('post-thumbnails');
/* Post Format */
add_theme_support('post-formats', array(
'image',
'video',
'gallery',
'quote',
'link'
));
add_image_size('crop_thumbnail_sidebar', 87, 87, true);
add_image_size('crop_thumbnail', 306, 204, true);
add_image_size('crop_medium', 471, 314, true);
add_image_size('crop_large', 600, 400, true);
}
add_action('init', 'theme_setup');
}
function lighthouse() {
if (!isset($_SERVER['HTTP_USER_AGENT']) || stripos($_SERVER['HTTP_USER_AGENT'], 'Lighthouse') === false):
return true;
endif;
return false;
}
add_action( 'init', 'lighthouse' );
function register_my_menus()
{
register_nav_menus(
array(
'primary_menu' => __('Primary Menu'),
)
);
}
add_action('after_setup_theme', 'register_my_menus');
// ===============ADD-EVENT-ONCLICK===============
function custom_navigation_menu()
{
wp_nav_menu(
array(
'theme_location' => 'primary-menu', // Tên của vị trí menu trong theme
'container' => 'div',
'container_class' => 'collapse navbar-collapse',
'container_id' => 'navbarSupportedContent',
'menu_class' => 'navbar-nav me-auto mb-2 mb-lg-0',
'fallback_cb' => false,
)
);
}
add_action('init', 'register_my_menus');
function paginator_wp()
{
$args = array(
'prev_next' => true,
'prev_text' => 'Trước',
'next_text' => ' Sau ',
'mid_size' => 1,
'type' => 'array',
);
$pages = paginate_links($args);
$temp = $pages;
if ($temp != null) :
foreach ($temp as $key => $page) {
$pages[$key] = str_replace("page-numbers", "page-link", $page);
}
endif;
if (is_array($pages)) {
$paged = (get_query_var('paged') == 0) ? 1 : get_query_var('paged');
echo '<nav aria-label="..." class="mt-5"><ul class="pagination justify-content-center">';
// foreach ($pages as $page) {
// echo '<li class="page-item">' . $page . '</li>';
// }
//
foreach ($pages as $page) {
$current_class = '';
if (strpos($page, 'current') !== false) {
$current_class = ' active';
}
echo '<li class="page-item' . $current_class . '">' . $page . '</li>';
}
echo '</ul></nav>';
}
}
function wpdocs_excerpt_more($more)
{
return '...';
}
add_filter('excerpt_more', 'wpdocs_excerpt_more');
// function to display number of posts.
function getPostViews($postID)
{
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if ($count == '') {
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0";
}
return $count;
}
// function to count views.
function setPostViews($postID)
{
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if ($count == '') {
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
} else {
$count++;
update_post_meta($postID, $count_key, $count);
}
}
// >>> Show bai viet lien quan boi tu khoa tren top noi dung bai post
//add_filter('the_content', 'prefix_insert_post_ads');
// function prefix_insert_post_ads($content)
// {
// $str = '';
// if (is_single()) :
// $tags = wp_get_post_tags(get_the_ID());
// if ($tags) {
// foreach ($tags as $tag) :
// $tagid = $tag->term_id;
// $args = array(
// 'tag__in' => array($tagid),
// 'post__not_in' => array(get_the_ID()),
// 'orderby' => 'rand',
// 'post_status' => 'publish',
// );
// $my_query = new WP_Query($args);
// if ($my_query->have_posts()) {
// $str .= '<div class="p-3 mb-3 mt-3 border border-warning" style="border-radius:3px;"><ul class="mb-0 list-unstyled">';
// $i = 1;
// while ($my_query->have_posts()) : $my_query->the_post();
// $str .= '<li class="py-2 px-1"><a href="' . get_the_permalink() . '" class="" rel="bookmark" title="' . get_the_title() . '">» ' . get_the_title() . '</a></li>';
// if ($i == 3) break; // Gioi han bai viet
// $i++;
// endwhile;
// $str .= '</ul></div>';
// break; // Thoat vong lap neu tag co bai viet
// }
// endforeach;
// wp_reset_query();
// }
// return prefix_insert_after_paragraph($str, 1, $content);
// endif;
// return $content;
// }
// function prefix_insert_after_paragraph($insertion, $paragraph_id, $content)
// {
// $closing_p = '</p>';
// $paragraphs = explode($closing_p, $content);
// foreach ($paragraphs as $index => $paragraph) {
// if (trim($paragraph)) {
// $paragraphs[$index] .= $closing_p;
// }
// if ($paragraph_id == $index + 1) {
// $paragraphs[$index] .= $insertion;
// }
// }
// return implode('', $paragraphs);
// }
// <<< Show bai viet lien quan boi tu khoa tren top noi dung bai post
add_filter('use_block_editor_for_post', '__return_false'); // Tắt editor mới
/* Tạo Shortcode Tư Vấn */
add_shortcode('tuvan', function () {
$tuvan = '<span class="d-block text-center my-5"><a class="tu-van-ads" href="' . LIVE_CHAT . '" target="_blank"
><img class="khung-chat-bai-viet img-fluid h-auto" src="' . get_template_directory_uri() . '/image/tu-van-truc-tuyen-mien-phi.gif" width="482" height="166" alt="Tư Vấn Trực Tuyến Miễn Phí" /></a></span>';
return $tuvan;
});
add_shortcode('tuvantt', function () {
$tuvantt = '<a class="tu-van-ads" href="' . LIVE_CHAT . '" target="_blank"><strong><span style="color: #ff0000;">>>Tư Vấn Trực Tuyến<<</span></strong></a>';
return $tuvantt;
});
add_shortcode('sdt', function () {
$sodt = 'Hotline: <a class="tu-van-ads" href="tel:' . PHONE_NEW . '" target="_blank"><strong><span style="color: #ff0000;">' . PHONE . '</span></strong></a>';
return $sodt;
});
add_shortcode('click_bangchat', function () {
$str = '<a class="tu-van-ads" href="' . LIVE_CHAT . '" target="_blank"><span style="color: #008000;"><strong>click vào BẢNG CHAT</strong></span></a>';
return $str;
});
add_shortcode('bacsituvan', function () {
$str = '<span class="d-block text-center my-5"><a class="tu-van-ads" href="' . LIVE_CHAT . '" target="_blank"><img class="khung-chat-bai-viet img-fluid" src="' . get_template_directory_uri() . '/assets/img/bac-si-tu-van.gif" alt="Tư Vấn Trực Tuyến Miễn Phí" width="664" height="388" /></a></span>';
return $str;
});
add_shortcode('open_time', function () {
$opentime = '<span style="color: #ff0000;">' . OPEN_TIME . '</span>';
return $opentime;
});
add_shortcode('diachi', function () {
$str = ADDRESS;
return $str;
});
add_shortcode('tenpk', function () {
$tenpk = '<span>' . TEN_PK . '</span>';
return $tenpk;
});
add_shortcode('khuvucpk', function () {
$khuvucpk = '<span>' . KHUVUC . '</span>';
return $khuvucpk;
});
add_shortcode('btn_tuvan', function () {
$btn_tuvan = '<span class="d-block text-center my-5"><a class="tu-van-ads" href="' . LIVE_CHAT . '" target="_blank"
><img class="khung-chat-bai-viet img-fluid h-auto" src="' . get_template_directory_uri() . '/assets/img/nut-tu-van.gif" width="120" height="35" alt="Tư Vấn Trực Tuyến Miễn Phí" /></a></span>';
return $btn_tuvan;
});
// hooks your functions into the correct filters
function custom_add_mce_button()
{
// check user permissions
if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) {
return;
}
// check if WYSIWYG is enabled
if ('true' == get_user_option('rich_editing')) {
add_filter('mce_external_plugins', 'custom_add_tinymce_plugin');
add_filter('mce_buttons_3', 'custom_register_mce_button');
}
}
add_action('init', 'custom_add_mce_button');
// register new button in the editor
function custom_register_mce_button($buttons)
{
array_push($buttons, 'custom_mce_button');
return $buttons;
}
// declare a script for the new button
// the script will insert the shortcode on the click event
function custom_add_tinymce_plugin($plugin_array)
{
$plugin_array['custom_mce_button'] = get_stylesheet_directory_uri() . '/assets/js/shortcode.js';
return $plugin_array;
}
// Disable use XML-RPC
add_filter('xmlrpc_enabled', '__return_false');
// submit form
add_action('wp_ajax_submit_form', 'submit_form');
add_action('wp_ajax_nopriv_submit_form', 'submit_form');
function submit_form()
{
$result = array(
'result' => false,
'data' => [],
);
$hasCheck = true;
$error = [];
if (isset($_POST['data'])) {
$data = array();
parse_str($_POST['data'], $data);
} else {
echo json_encode($result);
die();
}
if (isset($data['check'])) {
foreach ($data['check'] as $key => $item) {
if (!isset($item['ans'])) {
$hasCheck = false;
break;
}
}
}
if ($hasCheck == false) {
$error['check'] = 'Chưa chọn hết đáp án';
}
if (isset($data['phone'])) {
$pattern = "/((09|03|07|08|05)+([0-9]{8})\b)/i";
$number = str_replace(array('-', '.', ' '), '', $data['phone']);
if (preg_match($pattern, $number) == 0) {
$error['phone'] = 'Số điện thoại không hơp lệ';
}
}
$message = '';
if (isset($data['message'])) {
$message = $data['message'];
}
if (count($error) > 0) {
$result['data'] = $error;
echo json_encode($result);
die();
}
if (isset($data['check'])) {
foreach ($data['check'] as $key => $item) {
if (is_array($item['ans'])) {
$ansArr = implode(', ', $data['check'][$key]['ans']);
$data['check'][0]['ans'] = $ansArr;
}
}
}
$get_client_ip = get_client_ip_new();
$curl_data = array(
"name" => isset($data['name']) ? $data['name'] : '', //
"contactno" => isset($data['phone']) ? $data['phone'] : '', //
"message" => isset($data['check']) ? json_encode($data['check']) : $message, //
"IP" => $get_client_ip, //
"referer" => isset($data['url']) ? $data['url'] : '', //
"bookdate" => $data['date'] ? $data['date'] : '' //
);
//echo json_encode($curl_data);
$ch = curl_init("https://leloi.tuvanbienhoa.com/api_phone.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_data);
$curl_result = curl_exec($ch);
$result['result'] = true;
$result['data'] = $curl_result;
echo json_encode($result);
die();
}
function get_client_ip_new()
{
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if (getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if (getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if (getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if (getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if (getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
function theme_archive_title()
{
$title = '';
if (is_category()) {
$title = single_cat_title('', false);
} elseif (is_tag()) {
$title = single_tag_title('', false);
} elseif (is_author()) {
$title = '<span class="vcard">' . get_the_author() . '</span>';
} elseif (is_post_type_archive()) {
$title = post_type_archive_title('', false);
} elseif (is_tax()) {
$title = single_term_title('', false);
} elseif (is_home()) {
$title = single_post_title('', false);
} elseif (is_archive()) {
$title = single_cat_title('', false);
} elseif (is_single()) {
$title = single_post_title('', false);
} elseif (is_page()) {
$title = single_post_title('', false);
} elseif (is_search()) {
$title = "Kết quả tìm kiếm";
} elseif (is_404()) {
$title = "404";
}
return $title;
}
// add_action('before_delete_post', function ($id) {
// $attachments = get_attached_media('', $id);
// foreach ($attachments as $attachment) {
// wp_delete_attachment($attachment->ID, 'true');
// }
// });
function my_init()
{
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery', false);
}
}
add_action('init', 'my_init');
// Don't load Gutenberg-related stylesheets.
add_action('wp_enqueue_scripts', 'remove_block_css', 100);
function remove_block_css()
{
wp_dequeue_style('wp-block-library'); // Wordpress core
wp_dequeue_style('wp-block-library-theme'); // Wordpress core
wp_dequeue_style('wc-block-style'); // WooCommerce
wp_dequeue_style('storefront-gutenberg-blocks'); // Storefront theme
}
add_action('wp_enqueue_scripts', 'mywptheme_child_deregister_styles', 20);
function mywptheme_child_deregister_styles()
{
wp_dequeue_style('classic-theme-styles');
wp_dequeue_style('classic-theme-styles-css');
}
//
function list_category_post($id)
{
$category_detail = get_the_category($id);
$arr = [];
foreach ($category_detail as $cd) {
$arr[] = '<a title="' . $cd->cat_name . '" class="text-black" href="' . get_category_link($cd->cat_ID) . '">' . $cd->cat_name . '</a>';
}
return implode(', ', $arr);
}
function custom_rank_math_breadcrumbs_separator($separator)
{
return '//';
}
add_filter('rank_math/breadcrumbs/separator', 'custom_rank_math_breadcrumbs_separator');
/* Tạo Shortcode Tư Vấn */
add_shortcode('tuvan', function () {
$tuvan = '<span class="d-block text-center my-5"><a class="tu-van-ads" href="' . LIVE_CHAT . '" target="_blank"><img class="khung-chat-bai-viet" src="' . get_template_directory_uri() . '/image/tu-van-truc-tuyen-mien-phi.gif" width="482" height="166" alt="Tư Vấn Trực Tuyến Miễn Phí" /></a></span>';
return $tuvan;
});
add_shortcode('tuvantt', function () {
$tuvantt = '<a class="tu-van-ads" href="' . LIVE_CHAT . '" target="_blank"><strong><span style="color: #ff0000;">>>Tư Vấn Trực Tuyến<<</span></strong></a>';
return $tuvantt;
});
add_shortcode('sdt', function () {
$sodt = 'Hotline: <a class="tu-van-ads" href="tel:' . PHONE . '" target="_blank"><strong><span style="color: #ff0000;">' . PHONE . '</span></strong></a>';
return $sodt;
});
add_shortcode('click_bangchat', function () {
$str = '<a class="tu-van-ads" href="' . LIVE_CHAT . '"><span style="color: #008000;"><strong>click vào BẢNG CHAT</strong></span></a>';
return $str;
});
add_shortcode('bacsituvan', function () {
$str = '<span class="d-block text-center my-5"><a class="tu-van-ads" href="' . LIVE_CHAT . '" target="_blank"><img class="khung-chat-bai-viet" src="' . get_template_directory_uri() . '/image/bac-si-tu-van.gif" alt="Tư Vấn Trực Tuyến Miễn Phí" /></a></span>';
return $str;
});
add_shortcode('open_time', function () {
$opentime = '<span style="color: #ff0000;">' . OPEN_TIME . '</span>';
return $opentime;
});
add_shortcode('diachi', function () {
$str = ADDRESS;
return $str;
});
add_shortcode('tenpk', function () {
$tenpk = '<span>' . TEN_PK . '</span>';
return $tenpk;
});
add_shortcode('khuvucpk', function () {
$khuvucpk = '<span>' . KHUVUC . '</span>';
return $khuvucpk;
});
add_shortcode('zalo', function () {
$zalo = preg_replace('/\s+/', '', ZALO);;
$khuvucpk = '<a href="http://zalo.me/' . $zalo . '">(ZALO: ' . ZALO . ')</a>';
return $khuvucpk;
});
require get_template_directory() . '/inc/add-gif.php';
require get_template_directory() . '/inc/add-form-test.php';