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