0

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.

1

You are underestimating the complexity of robots file handling in WordPress. The presence of physical file is one of two cases.

If physical file is not present during template load is_robots() checks is performed and in positive case do_robots() runs, generating and serving the "file" on the fly.

Just going ahead and creating robots.txt in arbitrary site might very well wreck havoc with related code that is making use of virtual file and hooks in it.

Answering direct question — I am not sure which goals you are pursuing by this functionality so it is a little hard to tell what would work best for timing. The simples might be to run it on visiting theme's options page (in same way as visiting native permalink settings performs permalink flush).

1
  • Wow, WordPress is better than I expected! Thank you Rarst! – Gerard Jul 20 '14 at 7:34
0

Question has too much detail. It doesn't matter what you want to do, if you want to run code only "once" the best time is theme's activation. probably something like

if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) { get_robots(); }

but: This is a great example why themes should not touch functionality beyond fronend design as it is easy to see that you don't understand how robots.txt is generated by wordpress and you are likely to disrupt the functionality of dedicated plugins. Actually your code is unlikely to work at all on any site with minimal security settings.

3
  • If you use after_switch_theme in a theme, it only works if the theme that adds action is the one being enabled. It is the best action hook to run code on theme activation. Use switch_theme in a theme as action hook for theme deactivation. – cybmeta Jul 18 '14 at 18:47
  • true, after_switch_theme is a cleaner hook to use. always learn something new :) – Mark Kaplun Jul 19 '14 at 3:10
  • Oh I see. Then, I won't use this function on my theme. And according to the reasons you told me, I deduce it's the same for the .htaccess file. – Gerard Jul 20 '14 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.