日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHP学习笔记(六)

發布時間:2023/12/1 php 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP学习笔记(六) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

《Wordpress 50個過濾鉤子》 1-10

過濾鉤子是一類函數,wordpress執行傳遞和處理數據的過程中,在針對這些數據做出某些動作之前的特定點執行。本質上,就是在wordpress輸出之前,將對瀏覽數據做出反應。

添加過濾鉤子: add_filter($tag, $function_to_add, $piority, $accepted_tags);

參數解釋: $tag(必須):過濾鉤子的名稱

      $funciton_to_add(必須): 要掛載的函數名。

      $piority: 整數,判斷函數什么時候執行,默認是10,數值越低,優先級越高。

      $accepted_tages: 整數,默認是1,設置接收參數的個數。

移除過濾鉤子:remove_filter($tag, $function_to_remove, $piority)?

應用鉤子: apply_filter($tag, $value, $var1, $var2,....)

參數解釋: $tag(必須):鉤子的名稱

      $value(必須): 通過add_filter()掛載的過濾函數要修改的值

?


1. log_errors: 改變默認登錄錯誤信息

默認的錯誤信息顯得比較啰嗦,如果需要簡單的錯誤信息,可以使用該過濾鉤子對錯誤信息進行修改然后返回。

<?phpadd_filter( 'login_errors', 'login_errors_example' );function login_errors_example( $error ) {$error = 'this is the modified error';return $error; }?>

這樣,當登錄失敗的時候,就會顯示 this is the modified error?

2. comment_post_redirect: 更改提交評論后的顯示頁面

當用戶提交完評論后,默認是留在同一頁面的,當你有需求在評論后跳轉到另外一個頁面時,可以用這個鉤子進行頁面指定。

<?phpadd_filter( 'comment_post_redirect', 'comment_post_redirect_example' );function comment_post_redirect_example( $location ) {return '/thanks-for-your-comment/'; }?>

$location是默認的頁面地址。

3. allowed_redirect_hosts:增加wp_safe_redirect()允許訪問的地址。

默認情況下,wp_safe_redirect()函數僅僅允許站內訪問,如果想要實現其他的地址訪問,可以用這個鉤子來添加地址。

1 <?php 2 3 add_filter( 'allowed_redirect_hosts', 'allowed_redirect_hosts_example' ); 4 5 function allowed_redirect_hosts_example( $content ) { 6 $content[] = 'forum.mywebsite.com'; 7 $content[] = 'welcome.mywebsite.com'; 8 $content[] = 'help.mywebsite.com'; 9 return $content; 10 } 11 12 // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/allowed_redirect_hosts 13 14 ?>

$content是數組,存儲著允許訪問站點的地址。

4.body_class: 給<body>標簽加css類。

如果需要給特定的頁面指定css的時候,可以通過該鉤子給body標簽加上css類。

1 <?php 2 3 add_filter( 'body_class', 'body_class_example' ); 4 5 function body_class_example( $classes ) { 6 if( is_single() ) { 7 foreach( get_the_category( get_the_ID() ) as $category ) 8 $classes[] = 'cat-' . $category->category_nicename; 9 } 10 return $classes; 11 } 12 13 // Example source: https://codex.wordpress.org/Function_Reference/body_class#Add_Classes_By_Filters 14 15 ?>

5.locale:改變地區(針對翻譯功能).

通過該鉤子,可以改變地區從而讓系統改變讀取的翻譯文件。

1 <?php 2 3 add_filter( 'locale', 'locale_example' ); 4 5 function locale_example( $lang ) { 6 if ( 'tr' == $_GET['language'] ) { 7 return 'tr_TR'; 8 } else { 9 return $lang; 10 } 11 } 12 13 // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/locale 14 15 ?>

6.sanitize_user:過濾username

通過該鉤子,可以對用戶登錄時輸入的username進行操作,如轉換為小寫,字符檢查等。

1 <?php 2 3 add_filter( 'sanitize_user', 'strtolower' ); 4 5 // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/sanitize_user 6 7 ?>

7.the_content:過濾post的內容

對于post的內容,如果需要進行操作,如字符串替換,給文章插入標記等等。可以使用該過濾鉤子

<?phpadd_filter( 'the_content', 'the_content_example' );function the_content_example( $content ) {return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content); }// Example source: http://wpsnipp.com/index.php/functions-php/remove-p-tag-from-around-images-in-the_content/?>

8.the_password_form:過濾password form

對于帶有密碼保護的post, wordpress會自定將其替換為password form, 使用該鉤子你可以訪問和自定義這個form.

1 <?php 2 3 add_filter( 'the_password_form', 'the_password_form_example' ); 4 5 function the_password_form_example() { 6 $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">'; 7 $output .= '<span>' . __( "Enter the password:" ) . ' </span>'; 8 $output .= '<input name="post_password" type="password" size="20" />'; 9 $output .= '<input type="submit" name="Submit" value="' . esc_attr__( "Go" ) . '" />'; 10 $output .= '</form>'; 11 return $output; 12 } 13 14 // Example source: http://codex.wordpress.org/Using_Password_Protection#Password_Form_Text 15 16 ?>

9.the_terms: 過濾the_trems() 函數。

使用該鉤子可以過濾函數the_terms()的輸出,例如去除其中的標簽:

1 <?php 2 3 add_filter( 'the_terms', 'strip_tags' ); 4 5 ?>

10. wp_mail_from: 改變郵箱發件人的地址。

如果你想使用wordpress 發送郵件功能時改變發件人的地址,用該鉤子就可以實現。

<?phpadd_filter( 'wp_mail_from', 'wp_mail_from_example' );function wp_mail_from_example( $email ) {return 'my.email.address@mywebsite.com'; }?>

?

英文原文:http://code.tutsplus.com/tutorials/50-filters-of-wordpress-the-first-10-filters--cms-21295

?

轉載于:https://www.cnblogs.com/JacobQiao/p/5233225.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的PHP学习笔记(六)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。