Last active
March 10, 2026 10:49
-
-
Save bugnumber9/c200468ee2ca68c1bad4ecb756f04efe to your computer and use it in GitHub Desktop.
Trim long URLs
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
| add_filter( 'the_content', function( $content ) { | |
| return preg_replace_callback ( | |
| '#https?://[^\s<]+#i', | |
| function( $matches ) { | |
| $url = $matches[0]; | |
| $display = preg_replace( '#https?://#', '', $url ); | |
| if( strlen( $display ) > 50 ) { | |
| $display = substr( $display, 0, 25 ) . '…' . substr( $display, -15 ); | |
| } | |
| return '<a href="' . $url . '" target="_blank" rel="noopener">' . $display . '</a>'; | |
| }, | |
| $content | |
| ); | |
| } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment