I'm using the Option-Tree framework for the options of my theme. Now I want to add the option to create and edit the robots.txt file. First, I have the function to get the content of the file or create it with a default content; is this:
function get_robots($path)
{
$robots_file = $path . DIRECTORY_SEPARATOR . 'robots.txt'; //The robots file.
if(file_exists($robots_file)){
return file_get_contents($robots_file);
} else {
$default_content = "User-agent: *\nDisallow:";
file_put_contents($robots_file, $default_content);
return $default_content;
}
}
get_robots(getcwd());
If I use this function on functions.php
, it will run on every page load, and that's a tiny waste. I'm planning to use this function on robots.php
and then I will call it when I need it. But, when it's the best moment, where is the best place of the code to run it once?
Every time when I load the options page, for example? Edit: No, it's not working, because the options settings are included on functions.php
so it creates the file on every page view.