Created
September 17, 2021 16:48
-
-
Save iamcanadian1973/a092c45fdd54406251d5a45fc3ebbd09 to your computer and use it in GitHub Desktop.
WPML Filter URL's
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function filter_language_code( $content ) { | |
$new_content = ''; | |
$dom = new DOMDocument(); | |
$dom->encoding = 'utf-8'; | |
libxml_use_internal_errors(true); | |
$dom->loadHTML( $content ); | |
$anchors = $dom->getElementsByTagName( 'a' ); | |
foreach ( $anchors as $anchor ) { | |
$href = $anchor->getAttribute( 'href' ); | |
/* | |
if( 'XXX' ! = ICL_LANGUAGE_CODE ) { | |
continue; | |
} | |
*/ | |
/* | |
$find = 'de'; | |
$pos = strpos( $href, $find ); | |
if ($pos === false) { | |
continue; | |
} */ | |
// do what you need to do to th "href" | |
$href = str_replace( site_url(), home_url(), $href ); // this might work | |
$anchor->setAttribute( 'href', esc_url( $href ) ); | |
} | |
$new_content = $dom->saveHTML(); | |
return $new_content; | |
} | |
add_filter( 'the_content', 'filter_language_code', 999 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment