[wp-hackers] Help to fix orphaned plugin

Hikari lists at hikarinet.info
Wed Feb 24 06:57:28 UTC 2010


Just to give a feedback to anybody that may have the same problem, or in case somebody understand rewrite rules more than me and may 
help.

Anybody interested and that didn't read previous posts, they are quoted below, just read from bottom to top.




All the problem is in these 2 lines:

$scategory_structure = $wp_rewrite->root . "%scategory%/%postname%";
return array_merge($post_rewrite, $wp_rewrite->generate_rewrite_rules($scategory_structure));


If they are present, posts are returned properly, but paginated comments return 404. If they are not (with a simple " return 
$post_rewrite; " ), posts return 404 when %scategory% rewrite tag is used anywhere in permastruct.

What I was able to understand from this code, it adds "%scategory%/%postname%", "%scategory%" and "%postname%" to possible 
permastructs. Since my structure is not any of those, I thought it shouldn't be needed, but the situation is that without it 
Wordpress returns 404...



Reading what original author wrote in change log and comparing differences from v0.3.0 to v0.4.0, it seems those 2 lines are the 
cause of paginated comments returning 404, and somehow he was able to make the plugin works without them. But I'm not able to remove 
them and stop 404, and neither able to keep them and fix paginated comments.

After v0.4.0 until v0.6.2, this code was never changed again, he just changed UI features and how post category and captured and 
stored. So v0.4.0 has the final fix for rewrite rule. Currently I'm using a modified version of v0.6.2, with these lines grabbed 
from v0.3.0, and it has been working from what I can see.





BUT, I've found a better way to fix all this mess, it's just a matter to simplify everything.

As everybody who understands how Wordpress deals with rewrite rules and permalink generation, there are 2 totally separate codes for 
rewrite rules parsing and for generating the permalink.

All the trick remains in making these 2 codes match: for each post (and its subpages, exemple multipages, paginated comments, feeds, 
etc), the rewrite rule parsing code must be able to parse what the permalink generator code generates. If they are able to match, 
the website will present in its pages, links to posts and their subpages, and when a user clicks on those links, the rewrite rule 
parsing code will find out what that URL means (what the permalink generator meant to tell it), and return that page.

It's really a very complex system, since a new added rewrite tag won't instantly be known by the permalink generator; and if a 
filter is hooked to permalink generator and changes its resource, rewrite rule parser won't instantly be prepared to understand it. 
The trick for developing a plugin is prepare both of them together and make sure they keep shaking hands.



In the specific situation of this plugin, it could be very simpler, because there is already a rewrite tag that refers to 
categories.

The whole idea of the plugin is let users define a specific category to be used in permalinks, other than the default category with 
lowest ID.

Rewrite rule parser accepts any category that it's provided, even if it's different from the one that permalink generator chooses. 
Once it's identified as a category, it moves on.

SO, if permalink generator was able to set a user chosen category instead of the one with lowest ID, rewrite rules parser wouldn't 
need to have a line of code changed to accept it. The problem is that permalink generator always uses the category with lowest ID, 
and once a filter is available to change the permalink, all rewrite tags were already replaced.

THAT's why we must create new rewrite tags when we want permalinks to be different from default, because we don't even have 
opportunity to change what permalink generator does with default rewrite tags.

If plugins had priority over default core code on dealing with permastructs, we'd have more flexibility. I accept the fact that 
%category% is the rewrite tag that would be most improved with it. Maybe some crazy guy wanting to have the most flexibility and 
wants to use %tag% in permalink :P


Anyway, if another filter was available in the permalink generator code before it started parsing the permastruct, no harm would be 
made and this kind of stuff would be much simpler to be done, because custom permalinks would be able to be generated without 
together having to hack rewrite rules parser.

I've added a ticket to trac suggesting this new filter to be added: http://core.trac.wordpress.org/ticket/12240
I hope it's accepted ^^


Sorry for these large emails and for bothering about this issue :)


---------------------------
Hikari -  A Luz ilumina a PAZ
http://Hikari.ws
http://ConscienciaPlanetaria.com

