Blog Wordpress, Thủ thuật Ngày 18/07/2022 12318 lượt xem

Phần 3 : Các hook trong wordpress – Hook trong blog post

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ê Anh ĐôngFlatsome trên Google News

Các hook trong wordpress - Phần 3 - Hook trong blog post

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é

Các hook trong wordpress – Phần 3 – Hook trong blog post

Các hook trong wordpress - Phần 3 - Hook trong blog post
Các hook trong wordpress – Phần 3 – Hook trong blog post

Cú pháp để chèn 1 hook vào WordPress và vị trí hook ra tớ đã nói ở bài 1 và bài 2 nhé

Cú pháp mặc định là dạng

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');

OK xem qua vị trí hook nhé

CÁC VỊ TRÍ HOOK TRONG BLOG

1 .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 đề - flatsome.xyz
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);

 

2 . 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

1. 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' );

2 . 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');

Các hook trong wordpress - Phần 3 - Hook trong blog post

Các hook trong wordpress – Phần 3 – Hook trong blog post

3 . 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;    
}
Ví dụ về thêm text ở giữa content theo từng đoạn
Ví dụ về thêm text ở giữa content theo từng đoạn

 

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 ở commem

// 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

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
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

thêm vào function nhé

function block_header_top_flatsome(){
    echo do_shortcode('

Block "banner-header-top" not found

'); } add_action('flatsome_before_header','block_header_top_flatsome');
Hook vào top trang web
Hook vào top trang web

 

 

OK , tạm thời đến đây nhé – Bài tiếp tớ tìm những hook anh em cần cho Custom và share cùng anh em

Nếu cảm thấy bài hữu ích Nhớ Like và Share dùm nhé

Cảm ơn bạn nhiều

Điểm 5/5 - ( Có 11 bình chọn)

Để lại một bình luận

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 *

Các dịch vụ của FLATSOME.XYZ