Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End wordpress – link to file at root level

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #28283
    fishnfrogs
    Member

    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!

    #82227
    TheDoc
    Member

    Can you not just put the file in the root?

    #82228
    fishnfrogs
    Member

    Hi, 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. :)

    #82230
    TheDoc
    Member

    Fair 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.

    #82231
    TheDoc
    Member

    Or some other more elegant solution that I have not thought of.

    #82233
    ddliu
    Member

    Well 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

    #82311
    fishnfrogs
    Member

    Thanks 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!!

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Back End’ is closed to new topics and replies.