Rewrite rule problem
Rewrite rule problem
Rewrite rule problem
Which works great when going to this URL: http://mylocalsite.site/?author=1&author_view=pinboard Yes, and that is because in that URL, the author value is a user ID. But in the “pretty” version of that URL (e.g. http://mylocalsite.site/author/admin/pinboard), WordPress uses the user’s “nice name” (from the user_nicename column in the wp_users table) which by default is the same as … Read more
WordPress shows front page, when it should show 404 with pagination style urls
Well I solved it – in a rather unsatisfactory way – by explicitly re-defining the default posts rule in my own code. i.e. $rewrite_keywords_structure = $wp_rewrite->root.”/%year%/%monthnum%/%postname%/”; ..etc.. I don’t really understand why this is necessary, but there you go.
This is the working code: add_action(‘init’, ‘rewrite_init’); // Rewrite add_action(‘query_vars’, ‘rewrite_query_vars’); add_filter(‘template_include’, ‘rewrite_template_include’); function rewrite_init(){ add_rewrite_rule(‘([^/]+)/([0-9]+)/?$’, ‘index.php?catname=$matches[1]¤tpage=$matches[2]’, ‘top’); add_rewrite_rule(‘cam/([^/]+)/?$’, ‘index.php?name=cam&perf=$matches[1]’, ‘top’); } function rewrite_query_vars($query_vars){ $query_vars[] = ‘currentpage’; $query_vars[] = ‘catname’; $query_vars[] = ‘perf’; return $query_vars; } function rewrite_template_include($template){ if(get_query_var(‘currentpage’) || get_query_var(‘catname’)){ $template = locate_template(array(‘category.php’)); } elseif(get_query_var(‘perf’)){ $template = locate_template(array(‘single.php’)); } return $template; }
Set the option that stores the rewrite configuration to autoload via the 4th argument of add_option: add_option( ‘my_plugin_option’, $my_plugin_options, ”, ‘yes’ ); It’ll get loaded and cached with the query that already runs on every page load, so it’s a non-issue. Then you can add your rewrite rules on the correct hook, which is init, … Read more
The second argument of add_rewrite_rule is not a path, it the query string you want WordPress executes. This should work for example.com/sub-folder/thepage/ (change page_id with ID of your page): add_rewrite_rule(‘thepage/’, ‘index.php?page_id=12’); or with page slug: add_rewrite_rule(‘thepage/’, ‘index.php?pagename=thepage’); As you have WordPress in a subdirectory and you want to rip out the directoy name, you have … Read more
I think you should stop using rewrite endpoints to handle JSON responses. Instead you can and you should use the REST API. So, instead of this: function makeplugins_add_json_endpoint() { add_rewrite_endpoint( ‘json’, EP_PERMALINK | EP_PAGES ); } add_action( ‘init’, ‘makeplugins_add_json_endpoint’ ); And then handle the JSON response at your own, you can and you should do … Read more
add_rewrite_rule() cannot automatically create that permalink structure for you. You should use wp_link_pages_link filter. E.g: add_filter(‘wp_link_pages_link’, function($link, $i) { global $post, $photo_id; if ($photo_id && ‘gallery’ === $post->post_type) $link = $link . “https://wordpress.stackexchange.com/” . intval($photo_id) . “https://wordpress.stackexchange.com/”; return $link; }, PHP_INT_MAX, 2); Then, you must use add_rewrite_tag() to make WordPress aware of photo query string: … Read more
Ok after 4 hours I managed to make it work function pl_add_rewrite_rule() { // this works with pagination too add_rewrite_rule(‘^slike(\/([a-z]+))?(\/([a-z]+))?(\/page\/([0-9]+))?$’,’index.php?page_id=45893&carstvo=$matches[2]&kategorija=$matches[4]&paged=$matches[6]’,’top’); add_rewrite_tag(‘%carstvo%’,'([^/]*)’); add_rewrite_tag(‘%kategorija%’,'([^/]*)’); } add_action(‘init’, ‘pl_add_rewrite_rule’, 10, 0);