Understanding the Rewrite API with examples

Understanding the Rewrite API with examples

WordCamp Sofia 2015

Cf58282ee2e95590510aaa24b734c186?s=128

Marin Atanasov

October 24, 2015
Tweet

Transcript

  1. 1.
  2. 3.
  3. 4.
  4. 5.
  5. 6.
  6. 7.
  7. 8.
  8. 12.
  9. 13.
  10. 14.
  11. 15.
  12. 16.
  13. 17.

    # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule

    ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
  14. 18.

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="WordPress Rule"

    stopProcessing="true"> <match url=".*" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
  15. 19.
  16. 24.

    add_action( 'init', 'rae_add_custom_rewrite_rules' ); function rae_add_custom_rewrite_rules() { // Matches /event/2015/event-name/

    add_rewrite_rule( '^event/[0-9]{4}/([^/]+)/?$', 'index.php?name=$matches[1]&post_type=event', 'top' ); } add_rewrite_rule($regex, $redirect, $after)
  17. 28.

    add_action( 'init', 'rae_add_custom_rewrite_tags' ); function rae_add_custom_rewrite_tags() { add_rewrite_tag('%event_year%', '([0-9]{4})'); }

    add_action( 'init', 'rae_add_custom_rewrite_rules' ); function rae_add_custom_rewrite_rules() { // Matches /event/2015/event-name/ add_rewrite_rule( '^event/([0-9]{4})/([^/]+)/?$', 'index.php?event_year=$matches[1]&name=$matches[2]&post_type=event', 'top' ); }
  18. 29.
  19. 30.

    define('EP_NONE', 0); // 0000000000000 define('EP_PERMALINK', 1); // 0000000000001 define('EP_DATE', 4);

    // 0000000000100 define('EP_YEAR', 8); // 0000000001000 define('EP_PAGES', 4096); // 1000000000000 define('EP_ALL', 8191); // 1111111111111 // all posts or pages EP_PERMALINK | EP_PAGES // 1000000000001 // full dates (yyyy/mm/dd), years, months or days EP_DATE | EP_YEAR | EP_MONTH | EP_DAY // 0000000111100
  20. 31.
  21. 32.

    add_action( 'init', 'rae_add_custom_rewrite_endpoints' ); function rae_add_custom_rewrite_endpoints() { // Matches /post-name/view/gallery/

    // and /page-name/view/gallery/ add_rewrite_endpoint( 'view', EP_PERMALINK | EP_PAGES ); } add_rewrite_endpoint( $name, $places )
  22. 33.

    $args = array( 'with_front' => true, 'ep_mask' => EP_NONE, 'paged'

    => true, 'feed' => true, 'forcomments' => false, 'walk_dirs' => true, 'endpoints' => true, ) add_permastruct( $name, $struct, $args );
  23. 37.
  24. 38.

    add_action( 'init', 'rae_add_custom_rewrite_feeds' ); function rae_add_custom_rewrite_feeds() { add_feed( 'customfeed', 'rae_customfeed_display'

    ); } // Responsible for the RSS feed contents function rae_customfeed_display() { ... } add_feed( $feedname, $function )
  25. 39.
  26. 41.

    register_post_type( 'project', array( 'labels' => array( //... ), 'public' =>

    true, 'rewrite' => false, 'hierarchical' => false, ) ); register_taxonomy( 'project_category', 'project', array( 'labels' => array( // ... ), 'hierarchical' => true, 'rewrite' => false, ) );
  27. 42.

    // Add custom rewrite rules add_action( 'init', 'rae_add_custom_rewrite_rules' ); function

    rae_add_custom_rewrite_rules() { // Portfolio post type archive - /portfolio/ add_rewrite_rule( '^portfolio/?$', 'index.php?post_type=project', 'top' ); // Pagination for portfolio archive - /portfolio/page/5/ add_rewrite_rule( '^portfolio/page/?([0-9]{1,})/?$', 'index.php?post_type=project&paged=$matches[1]', 'top' ); }
  28. 43.

    // Add custom rewrite tags add_action( 'init', 'rae_add_custom_rewrite_tags' ); function

    rae_add_custom_rewrite_tags() { // Rewrite tag for the Project post type - non hierarchical add_rewrite_tag( "%project%", '([^/]+)' ); // Rewrite tag for the Project Category taxonomy - hierarchical add_rewrite_tag( "%project_category%", '(.+?)' ); }
  29. 44.

    // Add custom rewrite endpoints add_action( 'init', 'rae_add_custom_rewrite_endpoints' ); function

    rae_add_custom_rewrite_endpoints() { // Custom /gallery/ view for single project pages add_rewrite_endpoint( 'gallery', EP_PERMALINK ); // Custom /slideshow/ view for single project pages add_rewrite_endpoint( 'slideshow', EP_PERMALINK ); }
  30. 46.
  31. 47.
  32. 48.