《wordpress 50个过滤钩子》 11-20

11.gettext: 过滤wordpress的翻译数据。

在wordpress中,使用__(), _e(), _x(), _ex(), 的内容可以在翻译文件中生成,病根据不同的location加载不同的po文件从而翻译。使用gettext钩子,可以过滤翻译的内容。

 <?php

 add_filter( 'gettext', 'gettext_example', 20, 3 );

 function gettext_example( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'E-meil Adress' :
$translated_text = __( 'Email Address', 'plugin_text_domain' );
break;
}
return $translated_text
} // Example source: http://speakinginbytes.com/2013/10/gettext-filter-wordpress/ ?>

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添加到数组中,返回数组即可:

 <?php

 add_filter( 'no_texturize_shortcodes', 'no_texturize_shortcodes_example' );

 function no_texturize_shortcodes_example( $shortcodes ) {
$shortcodes[] = 'myshortcode';
return $shortcodes;
} // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/no_texturize_shortcodes ?>

14.pre_comment_approve: 过滤评论审核

如果需要在代码中添加对评论的审核逻辑,可以使用该钩子获取评论内容病添加逻辑,修改审核结果:

 <?php

 add_filter( 'pre_comment_approved', 'pre_comment_approved_example', 99, 2 );

 function pre_comment_approved_example( $approved, $commentdata ) {
return ( strlen( $commentdata['comment_author'] ) > 75 ) ? 'spam' : $approved;
} // Example source: https://gist.github.com/norcross/5468979 ?>

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.

 <?php

 add_filter( 'wp_title', 'wp_title_example', 10, 2 );

 function wp_title_example( $title, $sep ) {
global $paged, $page; if ( is_feed() )
return $title; // Add the site name.
$title .= get_bloginfo( 'name' ); // Add the site description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) )
$title = "$title $sep $site_description"; // Add a page number if necessary.
if ( $paged >= 2 || $page >= 2 )
$title = sprintf( __( 'Page %s', 'tuts_filter_example' ), max( $paged, $page ) ) . " $sep $title"; return $title;
} // Example source: http://tommcfarlin.com/filter-wp-title/ ?>

17. preprocess_comment:在评论存入DB之前修改和加工它们

 <?php

 add_filter( 'preprocess_comment', 'preprocess_comment_example' );

 function preprocess_comment_example( $commentdata ) {
if( $commentdata['comment_content'] == strtoupper( $commentdata['comment_content'] ))
$commentdata['comment_content'] = strtolower( $commentdata['comment_content'] );
return $commentdata;
} // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/preprocess_comment ?>

18.login_redirect: 管理界面登录后重定向。

注意,该filter 一定要在is_admin()之外使用add_filter(),因为在is_admin()被调用的时候这个钩子还不可用。

 <?php

 add_filter( 'login_redirect', 'login_redirect_example', 10, 3 );

 function login_redirect_example( $redirect_to, $request, $user ) {
global $user;
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if ( in_array( 'subscriber', $user->roles ) ) {
return home_url();
} else {
return $redirect_to;
}
}
return;
} ?>

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中的内容

 <?php

 add_filter( 'the_editor_content', 'the_editor_content_example' );

 function the_editor_content_example( $content ) {
// Only return the filtered content if it's empty
if ( empty( $content ) ) {
$template = 'Hey! Don\'t forget to...' . "\n\n";
$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";
$template .= 'Bye!';
return $template;
} else
return $content;
} // Example source: http://wpfilte.rs/the_editor_content/ ?>

原文链接:http://code.tutsplus.com/tutorials/50-filters-of-wordpress-filters-11-20--cms-21296

