Need to make a php file inside theme accessible via url

I checked this post here Making a plugin file accessible via url rewrite? which seems to have the same problem as me but its for a plugin. But so far i am getting 404.

add_action( 'init', 'my_rewrite' );
function my_rewrite() {
    global $wp_rewrite;

    add_rewrite_rule('/$', '/wp-content/themes/econ/adserver/adserver.js.php', 'top');
    $wp_rewrite->flush_rules(true);  
}

I have this file path /wp-content/themes/econ/adserver/adserver.js.php and need to show it as http://mysite.com/adserver.js.php

Read More

UPDATE:

The code that Bainternet gave is working good except the rewrite rule part. I can now access the file using index.php?myjs=true. Here is the code in my htaccess file you might wanna take a look.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Related posts

Leave a Reply

3 comments

  1. Ok here is a test working solution:

    <?php
    /*
    Plugin Name: wpse26719
    Plugin URI: http://en.bainternet.info
    Description: Need to make a php file inside theme accessible via url
    Version: 1.0
    Author: Bainternet
    Author URI: http://en.bainternet.info
    */
    
    // Register a URL that will set this variable to true
    add_action('generate_rewrite_rules', 'wpse26719_rw');
    function wpse26719_rw($wp_rewrite) {
       $newrules = array();
       $new_rules['^adserver.js.php$'] = 'index.php?myjs=true';
       $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }
    
    
    // Add myjs as a query var
    add_action( 'query_vars', 'wpse26719_query_vars' );
    function wpse26719_query_vars( $query_vars )
    {
        $query_vars[] = 'myjs';
        return $query_vars;
    }
    
    // If this is done, we can access it later
    // This example checks very early in the process:
    // if the variable is set, we include our page and stop execution after it
    add_action( 'parse_request', 'wpse26719_parse_request' );
    function wpse26719_parse_request( &$wp )
    {
        if ( array_key_exists( 'myjs', $wp->query_vars ) ) {
            include( dirname( __FILE__ ) . '/adserver.js.php' );
            exit();
        }
    }
    
  2. WP rewrite rules are not real rewrites nor will they allow redirects like that. So you can’t use them that way. Fortunately, WP should have you covered there even if you try.

    See, an internal WordPress rewrite rule must change a URL pattern into an index.php?key=value pattern.

    So when you use add_rewrite_rule to insert a rule that doesn’t point to the index.php file, then this should get detected and a “hard” rule will be created in the .htaccess file for it. Thus, check the .htaccess file after doing the hard flush and see if your new rule exists there as a normal RewriteRule. If not, then maybe the .htaccess isn’t writable.

  3. Try this:

    add_action( 'init', 'my_rewrite' );
    function my_rewrite() {
        global $wp_rewrite;
    
        add_rewrite_rule('^adserver.js.php$', '/wp-content/themes/econ/adserver/adserver.js.php', 'top');
        $wp_rewrite->flush_rules(true);  
    }
    

    And you should probably remove $wp_rewrite->flush_rules(true); once you have it working, since that’s a big performance hit on every page view.