• Resolved mcnicks

    (@mcnicks)


    Hi,

    I realise this kind of question has been asked several times, but I have read all of the posts I can find here and on the interweb, and I am still stumped. What I want to do is write a plugin and map URLs onto PHP scripts in the plugin’s directory. Say the plugin is called ‘foo’, I would want to set up rules to map:

    /foo/ => /wp-content/plugins/foo/main.php
    /foo/bar/ => /wp-content/plugins/foo/bar.php
    /foo/baz/ => /wp-content/plugins/foo/baz.php

    And so on. With WP1.5 I would have written some RewriteRules for .htaccess, something along the lines of:

    RewriteRule ^foo/bar/(.*)$ /wp-content/plugins/foo/bar.php?uri=$1 [QSA,L]
    RewriteRule ^foo/baz/(.*)$ /wp-content/plugins/foo/baz.php?uri=$1 [QSA,L]
    RewriteRule ^foo/$ /wp-content/plugins/foo/main.php [QSA,L]

    (NB: I just made these up so they may be wrong, but you get the idea …)

    It looks like I should be able to do this programmatically in WP2.0 but I can never get anything to work, no matter what filters / hooks I try to use. I have tried copying the method that Ultimate Tag Warrior uses but I cannot even get UTW to redirect properly (version 2.9.1 on a fresh install of WP2.0 – but that is another issue).

    What should I be doing? The fact that UTW does not work makes me think that there might be a deeper problem with redirects in WP2.0.

    Cheers,
    David

Viewing 4 replies - 1 through 4 (of 4 total)
  • Have you tried something like this?

    function foo_bar_rewrite_rules($rules) {
    $newrules['foo/bar/(.*)$'] = '/wp-content/plugins/foo/bar.php?uri=$1';
    $newrules['foo/baz/(.*)$'] = '/wp-content/plugins/foo/baz.php?uri=$1';
    $newrules['foo/$'] = '/wp-content/plugins/foo/main.php';

    return array_merge($newrules,$rules);
    }

    add_filter('rewrite_rules_array','foo_bar_rewrite_rules');

    Thread Starter mcnicks

    (@mcnicks)

    I have tried something similar … Maybe it would be more useful if I worked through and described a case where this definitely does not work.

    I have set up a fresh version of WordPress 2.0 on a VirtualHost on my Windows XP laptop. I have dropped and recreated the database to make sure nothing pre-exists the installation. I perform the WP installation, and set up permalinks and category tags (ie the rewrite code and Apache mod_rewrite are definitely working). Then I add the following plugin into the plugins dir:

    <?php
    /*
    Plugin Name: Test Plugin
    Version: 1.0
    Author: Me
    Description: Test Plugin
    */

    function foo_rewrite_rules( $rules ) {

    $newrules[‘foo/(.*)$’] = ‘/wp-content/plugins/foo.php?uri=$1’;
    return array_merge( $newrules, $rules );
    }

    add_filter( ‘rewrite_rules_array’, ‘foo_rewrite_rules’ );

    ?>

    And created a dummy foo.php beside it with the content:

    <h1>Foo</h1>

    When I browse to https://test.local/foo/bar.html I see the WordPress index page. If I disable the test plugin and reload, I get a 404 error. So the rewrite rules must be doing _something_ …

    This was done with Apache 2.0.55 and MySQL 4.1.6 on Windows XP SP2, and the URLs were viewed with Firefox 1.5.

    PS: test.local is aliased to 127.0.0.1 in my hosts file, and Apache is set up to NameVirtualHost 127.0.0.1, so that I can set up and test multiple web sites like this one – I do not imagine that this will be affecting the issue, though.

    Thread Starter mcnicks

    (@mcnicks)

    Ok, I think I have something working in theory. It looks like I have to add threee filters:

    1. Rewrite the URL so that it puts the path components into query variables, like so:

    add_filter( ‘rewrite_rules_array’, ‘foo_rewrite_rules’ );

    function foo_rewrite_rules ( $rewrite ) {
    global $wp_rewrite;

    // add rewrite tokens
    $wp_rewrite->add_rewrite_tag( ‘%fooaction%’, ‘(.+)’, ‘fooaction=’ );

    $foo_structure = $wp_rewrite->root . “foo/%fooaction%/”;
    $foo_rewrite = $wp_rewrite->generate_rewrite_rules($foo_structure);

    return ( $rewrite + $foo_rewrite );
    }

    2. Tell WP about the fooaction query variable:

    add_filter( ‘query_vars’, ‘foo_query_vars’ );

    function foo_query_vars ( $vars ) {
    $vars[] = “fooaction”;
    return $vars;
    }

    3. Write an is_foo() function so we can tell when we are in the foo ‘application’:

    function is_foo () {
    global $wp_query;

    return $wp_query->get( “fooaction” );
    }

    4. Finally, override template reidrection to return the relevant foo page templates if we are in the foo application:

    add_filter( ‘template_redirect’, ‘foo_redirect’ );

    function foo_redirect () {

    if ( is_foo() ) {

    include( ABSPATH . ‘wp-content/plugins/foo/main.php’ );
    exit;
    }
    }

    And that’s it! The main.php file can still use the current theme just by calling the usual template functions, eg:

    <?php get_header(); ?>

    <div id=”content” class=”narrowcolumn”>

    <div class=”post”>
    <h2>Foo</h2>
    <div class=”entrytext”>
    You are in the foo application.
    </div>
    </div>
    </div>

    <?php get_sidebar(); ?>

    <?php get_footer(); ?>

    Thread Starter mcnicks

    (@mcnicks)

    Also, the rewriting filter does not need to be as complicated as the one above (which I pinched from the Jerome’s Keywords example in the codex, btw). This works too, and also gives you an ‘index page’:

    function foo_rewrite_rules ( $rules ) {
    global $wp_rewrite;

    $foo_rules[‘foo/?$’] = ‘index.php?fooaction=index’;
    $foo_rules[‘foo/(.+)?$’] = ‘index.php?fooaction=$matches[1]’;

    return ( $rules + $foo_rules );
    }

    Ohh and, the query vars tip came from Christine Davis of UltimateTagWarrior fame. She writes about it here:

    https://www.neato.co.nz/archives/2005/12/05/wordpress-20-rc1-plugins-and-url-rewriting/

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Rewrite rules for new plugin’ is closed to new replies.