Skip to content

Instantly share code, notes, and snippets.

@iamcanadian1973
Created September 17, 2021 16:48
Show Gist options
  • Save iamcanadian1973/a092c45fdd54406251d5a45fc3ebbd09 to your computer and use it in GitHub Desktop.
Save iamcanadian1973/a092c45fdd54406251d5a45fc3ebbd09 to your computer and use it in GitHub Desktop.
WPML Filter URL's
<?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