- This topic is empty.
-
AuthorPosts
-
June 23, 2011 at 4:37 pm #28283fishnfrogsMember
Hi everyone! I hope that everyone is well today. I’m still learning how to hack my way around WordPress and have a question. I would like to add a file that’s located in my theme folder, but make it look like it’s at the root of my site. For example, I have a php file called howdy.php. It’s located at http://www.example.com/wp-content/themes/my-theme/howdy.php but I want to link to it as http://www.example.com/howdy.txt. Can anyone offer any support as to how to do this? I’ve looked on google, but haven’t searched any plugins. I don’t really want to use a plugin because, well, teach a man to fish and what not. Thank you in advanced!
June 23, 2011 at 4:48 pm #82227TheDocMemberCan you not just put the file in the root?
June 23, 2011 at 4:53 pm #82228fishnfrogsMemberHi, I could, but I’d rather learn how to do it on a theme level. Not for any real reason other than I was looking at the WordPress SEO plugin and wondered how he did it for the sitemap. I have all my webmasters files at the root. I was just curious how to do it. I learned how to do the ‘do_feed’ action yesterday and was trying to expand my learning. :)
June 23, 2011 at 6:41 pm #82230TheDocMemberFair enough.
I suppose you could use some .htaccess to make it appear as example.com/howdy but I’m not too skilled in that department.
Or you could create a folder in your root called ‘howdy’ and place an index.html file in there which redirects to your howdy.php file.
June 23, 2011 at 6:41 pm #82231TheDocMemberOr some other more elegant solution that I have not thought of.
June 23, 2011 at 8:19 pm #82233ddliuMemberWell I suppose you will have a fixed filename such as “/sitemap.xml” that you want to link to the “/wp-content/themes/my-theme/howdy.php” script.
I don’t know whether there is a way to do a redirect in a .xml file or a .txt file, and I think redirect won’t work in such condition.
So .htaccess rewrite might be the best solution as mentioned by @TheDoc, rewrite rule as following:
RewriteEngine On
RewriteRule ^sitemap.xml$ wp-content/themes/my-theme/howdy.php
You can learn more about Apache mod_rewrite here: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
June 24, 2011 at 2:12 pm #82311fishnfrogsMemberThanks everyone. I was trying to keep it at a theme level for reasons I don’t quite understand. Anyway, I installed the Humans TXT plugin and reversed it to do what I wanted. Turns out I was looking for ‘template_redirect’ and some added code:
add_action('init', 'addHowdy');
add_action('template_redirect', 'howdy_template_redirect', 8);
function addHowdy() {
global $wp_rewrite;
$rewrite_rules = get_option('rewrite_rules');
add_filter('query_vars', create_function('$qv', '$qv[] = "howdy"; return $qv;'));
add_rewrite_rule('howdy.txt$', $wp_rewrite->index.'?howdy=1', 'top');
flush_rewrite_rules(false);
}
function is_howdy() {
return (bool)get_query_var('howdy');
}
function howdy_template_redirect() {
if(is_howdy())
{
load_template( TEMPLATEPATH . '/utils/howdy.php');
exit();
}
}
It seems to work and I can’t say it’s the most efficient, but it works for what I need. Thanks for everyone’s replies!!
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.