【问题标题】:WordPress add_filter is not working for robots_txt in localhostWordPress add_filter 不适用于 localhost 中的 robots_txt
【发布时间】:2019-06-29 02:37:03
【问题描述】:

我是 WordPress 插件开发的新手,我在这件事上遇到了障碍。问题是使用过滤器add_filter('robots_txt', 'AddToRobotsTxt', 10, 2); 不会在我的本地主机中创建 robots.txt 文件,我不知道是什么问题。我已将目录权限设置为

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

由于我认为这可能是导致问题的原因,因此我附上了我的其余代码。

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');
}

此功能用于html页面的设置。 它会调用这些函数。

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;
}

任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress apache phpmyadmin


    【解决方案1】:

    只需尝试将其添加到您的插件基础文件中,即可在初始化操作时将其移至类。如果没有,也请创建一个 robots.txt 文件。

    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;
    }
    

    【讨论】:

    • 如果 robots.txt 文件不存在,此过滤器是否会动态生成它?
    • 不,如果文件不存在,它不会生成文件。它是“do_robots”函数输出的过滤器
    • 实际上它不起作用我已经尝试在激活的主题中创建一个 robots.txt 文件然后对其进行测试。
    猜你喜欢
    • 2019-09-09
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多