Tenha seu próprio email meunome @ ConscienciaPlanetaria.com.br!: http://seunome.ConscienciaPlanetaria.com.br


----- Original Message ----- 
Sent: Monday, 15 February, 2010 2:16 AM


Ok, now I'm cool I tried again, searched on WP doc and verified its source. Now I understand a bit better how rewrite rule works and
narrowed down the source of the bug.


First of all, here's my permalink:
/%scategory%/%post_id%/%postname%/

/%scategory% is added by the plugin



v0.3.0 had this code in a function hooked to filter 'post_rewrite_rules':

{
...

    $scategory_structure = $wp_rewrite->root . "%scategory%/%postname%";
    return array_merge($rewrite, $wp_rewrite->generate_rewrite_rules($scategory_structure));
}



THIS array_merge was removed from later versions. I have no idea what it does exactaly, but adding it back fixes the bug of posts
returning 404.

Apparently it is also the cause of paged comments returning 404, and maybe that's why the author removed it. But without it, ANY
permalink I try that uses /%scategory% anywhere will always generate 404 for posts.


I was only able to understand that generate_rewrite_rules() is the main function for generating all rewrite rules, but I can't
understand what its other parameters mean, and I have no idea why adding "%scategory%/%postname%" to rewrite rules is required to
make sCategory works, or why it's only needed on my 2 sites (if author removed it and released a few versions later, I suppose the
bug doesn't happen with him).


Is anybody able to understand it and would help me find a way to make the new rewrite tag works without breaking comments pages?



----- Original Message ----- 
Sent: Monday, 15 February, 2010 1:09 AM


Hello guys, as a coincidence I'm having trouble with a plugin whose author is not accessible. It's essencial for my sites and I'm
not able to fix it.

The plugin is sCategory Permalink: http://wordpress.org/extend/plugins/scategory-permalink/

It's on version 0.6.2, but I'm still using 0.3.0.



- 0.3.0 would be perfect if it didn't have a bug: comments pages return 404, so comments can't be breaken on pages.

- 0.4.0 fix this bug, but this version generates 404 for any permalink that uses %scategory% in it

- any other version after 0.4.0 has the same 404 bug, only 0.3.0 works



Well, I've tried everything. Disabled and re-enabled permalinks a lot of times, tried all kinds of permalinks. As soon as I remove
%scategory% from permalink posts start back working. As soon as I add %scategory% back 404 is back too.

I decided to concentrate on 0.3.0 to 0.4.0 differences because they are almost the same.



There are basically 2 changes on these versions, one of them must be creating the 404 bug:

1) The following function is moved from 'post_rewrite_rules' filer to 'init' action:

0.3.0:

function addRewriteRules($rewrite) {
    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag('%scategory%', '(.+?)', 'category_name=');

    $scategory_structure = $wp_rewrite->root . "%scategory%/%postname%";

    $structure = ltrim($wp_rewrite->permalink_structure, '/');
    if (0 === strpos($structure, '%scategory%'))
        $wp_rewrite->use_verbose_page_rules = true;

    return array_merge($rewrite, $wp_rewrite->generate_rewrite_rules($scategory_structure));
}


0.4.0:

function addRewriteRules() {
    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag('%scategory%', '(.+?)', 'category_name=');
}




2) this new function, added to 0.4.0, hooked to 'post_rewrite_rules' filter:

function setVerbosePageRules($post_rewrite) {
    global $wp_rewrite;
    if (preg_match("/^[^%]*%scategory%/", $wp_rewrite->permalink_structure) )
        $wp_rewrite->use_verbose_page_rules = true;

    return $post_rewrite;
}





The lack of comments explaining what he is doing makes it harder to understand. IDK why he split the old function into 2 and moved
part of it to 'init'. What he wanted to do and consequences.

I'm also not able to distinguish what part of code is fixing comment pages bug, and where permalink bug may be happening.

Any help will be very much appreciated.



More information about the wp-hackers mailing list