PHP学习笔记(七)的更多相关文章

  1. (转)Qt Model/View 学习笔记 (七)——Delegate类

    Qt Model/View 学习笔记 (七) Delegate  类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...

  2. Learning ROS for Robotics Programming Second Edition学习笔记(七) indigo PCL xtion pro live

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...

  3. Typescript 学习笔记七:泛型

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  4. python3.4学习笔记(七) 学习网站博客推荐

    python3.4学习笔记(七) 学习网站博客推荐 深入 Python 3http://sebug.net/paper/books/dive-into-python3/<深入 Python 3& ...

  5. Go语言学习笔记七: 函数

    Go语言学习笔记七: 函数 Go语言有函数还有方法,神奇不.这有点像python了. 函数定义 func function_name( [parameter list] ) [return_types ...

  6. iOS 学习笔记七 【博爱手把手教你使用2016年gitHub Mac客户端】

    iOS 学习笔记七 [博爱手把手教你使用gitHub客户端] 第一步:首先下载git客户端 链接:https://desktop.github.com 第二步:fork 大神的代码[这里以我的代码为例 ...

  7. 【opencv学习笔记七】访问图像中的像素与图像亮度对比度调整

    今天我们来看一下如何访问图像的像素,以及如何改变图像的亮度与对比度. 在之前我们先来看一下图像矩阵数据的排列方式.我们以一个简单的矩阵来说明: 对单通道图像排列如下: 对于双通道图像排列如下: 那么对 ...

  8. Linux学习笔记(七) 查询系统

    1.查看命令 (1)man 可以使用 man 命令名称 命令查看某个命令的详细用法,其显示的内容如下: NAME:命令名称 SYNOPSIS:语法 DESCRIPTION:说明 OPTIONS:选项 ...

  9. go微服务框架kratos学习笔记七(kratos warden 负载均衡 balancer)

    目录 go微服务框架kratos学习笔记七(kratos warden 负载均衡 balancer) demo demo server demo client 池 dao service p2c ro ...

  10. Java IO学习笔记七:多路复用从单线程到多线程

    作者:Grey 原文地址:Java IO学习笔记七:多路复用从单线程到多线程 在前面提到的多路复用的服务端代码中, 我们在处理读数据的同时,也处理了写事件: public void readHandl ...

随机推荐

  1. jsf服务器端验证用户输入

    服务器端验证用户输入数据步骤 1 html页面上插入要输入数据控件 <h:inputText size="10" value="#{commodityBean.fo ...

  2. shell脚本应用(3)--语法结构

    判断语句 条件判断 test expression [ expression ] 条件表达式中常用的判断 数值-eq -ne -gt -lt -ge -le[equal not greater tha ...

  3. UVa11404 - Palindromic Subsequence(区间DP+打印路径)

    题目大意 给定一个字符串,要求你删除尽量少的字符,使得原字符串变为最长回文串,并把回文串输出,如果答案有多种,则输出字典序最小的 题解 有两种解法,第一种是把字符串逆序,然后求两个字符串的LCS,并记 ...

  4. Yii Active Record 查询结果转化成数组

    使用Yii 的Active Record 来获取查询结果的时候,返回的结果集是一个对象类型的,有时候为了数据处理的方便希望能够转成数组返回.比如下面的方法: // 查找满足指定条件的结果中的第一行 $ ...

  5. zoj 3460 Missile【经典建图&&二分】

    Missile Time Limit: 2 Seconds      Memory Limit: 65536 KB You control N missile launching towers. Ev ...

  6. 如何使用iframe实现隐藏的CSRF

    1.攻击者在“页面1”中http://www.b.com/indexb.html中写下如下代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1. ...

  7. Xsocket学习

    1.xsocket是一个轻量级的基于NIO的服务器框架,用于开发高性能.可扩展.多线程的服务器.该框架封装了线程处理,异步读写等方面的操作. 定义一个借口,继承IDataHandler,IConnec ...

  8. 关于python文件转为exe文件

    一.简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具,这样,你就可以不用装python而在windows系统上运行这个可执行程序. py2e ...

  9. Android实时监听网络状态(1)

    其实手机在网络方面的的监听也比较重要,有时候我们必须实时监控这个程序的实时网络状态,android在网络断开与连接的时候都会发出广播,我们通过接收系统的广播就可以实现网络的监听. 1.添加访问网络和获 ...

  10. Android 定时任务高度【schedule】与【scheduleAtFixedRate】差别

    在android中调度定时任务有两种方法 1.schedule 2.scheduleAtFixedRate 这两种方法的差别在于 首次调用时间(Date when)这个參数 <span styl ...