Working on a plugin that creates short codes to embed into a page.
I have permalinks set to "Post name", so /
I want to be able to pass additional information in the URL so the short code can use it, for example: / as opposed to ?name=value
On our dev servers, this works and I can do something like the following to get the value:
$url = home_url( $wp->request );
$page_id = $post->post_name;
$thing_pos = strpos( $url, "/" . $page_id );
$thing = substr( $url, $pos + strlen( $page_id ) + 2 );
$thing now equals "bar-thing";
On some servers, however, I get a "That page can’t be found" message. (the page id is correct) I have compared the settings and I can't find any differences in permalinks settings, etc.
All the servers are running 5.1.1
I'm sure I'm missing something obvious, but I've searched for a couple of hours and I can't locate any info about this - I'm probably not using the correct search terms.
Any help would be appreciated.
Update 1
I've added the following code to my plugin php:
function custom_rewrite() {
add_rewrite_tag(
'%myid%',
'([^/]+)'
);
add_rewrite_rule(
'^category/([^/]+)/?',
'index.php?pagename=mypage&myid=$matches[1]',
'top'
);
}
add_action( 'init', 'custom_rewrite' );
And I'm still getting a 404 when I go to "mypage" with anything following the pagename, like so: . works fine and the plugin loads and executes.
Update 2
Been working on this all day, still can't get it to work. The current code:
In functions.php
add_filter('query_vars', 'add_query_vars', 10, 1);
add_action('init', 'add_rewrite_rules');
function add_query_vars($vars) {
$vars[] = "test";
return $vars;
}
function add_rewrite_rules() {
add_rewrite_rule('^category/([0-9]+)/?', 'category?test=$1', 'top');
}
In the plugin:
$test = get_query_var( 'test' );
When I go to this a URL like this: I get 123 in $test as expected. When I go to I get an error 404. I have updated the permalinks in wp-admin.
Working on a plugin that creates short codes to embed into a page.
I have permalinks set to "Post name", so http://example/post-name/
I want to be able to pass additional information in the URL so the short code can use it, for example: http://example/foo-page/bar-thing/ as opposed to ?name=value
On our dev servers, this works and I can do something like the following to get the value:
$url = home_url( $wp->request );
$page_id = $post->post_name;
$thing_pos = strpos( $url, "/" . $page_id );
$thing = substr( $url, $pos + strlen( $page_id ) + 2 );
$thing now equals "bar-thing";
On some servers, however, I get a "That page can’t be found" message. (the page id is correct) I have compared the settings and I can't find any differences in permalinks settings, etc.
All the servers are running 5.1.1
I'm sure I'm missing something obvious, but I've searched for a couple of hours and I can't locate any info about this - I'm probably not using the correct search terms.
Any help would be appreciated.
Update 1
I've added the following code to my plugin php:
function custom_rewrite() {
add_rewrite_tag(
'%myid%',
'([^/]+)'
);
add_rewrite_rule(
'^category/([^/]+)/?',
'index.php?pagename=mypage&myid=$matches[1]',
'top'
);
}
add_action( 'init', 'custom_rewrite' );
And I'm still getting a 404 when I go to "mypage" with anything following the pagename, like so: http://example/mypage/foo. http://example/mypage works fine and the plugin loads and executes.
Update 2
Been working on this all day, still can't get it to work. The current code:
In functions.php
add_filter('query_vars', 'add_query_vars', 10, 1);
add_action('init', 'add_rewrite_rules');
function add_query_vars($vars) {
$vars[] = "test";
return $vars;
}
function add_rewrite_rules() {
add_rewrite_rule('^category/([0-9]+)/?', 'category?test=$1', 'top');
}
In the plugin:
$test = get_query_var( 'test' );
When I go to this a URL like this: http://example/category?test=123 I get 123 in $test as expected. When I go to http://example/category/123 I get an error 404. I have updated the permalinks in wp-admin.
Share Improve this question edited May 7, 2019 at 3:38 Craig Jacobs asked May 6, 2019 at 20:06 Craig JacobsCraig Jacobs 1334 bronze badges1 Answer
Reset to default 4In reply to "Update 1":
The code worked fine for me. So,
I'm still getting a 404 when I go to "mypage" with anything following the pagename, like so: http://example/mypage/foo.
Because your RegEx pattern is ^category/([^/]+)/?
, so the URL you should have visited is http://example/category/foo
. But then, you shouldn't use category
because that "base" is used for category archive permalinks — unless of course you changed it to something like topics
.
So you'd use ^mypage/([^/]+)/?
and not ^category/([^/]+)/?
.
However, you should know that the rewrite rule would conflict with pages which are children of the mypage
page. I.e. If foo
is a child of mypage
, then mypage/foo
would load the mypage
page and not the child foo
page.
In reply to "Update 2":
When I go to http://example/category/123 I get an error 404.
Because WordPress treats the request/URL as a category archive request, so WordPress tries to load a category with the slug of 123
and when not found, the error 404
is thrown. But the $test
(i.e. get_query_var( 'test' )
) would actually give you the expected value which is 123
as in the above example URL. :)