]+)>/i'; // Regex: Find tags WITHOUT a title attribute. $title = false; if ( function_exists( 'is_main_query' ) && ! is_main_query() && isset( $post ) && isset( $post->post_title ) ) { // We are in a secondary loop. global $post; $title = $post->title; if( $title ) { $replacement = ''; $content = preg_replace( $pattern, $replacement, $content ); return $content; } } else{ // We aren't in a loop. preg_match_all( $pattern, $content, $matches ); if( $matches && is_array( $matches[0] ) ) { foreach( $matches[0] as $link ) { if( false !== strpos( $link, ' href="' ) ) { $arr = explode( ' href="', $link ); $url = rtrim( $arr[1], '">' ); $post_id = url_to_postid( $url ); if( $post_id ) { $title = get_the_title( $post_id ); if( $title ) { $new_link = str_replace( ' href="', ' title="' . esc_attr( $title ) . '" href="', $link ); $content = str_replace( $link, $new_link, $content ); } } } } } } return $content; } if( ! is_admin() ) { // Run only on the frontend. add_filter( 'the_content', 'eos_add_missing_link_titles_to_links', 9999999999999 ); // Filter post content to automatically add title attributes to links for better accessibility and SEO. }