A PHP robots function
Here is a simple yet very effective static method for a PHP class which can be used to define different indexing rules throughout pages. It can be integrated and called very easily from outside a PHP class without the need for initialisation of the class as it is a static method. There is no need for something like $functions = new functions(). You can just call it as shown below if you autoload your classes. If you have a functions class or even an seo class you use throughout your site, that would be a great place to put it although you can place it anywhere you like.
public static function robots_meta_tag($index = false, $follow = false)
$index_rule = ($index == true ? "index" : "noindex");
$follow_rule = ($follow == true ? "follow" : "nofollow");
return '<meta name="robots" content="'.$index_rule.','.$follow_rule.'">';
functions::robots_meta_tag(); // returns default <meta name="robots" content="noindex,nofollow">
functions::robots_meta_tag(true,true); // returns <meta name="robots" content="index,follow">
functions::robots_meta_tag(true,false); // returns <meta name="robots" content="index,nofollow">
functions::robots_meta_tag(false,true); // returns <meta name="robots" content="noindex,follow">
The code explaned
The method / function itself is fairly straightforward. By default both the index and follow variables are assigned a noindex and nofollow value. While this may seem a strange thing to do, it is better to default pages to a noindex,nofollow value to stop sensitive pages from being indexed just in case you ever forget to configure the index rules and you call the function on your pages. Both the index and follow rules except just two values; true and false. Setting a true value on the index or follow variables produce an index and follow rule respectively and setting a false value on the index and follow variables produces noindex and nofollow rules respectively. You can also combine the rules by setting let’s say true,false to have index,nofollow or false,true to have noindex,follow rules. The ternary operator produces the actual string from the true / false values that is used within the actual HTML robots Meta tag and this in turn is retuned at the end of the method / function. You can greatly expand and customise this method / function by including extra rules for other things that dictate if the page should be indexed or not like setting rules for different URL parameters.