duanbi6522 2019-02-05 09:52
浏览 63

WordPress add_filter不适用于localhost中的robots_txt

I am a newbie in WordPress plugin development and i have kind of hit a roadblock in this matter. The thing is that using the filter add_filter('robots_txt', 'AddToRobotsTxt', 10, 2); does not create a robots.txt file in my localhost i do not know what is the issue. I have set directory permissions to

<Directory />
    AllowOverride All
    Require all granted
</Directory>

Since I thought this might be causing the issue, I'm attaching the rest of my code.

public function allRobotSettings(){


    register_setting('energizer_robot_group', 'energizer_robots-name');

    add_settings_section('energizer_robot_index', 'Robot Setting', array( $this->callbacks_mngr, 'robotSectionManager' )
    , 'energizer_robots');

    add_settings_field('robot_field_manager', 'Robot Document', array( $this->callbacks_mngr, 'robotInputboxField' ),
    'energizer_robots', 'energizer_robot_index');
}

This function is for settings on html page. And it calls these functions.

public function robotSectionManager()
{
    echo 'Edit your robot.txt file here.';
}

public function robotInputboxField()
{
    $data=get_option('energizer_robots-name');  

    add_filter( 'robots_txt', 'AddToRobotsTxt',10,2);
    $content=get_option('energizer_robots-name');   

    echo '<div ><input type="text"  name="energizer_robots-name" value="'. $content.'" 
    style="height: 150px;
    width: 100%;
    box-sizing: border-box;
    border: 2px solid #ccc;
    border-radius: 4px;
    background-color: #f8f8f8; ></div>';
}
public function AddToRobotsTxt($robotstext, $public) {
    $robotsrules = get_option('energizer_robots-name');
    $new_value=$robotstext . $robotsrules;
    update_option( 'energizer_robots-name', $new_value);

    return $robotstext . $robotsrules;
}

Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • douganmo1121 2019-02-05 09:58
    关注

    Just try adding this in your plugin base file, it can be moved to class on init action. Also please create one robots.txt file if there is not.

    add_filter( 'robots_txt', 'AddToRobotsTxt',10,2);
    
    function AddToRobotsTxt($robotstext, $public) {
    
        $robotsrules = get_option('energizer_robots-name');
        $new_value=$robotstext . $robotsrules;
        update_option( 'energizer_robots-name', $new_value);
    
        return $robotstext . $robotsrules;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?