2

Working on a plugin that creates short codes to embed into a page.

I have permalinks set to "Post name", so http://example.com/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.com/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.com/mypage/foo. http://example.com/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.com/category?test=123 I get 123 in $test as expected. When I go to http://example.com/category/123 I get an error 404. I have updated the permalinks in wp-admin.

3

In 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.com/mypage/foo.

Because your RegEx pattern is ^category/([^/]+)/?, so the URL you should have visited is http://example.com/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.com/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. :)

  • I feel so stupid - I completely overlooked the fact that category is a reserved name in WordPress. – Craig Jacobs May 7 at 17:01
  • 1
    Yes, it's reserved. But it can be changed via the Permalink Settings page - see "Category base" at the bottom of the page. – Sally CJ May 8 at 1:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.