| 1 | // Here is my plugin's init function, where it adds the endpoint: |
|---|
| 2 | |
|---|
| 3 | $langSwitchPermalink = "langswitch_lang"; |
|---|
| 4 | |
|---|
| 5 | function langswitch_init () { |
|---|
| 6 | global $langSwitchPermalink; |
|---|
| 7 | global $wp_rewrite; |
|---|
| 8 | |
|---|
| 9 | // this doesn't seem to work in global namespace |
|---|
| 10 | add_rewrite_endpoint( $langSwitchPermalink, |
|---|
| 11 | EP_PERMALINK + |
|---|
| 12 | EP_DATE + |
|---|
| 13 | EP_YEAR + |
|---|
| 14 | EP_MONTH + |
|---|
| 15 | EP_DAY + |
|---|
| 16 | EP_ROOT + |
|---|
| 17 | EP_COMMENTS + |
|---|
| 18 | EP_SEARCH + |
|---|
| 19 | EP_CATEGORIES + |
|---|
| 20 | EP_AUTHORS + |
|---|
| 21 | EP_PAGES ); |
|---|
| 22 | $wp_rewrite->flush_rules(); |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | // The actual permalink sections are added with filters, e.g.: |
|---|
| 26 | |
|---|
| 27 | add_filter( 'the_permalink', 'langswitch_add_language_url' ); |
|---|
| 28 | add_filter( 'post_link', 'langswitch_add_language_url' ); |
|---|
| 29 | |
|---|
| 30 | // Definition of that function (somewhat simplified -- the real version |
|---|
| 31 | // does a lot more checking |
|---|
| 32 | |
|---|
| 33 | $langswitch_lang = "es"; |
|---|
| 34 | |
|---|
| 35 | function langswitch_add_language_url( $urlstr ) |
|---|
| 36 | { |
|---|
| 37 | global $langSwitchPermalink, $langswitch_lang; |
|---|
| 38 | |
|---|
| 39 | return langswitch_trailingslashit( $urlstr ) . $langSwitchPermalink . "/" . |
|---|
| 40 | $langswitch_lang; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | function langswitch_trailingslashit( $uri ){ |
|---|
| 44 | if( $uri{ strlen($uri) - 1 } != '/' ) { |
|---|
| 45 | return $uri . '/'; |
|---|
| 46 | } else { |
|---|
| 47 | return $uri; |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | ///////////////////////////////// |
|---|
| 53 | Here is a printout of the rewrite rules being generated, with the |
|---|
| 54 | permalink set to /%category%/%postname%/ and a couple of pages present in the database. |
|---|
| 55 | |
|---|
| 56 | Current blog URL is http://poplarware.com/WPTest/ |
|---|
| 57 | I also have a hack installed on that page that prints |
|---|
| 58 | out which rule matched whenever you visit a link; the rewrite |
|---|
| 59 | rules are also printed out at the top of every post. Here they |
|---|
| 60 | are currently: |
|---|
| 61 | |
|---|
| 62 | robots.txt$ |
|---|
| 63 | wp-atom.php$ |
|---|
| 64 | wp-rdf.php$ |
|---|
| 65 | wp-rss.php$ |
|---|
| 66 | wp-rss2.php$ |
|---|
| 67 | wp-feed.php$ |
|---|
| 68 | wp-commentsrss2.php$ |
|---|
| 69 | (another-page/about)/trackback/?$ |
|---|
| 70 | (another-page/about)/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 71 | (another-page/about)/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 72 | (another-page/about)/page/?([0-9]{1,})/?$ |
|---|
| 73 | (another-page/about)/langswitch_lang(/(.*))?/?$ |
|---|
| 74 | (another-page/about)(/[0-9]+)?/?$ |
|---|
| 75 | another-page/about/attachment/([^/]+)/?$ |
|---|
| 76 | another-page/about/attachment/([^/]+)/trackback/?$ |
|---|
| 77 | another-page/about/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 78 | another-page/about/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 79 | (another-page)/trackback/?$ |
|---|
| 80 | (another-page)/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 81 | (another-page)/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 82 | (another-page)/page/?([0-9]{1,})/?$ |
|---|
| 83 | (another-page)/langswitch_lang(/(.*))?/?$ |
|---|
| 84 | (another-page)(/[0-9]+)?/?$ |
|---|
| 85 | another-page/attachment/([^/]+)/?$ |
|---|
| 86 | another-page/attachment/([^/]+)/trackback/?$ |
|---|
| 87 | another-page/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 88 | another-page/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 89 | (test-static-post-page)/trackback/?$ |
|---|
| 90 | (test-static-post-page)/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 91 | (test-static-post-page)/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 92 | (test-static-post-page)/page/?([0-9]{1,})/?$ |
|---|
| 93 | (test-static-post-page)/langswitch_lang(/(.*))?/?$ |
|---|
| 94 | (test-static-post-page)(/[0-9]+)?/?$ |
|---|
| 95 | test-static-post-page/attachment/([^/]+)/?$ |
|---|
| 96 | test-static-post-page/attachment/([^/]+)/trackback/?$ |
|---|
| 97 | test-static-post-page/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 98 | test-static-post-page/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 99 | feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 100 | (feed|rdf|rss|rss2|atom)/?$ |
|---|
| 101 | page/?([0-9]{1,})/?$ |
|---|
| 102 | comments/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 103 | comments/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 104 | comments/page/?([0-9]{1,})/?$ |
|---|
| 105 | search/(.+)/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 106 | search/(.+)/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 107 | search/(.+)/page/?([0-9]{1,})/?$ |
|---|
| 108 | search/(.+)/langswitch_lang(/(.*))?/?$ |
|---|
| 109 | search/(.+)/?$ |
|---|
| 110 | category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 111 | category/(.+?)/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 112 | category/(.+?)/page/?([0-9]{1,})/?$ |
|---|
| 113 | category/(.+?)/langswitch_lang(/(.*))?/?$ |
|---|
| 114 | category/(.+?)/?$ |
|---|
| 115 | author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 116 | author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 117 | author/([^/]+)/page/?([0-9]{1,})/?$ |
|---|
| 118 | author/([^/]+)/langswitch_lang(/(.*))?/?$ |
|---|
| 119 | author/([^/]+)/?$ |
|---|
| 120 | ([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 121 | ([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 122 | ([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$ |
|---|
| 123 | ([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/langswitch_lang(/(.*))?/?$ |
|---|
| 124 | ([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$ |
|---|
| 125 | ([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 126 | ([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 127 | ([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$ |
|---|
| 128 | ([0-9]{4})/([0-9]{1,2})/langswitch_lang(/(.*))?/?$ |
|---|
| 129 | ([0-9]{4})/([0-9]{1,2})/?$ |
|---|
| 130 | ([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 131 | ([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 132 | ([0-9]{4})/page/?([0-9]{1,})/?$ |
|---|
| 133 | ([0-9]{4})/langswitch_lang(/(.*))?/?$ |
|---|
| 134 | ([0-9]{4})/?$ |
|---|
| 135 | (.+?)/([^/]+)/trackback/?$ |
|---|
| 136 | (.+?)/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 137 | (.+?)/([^/]+)/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 138 | (.+?)/([^/]+)/page/?([0-9]{1,})/?$ |
|---|
| 139 | (.+?)/([^/]+)/langswitch_lang(/(.*))?/?$ |
|---|
| 140 | (.+?)/([^/]+)(/[0-9]+)?/?$ |
|---|
| 141 | .+?/[^/]+/([^/]+)/?$ |
|---|
| 142 | .+?/[^/]+/([^/]+)/trackback/?$ |
|---|
| 143 | .+?/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 144 | .+?/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 145 | .+?/[^/]+/attachment/([^/]+)/?$ |
|---|
| 146 | .+?/[^/]+/attachment/([^/]+)/trackback/?$ |
|---|
| 147 | .+?/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 148 | .+?/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 149 | (.+?)/feed/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 150 | (.+?)/(feed|rdf|rss|rss2|atom)/?$ |
|---|
| 151 | (.+?)/page/?([0-9]{1,})/?$ |
|---|
| 152 | (.+?)/langswitch_lang(/(.*))?/?$ |
|---|
| 153 | (.+?)/?$ |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|