php - WordPress custom slug (endpoint) and compare all links

admin2025-06-06  5

I work on the one i18n function. It will work with subdir. For example: eg/hello-world/en

For this I'm support EP_ALL for the WP custom endpoints.

function lang_add_endpoints(){
    add_rewrite_endpoint('en', EP_ALL);
    add_rewrite_endpoint('de', EP_ALL);
} 
add_action('init', 'lang_add_endpoints');

For example:

eg/en
eg/page/en
eg/post(hello-world)/fr
eg/10/11/2018/de
and more...

It is well done. Now, how can I add "/en/ or /de/ or /any/" slug all WP links if the query has "en".

For example:

I have COOKIE: (I have an idea just I say for my quest)

if($_COOKIE['lang'] == 'en') :

<a href="eg/example/en" >Example</a>
<a href="eg/05/11/2018/en" >Example</a>
<a href="eg/category/news/en" >Example</a>

elseif($_COOKIE['lang'] == 'de') :

<a href="eg/05/11/2018/de" >Example</a>
<a href="eg/tags/computer/de" >Example</a>

endif;

I work on the one i18n function. It will work with subdir. For example: eg/hello-world/en

For this I'm support EP_ALL for the WP custom endpoints.

function lang_add_endpoints(){
    add_rewrite_endpoint('en', EP_ALL);
    add_rewrite_endpoint('de', EP_ALL);
} 
add_action('init', 'lang_add_endpoints');

For example:

eg/en
eg/page/en
eg/post(hello-world)/fr
eg/10/11/2018/de
and more...

It is well done. Now, how can I add "/en/ or /de/ or /any/" slug all WP links if the query has "en".

For example:

I have COOKIE: (I have an idea just I say for my quest)

if($_COOKIE['lang'] == 'en') :

<a href="eg/example/en" >Example</a>
<a href="eg/05/11/2018/en" >Example</a>
<a href="eg/category/news/en" >Example</a>

elseif($_COOKIE['lang'] == 'de') :

<a href="eg/05/11/2018/de" >Example</a>
<a href="eg/tags/computer/de" >Example</a>

endif;
Share Improve this question edited Nov 7, 2018 at 22:29 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Nov 7, 2018 at 21:39 l6lsl6ls 3311 gold badge3 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

All of the API functions that generate links have filters to let you alter the output. Here's a quick example that covers most of them:

function wpd_endpoint_links( $url ){
    if( isset( $_COOKIE['lang'] ) ){
        $url = $url . $_COOKIE['lang'] . '/';
    }
    return $url;
}
add_filter( 'post_link', 'wpd_endpoint_links' );
add_filter( 'page_link', 'wpd_endpoint_links' );
add_filter( 'post_type_link', 'wpd_endpoint_links' );
add_filter( 'attachment_link', 'wpd_endpoint_links' );
add_filter( 'term_link', 'wpd_endpoint_links' );
add_filter( 'author_link', 'wpd_endpoint_links' );
add_filter( 'post_type_archive_link', 'wpd_endpoint_links' );
add_filter( 'day_link', 'wpd_endpoint_links' );
add_filter( 'month_link', 'wpd_endpoint_links' );
add_filter( 'year_link', 'wpd_endpoint_links' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749198276a317188.html

最新回复(0)