Search code examples
wordpresspermalinks

Custom permalink option not works, custom structure ignored and post-name used


At /wp-admin/options-permalink.php of a domain root with Wordpress (and no .htaccess), the first option, that is the standard option, is working, http://www.example.com/?p=123

So I think that is possible to reproduce (only to reproduce) exactly the same thing at the custom option... I try /?p=%post_id%, ?p=%post_id% and many other and always the effect is navigation as /post-name/ not ?p=id ... So, is not working? What exactly I need to copy/paste in the form box of "Custom structure" option?

I am using new wordpress, v4.4

PS: no clues at https://codex.wordpress.org/Settings_Permalinks_Screen


NOTES

Why?

Because "Custom permalink" is not working.... Or, in fact, it NOT WORKS on Wordpress?

It is a technical proof, when working I can do anything.
Example: need for hash, ?p=%post_id%#post-%post_id% is so important, for jump slide show, and scrool to page title.

In this example the problem is not a kind of Apache permission or rewrite mode, the ?p=123#post-123 works fine (!) in any context, therefore, Wordpress must comply with the required "Custom permalink".


Solution

  • It's an interesting question, but almost everything you want isn't possible.

    1. Use %post_id% twice:

      In wp-includes/class-wp-rewrite.php, at line 558 (WP 4.4) we have a for-loop that breaks if %post_id% was already found and used. The code is:

      preg_match_all('/%.+?%/', $this->permalink_structure, $tokens);
      $tok_index = 1;
      foreach ( (array) $tokens[0] as $token) {
          if ( '%post_id%' == $token && ($tok_index <= 3) ) {
              $front = $front . 'date/';
              break;
          }
          $tok_index++;
      }
      
    2. Using '#':

      In wp-admin/options-permalink.php, when wp is saving the user input we have this line of code:

      $permalink_structure = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $permalink_structure ) );
      

      That str_replace makes impossible using `#' char.

    3. The purpose of that screen:

      Wordpress uses that configuration screen for (1) give user the option of using pretty links or not and (2) give user the chance to choose how will be his posts permalink structure. Worpress itself handles a lot of rewrite rules that is not possible changing, unless you use it's url rewrite functions. Pages and custom posts, for instance, are handled differently.

    You can try adding /index.php?p=%post_id&var=%post_id% as custom structure to see the magic (not) happening, but remember: it just works for posts.

    Hope it helps.