WordPress的add_filter不适用于localhost中的robots_txt

时间:2019-02-05 09:52:25

标签: php wordpress apache phpmyadmin

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

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

只需尝试将其添加到您的插件基础文件中,它就可以在初始化操作时移至类中。另外,如果没有,请创建一个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;
}