add_filter doesn't work

时间:2012-12-21 作者:ahmadali shafiee

我正在开发一个插件来替换标题、帖子和评论中一些用户代理的角色,但过滤器不起作用。以下是插件:

<?php
/*
Plugin Name: WP-HalfSpace
Description: نمایش فاصله به‌جای نیم‌فاصله برای گوگل برای خراب‌نشدن نوشته در نتایج جست‌وجوی گوگل
Version: 1.0.0
Author: ahmadalli
Author URI: http://2barnamenevis.com
*/
function HalfSpace_filter($content)
{

    $u_agent = $_SERVER[\'HTTP_USER_AGENT\'];
    echo "hi";
    if(($u_agent=="Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" )
    || ($u_agent=="Googlebot/2.1 (+http://www.googlebot.com/bot.html)")
    || ($u_agent=="Googlebot/2.1 (+http://www.google.com/bot.html)")
    || ($u_agent=="Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)")
    )
    { 
        echo "aaaaaaaaa";
        $content=str_ireplace("‌‌"," ",$content); //"" is not empty. it\'s a persian character names halfspace.
    }
    return $content;
}

register_activation_hook( __FILE__, \'HalfSpacePlugin_activate\' );

function HalfSpacePlugin_activate()
{
    add_filter(\'the_title\',\'HalfSpace_filter\',1,1);
    add_filter(\'the_content\',\'HalfSpace_filter\',1,1);
    add_filter(\'comment_text\',\'HalfSpace_filter\',1,1);
}



register_deactivation_hook( __FILE__, \'HalfSpacePlugin_deactivate\' );

function HalfSpacePlugin_deactivate()
{
    remove_filter(\'the_title\',\'HalfSpace_filter\');
    remove_filter(\'the_content\',\'HalfSpace_filter\');
    remove_filter(\'comment_text\',\'HalfSpace_filter\');
}
?>
过滤器根本不工作,因为echo "hi"; 在里面HalfSpace_filter 做不到这是工作!

有人能帮我吗?

2 个回复
最合适的回答,由SO网友:Matthew Boynes 整理而成

register_activation_hookregister_deactivation_hook 分别指插件何时被激活和停用,并且仅在这两个实例中触发。例如,当您需要添加数据库表(并将其恢复)时,可以使用它们。您也不需要删除过滤器。您的插件可以缩短为:

function HalfSpace_filter( $content ) {
    $u_agent = $_SERVER[\'HTTP_USER_AGENT\'];
    echo "hi";
    if ( ( $u_agent=="Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" )
    || ( $u_agent=="Googlebot/2.1 (+http://www.googlebot.com/bot.html)" )
    || ( $u_agent=="Googlebot/2.1 (+http://www.google.com/bot.html)" )
    || ( $u_agent=="Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)" )
    ) { 
        echo "aaaaaaaaa";
        $content = str_ireplace( "‌‌", " ",$content ); //"" is not empty. it\'s a persian character names halfspace.
    }
    return $content;
}
add_filter( \'the_title\',\'HalfSpace_filter\' );
add_filter( \'the_content\',\'HalfSpace_filter\' );
add_filter( \'comment_text\',\'HalfSpace_filter\' );

SO网友:fuxia

激活和停用处理程序仅在插件激活和停用期间调用。因此得名。只要去掉这个条件,它就会起作用。

结束

相关推荐

Travel Blog Plugins

今年晚些时候,我将使用Wordpress创建一个关于我旅行的博客。我希望该博客具有以下功能我的帖子将被地理定位一张包含帖子位置的地图,可以单击地图上的各个点到达帖子</我正在寻找最好/最合适的插件。谢谢,艾尔。