《wordpress 50个过滤钩子》 11-20
11.gettext: 过滤wordpress的翻译数据。
在wordpress中,使用__(), _e(), _x(), _ex(), 的内容可以在翻译文件中生成,病根据不同的location加载不同的po文件从而翻译。使用gettext钩子,可以过滤翻译的内容。
1 <?php 2 3 add_filter( ‘gettext‘, ‘gettext_example‘, 20, 3 ); 4 5 function gettext_example( $translated_text, $text, $domain ) { 6 switch ( $translated_text ) { 7 case ‘E-meil Adress‘ : 8 $translated_text = __( ‘Email Address‘, ‘plugin_text_domain‘ ); 9 break; 10 } 11 return $translated_text 12 } 13 14 // Example source: http://speakinginbytes.com/2013/10/gettext-filter-wordpress/ 15 16 ?>
ps: __() 和_e()都是返回对应当前语言的字符串内容,区别是__()没有输出而_e()有输出,即:_e(‘hello‘) = echo __(‘hello‘), 带e的都表示echo ...
_x()表示翻译的根据上下文,_n()表示进行单复数编译(单数的字符串,复数的字符串,引用的数字)。
12.sanitize_title: 修改slug
<?php add_filter( ‘sanitize_title‘, ‘sanitize_title_example‘ ); function sanitize_title_example( $title ) { $title = str_replace( ‘-the-‘, ‘-‘, $title ); $title = preg_replace( ‘/^the-/‘, ‘‘, $title ); return $title; } ?>
13.no_texturize_shortcode: 指定shortcode不进行自动转义。
wptexturize是wordpress中的自动转义功能,如果希望指定的shortcode代码不自动进行转义,使用该钩子将指定的shortcode添加到数组中,返回数组即可:
1 <?php 2 3 add_filter( ‘no_texturize_shortcodes‘, ‘no_texturize_shortcodes_example‘ ); 4 5 function no_texturize_shortcodes_example( $shortcodes ) { 6 $shortcodes[] = ‘myshortcode‘; 7 return $shortcodes; 8 } 9 10 // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/no_texturize_shortcodes 11 12 ?>
14.pre_comment_approve: 过滤评论审核
如果需要在代码中添加对评论的审核逻辑,可以使用该钩子获取评论内容病添加逻辑,修改审核结果:
1 <?php 2 3 add_filter( ‘pre_comment_approved‘, ‘pre_comment_approved_example‘, 99, 2 ); 4 5 function pre_comment_approved_example( $approved, $commentdata ) { 6 return ( strlen( $commentdata[‘comment_author‘] ) > 75 ) ? ‘spam‘ : $approved; 7 } 8 9 // Example source: https://gist.github.com/norcross/5468979 10 11 ?>
15.enable_post_by_email_configuration: 打开或关闭Post_By_Email功能:
关闭:
<?php add_filter( ‘enable_post_by_email_configuration‘, ‘__return_false‘, 100 ); ?>
打开:
<?php add_filter( ‘enable_post_by_email_configuration‘, ‘__return_true‘, 100 ); ?>
16.wp_title: 重写你的page title.
1 <?php 2 3 add_filter( ‘wp_title‘, ‘wp_title_example‘, 10, 2 ); 4 5 function wp_title_example( $title, $sep ) { 6 global $paged, $page; 7 8 if ( is_feed() ) 9 return $title; 10 11 // Add the site name. 12 $title .= get_bloginfo( ‘name‘ ); 13 14 // Add the site description for the home/front page. 15 $site_description = get_bloginfo( ‘description‘, ‘display‘ ); 16 if ( $site_description && ( is_home() || is_front_page() ) ) 17 $title = "$title $sep $site_description"; 18 19 // Add a page number if necessary. 20 if ( $paged >= 2 || $page >= 2 ) 21 $title = sprintf( __( ‘Page %s‘, ‘tuts_filter_example‘ ), max( $paged, $page ) ) . " $sep $title"; 22 23 return $title; 24 } 25 26 // Example source: http://tommcfarlin.com/filter-wp-title/ 27 28 ?>
17. preprocess_comment:在评论存入DB之前修改和加工它们
1 <?php 2 3 add_filter( ‘preprocess_comment‘, ‘preprocess_comment_example‘ ); 4 5 function preprocess_comment_example( $commentdata ) { 6 if( $commentdata[‘comment_content‘] == strtoupper( $commentdata[‘comment_content‘] )) 7 $commentdata[‘comment_content‘] = strtolower( $commentdata[‘comment_content‘] ); 8 return $commentdata; 9 } 10 11 // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/preprocess_comment 12 13 ?>
18.login_redirect: 管理界面登录后重定向。
注意,该filter 一定要在is_admin()之外使用add_filter(),因为在is_admin()被调用的时候这个钩子还不可用。
1 <?php 2 3 add_filter( ‘login_redirect‘, ‘login_redirect_example‘, 10, 3 ); 4 5 function login_redirect_example( $redirect_to, $request, $user ) { 6 global $user; 7 if ( isset( $user->roles ) && is_array( $user->roles ) ) { 8 if ( in_array( ‘subscriber‘, $user->roles ) ) { 9 return home_url(); 10 } else { 11 return $redirect_to; 12 } 13 } 14 return; 15 } 16 17 ?>
19.plugin_aciton_links_插件文件名:给你的插件加上链接
<?php add_filter( ‘plugin_action_links_‘ . plugin_basename( __FILE__ ), ‘plugin_action_links_example‘ ); function plugin_action_links_example( $links ) { $links[] = ‘<a href="‘ . get_admin_url( null, ‘options-general.php?page=my_plugin_settings‘ ) . ‘">‘ . __( ‘Settings‘ ) . ‘</a>‘; return $links; } // Example source: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name) ?>
20.the_editor_content: 过滤post editor中的内容
1 <?php 2 3 add_filter( ‘the_editor_content‘, ‘the_editor_content_example‘ ); 4 5 function the_editor_content_example( $content ) { 6 // Only return the filtered content if it‘s empty 7 if ( empty( $content ) ) { 8 $template = ‘Hey! Don\‘t forget to...‘ . "\n\n"; 9 $template .= ‘<ul><li>Come up with good tags for the post,</li><li>Set the publish time to 08:00 tomorrow morning,</li><li>Change the slug to a SEO-friendly slug,</li><li>And delete this text, hehe.</li></ul>‘ . "\n\n"; 10 $template .= ‘Bye!‘; 11 return $template; 12 } else 13 return $content; 14 } 15 16 // Example source: http://wpfilte.rs/the_editor_content/ 17 18 ?>
原文链接:http://code.tutsplus.com/tutorials/50-filters-of-wordpress-filters-11-20--cms-21296
原文:http://www.cnblogs.com/JacobQiao/p/5324437.html