In my plugin I’m doing something like
function gra4_set_seo_friendly_rules($args)
{
$new = array();
$new['(gra4)(/[-/0-9a-zA-Z]+)?/(.*)$'] = 'index.php?pagename=$matches[1]';
return $new + $args;
}
add_filter("rewrite_rules_array", "gra4_set_seo_friendly_rules");
I also do $wp_rewrite->flush_rules();
on init.
I guess it should cover all URLs ‘longer’ than my plugin page (the plugin inserts content into a page),so anything like:
https://myserver.com/wp/gra4/whatever/i/put/here
pretty much is reflected to:
https://myserver.com/wp/gra4
with no 404 pages.
That’s pretty much how it works. Well, almost =)
On some URLs it still fires 404. What really surprises me, it happens only when POST data is present – the same page with GET only works ok. It also happens not to all URLs (so far I discovered only two malfunctions, allthough both with plenty of POST data), majority POST pages work just fine.
I commented all my code out… well, it does not matter, I guess when I see This is somewhat embarrassing, isn’t it? it does not even hit my code yet, right?
Any ides? I kinda lost, don’t even know where to start digging…
Thank you.
]]>$wp_rewrite->flush_rules();
to flush the permalink structure, but you have attached it to the INIT hook. Rewrite rules should never be flushed on INIT, only on activation.
When you flush rewrite rules on INIT (or many of hooks), you cause the permalinks to be flushed on every page load, and this, aside from causing poor performance, is also extremely prone to creating conflicts with other plugins that register custom post types or taxonomies.
Please remove the $wp_rewrite->flush_rules();
and instruct users to flush the permalinks themselves or move $wp_rewrite->flush_rules();
to an activation hook instead.
https://www.ads-software.com/extend/plugins/gallery-plugin/
]]>Any help is highly appreciated
Thanks!
Quokka
add_action('generate_rewrite_rules', 'my_generate_rewrite_rules');
function my_generate_rewrite_rules( $wp_rewrite )
{
$new_rules = array(
'projects/?$' => 'index.php?paged=1&post_type=project&orderby=title&order=ASC',
'projects/page/?([0-9]{1,})/?$' => 'index.php?&paged=$matches[1]&post_type=project&orderby=title&order=ASC',
'projects/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?&feed=$matches[1]&post_type=project',
'projects/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?&feed=$matches[1]&post_type=project'
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
I also added a custom menu item with an href of “/projects”. Well, that didn’t give me what I wanted either, so I decided to use the project.php template/page approach again. Basically reverted back to before, using a page and using the menu builder. But now, the permalink for the page is “projects-1”.
I’ve tried flushing the rewrite rules by adding
add_action( 'generate_rewrite_rules','deactivate');
function deactivate() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
to functions.php, I’ve tried changing settings>permalinks to “reset” the permalinks, and I’ve tried deleting the page and reassembling it. Now it’s “projects-2”! Obviously trashed pages’ permalink information is continuing to exist somewhere in the database, but I’m hesitant to go in there and just start dropping values. I obviously want the project page’s permalink to be /project.
Any ideas?
]]>I got a very strange problem. I want to add some custom rewrite-rules to my theme in order to get custom pretty permalinks. I must pass variables in the URL so I can adjust the content according to what the user selects in a submenu. In order to do this I use pretty much the standard procedure as described in the Codex.
It actually works, BUT ONLY if I add a php echo to the function my_flush_rules where I call $wp_rewrite->flush_rules().
If I leave out the echo or just echo an empty string it stops working. In this case no more variables are passed on and the urls aren’t rewritten anymore, so I suppose the new URL-rewrite rules simply aren’t applied.
As you may have guessed I do not want to echo some strange text just so url-rewriting works properly. What did I do wrong?
I’m totally stuck here, can anyone help?
Here’s my code from the functions.php:
add_filter('rewrite_rules_array', 'my_rewrite_rules');
add_filter('query_vars','my_rewrite_query_vars');
add_filter('init','my_flush_rules');
function my_flush_rules(){
echo "whatever";
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function my_rewrite_rules( $rewrite_rules ) {
$new_rules = array(
'(test)/([a-z]+)$' => 'index.php?pagename=$matches[1]&mycategoryvar=$matches[2]',
'(test)/([0-9]+)$' => 'index.php?pagename=$matches[1]&mypagevar=$matches[2]',
'(test)/([a-z]+)/([0-9]+)$' => 'index.php?pagename=$matches[1]&mycategoryvar=$matches[2]&mypagevar=$matches[3]'
);
$rewrite_rules = $new_rules + $rewrite_rules;
return $rewrite_rules;
}
function my_rewrite_query_vars($qvars)
{
array_push($qvars, 'mypagevar', 'mycategoryvar');
return $qvars;
}
And here’s my code from the .htaccess (I currently develop the page on my local XAMPP installation – hence the url):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) https://localhost/wordpress/$1/ [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
]]>I’ve been struggling with this for a while, and the documentation on the rewrite API is a little scarce. So I hope someone’s out there who can shed some light on this.
If I add rewrite rules, do I have to flush the rules on every page load? It seems that if I don’t flush them, it will keep working for a short while. Then after a couple of minutes it will break.
Flushing the rules currently causes an extra 100 (!) mysql queries on each page load. The queries look like this:
[23] => Array
(
[0] => SELECT * FROM wp_posts WHERE ID = 5106 LIMIT 1
[1] => 0.000135898590088
[2] => require, wp, WP->main, do_action_ref_array, call_user_func_array, XIC_flush, WP_Rewrite->flush_rules, WP_Rewrite->wp_rewrite_rules, WP_Rewrite->rewrite_rules, WP_Rewrite->page_rewrite_rules, WP_Rewrite->page_uri_index, get_page_uri, get_page, get_post
)
[24] => Array
(
[0] => SELECT <code>post_parent</code> FROM wp_posts WHERE ID = 5068 LIMIT 1
[1] => 0.0001220703125
[2] => require, wp, WP->main, do_action_ref_array, call_user_func_array, XIC_flush, WP_Rewrite->flush_rules, WP_Rewrite->wp_rewrite_rules, WP_Rewrite->rewrite_rules, WP_Rewrite->page_rewrite_rules, WP_Rewrite->page_uri_index, get_page_uri, get_page, get_post, _get_post_ancestors
)
[25] => Array
(
[0] => SELECT ID, post_name, post_parent FROM wp_posts WHERE post_type = 'attachment' AND post_parent = 5106
[1] => 0.000179052352905
[2] => require, wp, WP->main, do_action_ref_array, call_user_func_array, XIC_flush, WP_Rewrite->flush_rules, WP_Rewrite->wp_rewrite_rules, WP_Rewrite->rewrite_rules, WP_Rewrite->page_rewrite_rules, WP_Rewrite->page_uri_index
)
They are “small” queries, but still I’d love to get rid of them..
]]>However the rewrite rules (and hence taxonomy templates) don’t work for your new taxonomies until you either call $wp_rewrite->flush_rules();
from a plugin registration or happen to visit the permalink settings page in the admin. Registering a new taxonomy doesn’t automatically flush the rewrite rules.
What’s best practice in theme development for handling this issue?
]]>More to the point, I cannot login now – except by going to the [e] Edit link on any page and clicking on that. The normal login method doesnt work and I am hoping to restore it… if only I knew how. Can some kind soul help?
TIA,
Gary
————
[12-Jun-2008 21:57:07] PHP Fatal error: Call to a member function flush_rules() on a non-object in /home1/findgood/public_html/xxx/wp-content/plugins/advanced-permalinks/advanced-permalinks.php on line 256
[12-Jun-2008 22:20:07] PHP Warning: include(/home1/findgood/public_html/xxx/wp-content/plugins/redirection/models/redirection_item.php) [function.include]: failed to open stream: No such file or directory in /home1/findgood/public_html/xxx/wp-content/plugins/redirection/redirection.php on line 51
[12-Jun-2008 22:20:07] PHP Warning: include() [function.include]: Failed opening ‘/home1/findgood/public_html/xxx/wp-content/plugins/redirection/models/redirection_item.php’ for inclusion (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home1/findgood/public_html/xxx/wp-content/plugins/redirection/redirection.php on line 51
[12-Jun-2008 22:20:07] PHP Warning: include(/home1/findgood/public_html/xxx/wp-content/plugins/redirection/models/redirector.php) [function.include]: failed to open stream: No such file or directory in /home1/findgood/public_html/xxx/wp-content/plugins/redirection/redirection.php on line 52
[12-Jun-2008 22:20:07] PHP Warning: include() [function.include]: Failed opening ‘/home1/findgood/public_html/xxx/wp-content/plugins/redirection/models/redirector.php’ for inclusion (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home1/findgood/public_html/xxx/wp-content/plugins/redirection/redirection.php on line 52
[12-Jun-2008 22:20:07] PHP Warning: include(/home1/findgood/public_html/xxx/wp-content/plugins/redirection/models/log.php) [function.include]: failed to open stream: No such file or directory in /home1/findgood/public_html/xxx/wp-content/plugins/redirection/redirection.php on line 53
[12-Jun-2008 22:20:07] PHP Warning: include() [function.include]: Failed opening ‘/home1/findgood/public_html/xxx/wp-content/plugins/redirection/models/log.php’ for inclusion (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home1/findgood/public_html/xxx/wp-content/plugins/redirection/redirection.php on line 53
[12-Jun-2008 22:20:07] PHP Fatal error: Class ‘Redirector_Factory’ not found in /home1/findgood/public_html/xxx/wp-content/plugins/redirection/redirection.php on line 64
[12-Jun-2008 22:48:50] PHP Fatal error: Call to a member function flush_rules() on a non-object in /home1/findgood/public_html/xxx/wp-content/plugins/advanced-permalinks/advanced-permalinks.php on line 256