Như đã giới thiệu với các bạn Hook trong WordPress ở phần 1 và hook phần 2 . Hôm nay tớ giới thiệu nốt các hook còn lại trong bộ hook của WordPress là Hook trong blog post . Những hook này là tớ sưu tầm thường hay dùng nhưng lại ít người biết đến – Các bạn cùng tham khảo bài bên dưới nhé
Mục lục bài viết:
Các hook trong blog post bài viết WordPress
Trước khi biết đến các vị trí hook ta tìm hiểu qua cấu trúc HOOK trong wordpress
Code 1 cấu trúc hook cơ bản có dạng (1)
function mo_ta_hook() {
echo '<p>MÔ TẢ HOOK HOẶC CODE CẦN HOOK Ở ĐÂY</p>';
}
add_action('VI_TRI_HOOK', 'mo_ta_hook');
Code 2 cấu trúc hook cơ bản có dạng (2)
function mo_ta_hook() { ?>
Mô tả bằng text hoặc HTML hoặc PHP tại đây
<?php }
add_action('VI_TRI_HOOK', 'mo_ta_hook');
CÁC VỊ TRÍ HOOK TRONG BLOG
HOOK vào trước và sau tiêu đề bài viết
– flatsome_blog_post_before (hook nằm trên title)
– flatsome_blog_post_after (hook nằm dưới cùng)
Có dạng
//hook vào cuối chuỗi tiêu đề
function add_string_to_title($content){
return $content . ' - Flatsome.xyz - là nơi chia sẽ thủ thuật WordPress';
}
add_filter("the_title", "add_string_to_title",10,1);
Hook single blog post flatsome (chi tiết tin tức)
Trong chi tiết blog của flatsome thì có 2 hook cơ bản như sau:
the_content
the_title
Hook trước nội dung bài viết
function hook_before_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
if ( $GLOBALS['post']->ID == get_the_ID() ) {
$cdblog_content = 'flatsome.xyz - Trước nội dung bài viết ';
$cdblog_content .= $content;
}
return $cdblog_content;
}
}
add_filter( 'the_content', 'hook_before_content' );
Ví dụ về thêm text sau nội dung
function aftercontent( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
if ( $GLOBALS['post']->ID == get_the_ID() ) {
$content .= 'Nội dung bạn muốn thêm vào sau bài viết';
}
}
return $content;
}
add_filter('the_content', 'aftercontent');
Ví dụ về thêm text ở giữa content theo từng đoạn
add_filter('the_content', 'contentafter2para');
function contentafter2para($content){
if(is_single()){
$congdongblog_content = 'Nội dung chen vào giữa blog từ đoạn số 2';
$after = 2; //số đoạn bạn muốn hiển thị
$end = '</p>';
$content_congdongblog = explode($end, $content);
foreach($content_congdongblog as $key => $cont){
if(trim($cont)) {
$content_congdongblog[$key] .= $end;
}
if(($key + 1) == $after){
$content_congdongblog[$key] .= $congdongblog_content;
}
}
$content = implode('', $content_congdongblog);
}
return $content;
}
Cách vị trí hook khác có thể tham khảo
add_action( 'wp_head', 'rest_output_link_wp_head', 10, 0 );
add_action( 'wp_head', '_wp_render_title_tag', 1 );
add_action( 'wp_head', 'wp_enqueue_scripts', 1 );
add_action( 'wp_head', 'wp_resource_hints', 2 );
add_action( 'wp_head', 'feed_links', 2 );
add_action( 'wp_head', 'feed_links_extra', 3 );
add_action( 'wp_head', 'rsd_link' );
add_action( 'wp_head', 'wlwmanifest_link' );
add_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
add_action( 'wp_head', 'locale_stylesheet' );
add_action( 'wp_head', 'noindex', 1 );
add_action( 'wp_head', 'print_emoji_detection_script', 7 );
add_action( 'wp_head', 'wp_print_styles', 8 );
add_action( 'wp_head', 'wp_print_head_scripts', 9 );
add_action( 'wp_head', 'wp_generator' );
add_action( 'wp_head', 'rel_canonical' );
add_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
add_action( 'wp_head', 'wp_custom_css_cb', 101 );
add_action( 'wp_head', 'wp_site_icon', 99 );
add_action( 'wp_head', 'wp_no_robots' );
Hiện thông báo với use (không phải là admin)
// show a maintenance message for all your site visitors
add_action( 'get_header', 'maintenance_message' );
function maintenance_message() {
if (current_user_can( 'edit_posts' )) return;
wp_die( '<h1>Stay Pawsitive!</h1><br>Sorry, we\'re temporarily down for maintenance right meow.' );
}
Ẩn menu với các use khác admin
// remove specific dashboard menus for non-admin users
add_action( 'admin_menu', 'hide_admin_menus' );
function hide_admin_menus() {
if (current_user_can( 'create_users' )) return;
if (wp_get_current_user()->display_name == "Salman") return;
remove_menu_page( 'plugins.php' );
remove_menu_page( 'themes.php' );
remove_menu_page( 'tools.php' );
remove_menu_page( 'users.php' );
remove_menu_page( 'edit.php?post_type=page' );
remove_menu_page( 'options-general.php' );
}
Thông báo khi đăng nhập thành công
// show a custom login message above the login form
function custom_login_message( $message ) {
if ( empty( $message ) ) {
return "<h2>Welcome to Let's Develop by Salman Ravoof! Please log in to start learning.</h2>";
}
else {
return $message;
}
}
add_filter( 'login_message', 'custom_login_message' );
Hook chức danh use vào danh mục menu ( sau tên use )
<?php
//flatsome theme display role after account user
function display_role_after_account_user() {
$user_id = get_current_user_id();
$user_meta=get_userdata($user_id);
$user_roles=$user_meta->roles[0];
echo '<p style="color:red;"><b>Role:</b> '.$user_roles.'</p>';
}
add_action('flatsome_after_account_user', 'display_role_after_account_user');
Ẩn từ gì đó với người chưa đăng nhập ở comment
// hook into the 'comment_text' filter with the callback function
add_filter( 'comment_text', 'the_profanity_filter' );
// define a callback function to filter profanities in comments
function the_profanity_filter( $comment_text ) {
// define an array of profane words and count how many are there
$profaneWords = array('0988', '0987', '0901', 'số điện thoại', '0919');
$profaneWordsCount = sizeof($profaneWords);
// loop through the profanities in $comment_text and replace them with '*'
for($i=0; $i < $profaneWordsCount; $i++) {
$comment_text = str_ireplace( $profaneWords[$i], str_repeat('*', strlen( $profaneWords[$i]) ), $comment_text );
}
return $comment_text;
}
Sau khi được liệt kê ở trên những từ có dạng như “màu đỏ , red ,0988 , 0987 .. ) sẽ được thay bằng dấu chấm
Hook vào top trang web
function block_header_top_flatsome(){
echo do_shortcode('[block id="banner-header-top"]');
}
add_action('flatsome_before_header','block_header_top_flatsome');
Tạm vậy đã, bạn hãy thử làm xem nhé!
Bình luận bị cấm: Bình luận có chứa yếu tố SPAM và yếu tố quảng cáo.