Skip to content

Instantly share code, notes, and snippets.

@bugnumber9
Last active March 10, 2026 10:49
Show Gist options
  • Select an option

  • Save bugnumber9/c200468ee2ca68c1bad4ecb756f04efe to your computer and use it in GitHub Desktop.

Select an option

Save bugnumber9/c200468ee2ca68c1bad4ecb756f04efe to your computer and use it in GitHub Desktop.
Trim long URLs
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