I am not sure if there’s a native function for this, but a little regex might help the case:
function open_links_in_new_tab($content){
$pattern = '/<a(.*?)?href=[\'"]?[\'"]?(.*?)?>/i';
$content = preg_replace_callback($pattern, function($m){
$tpl = array_shift($m);
$hrf = isset($m[1]) ? $m[1] : null;
if ( preg_match('/target=[\'"]?(.*?)[\'"]?/i', $tpl) ) {
return $tpl;
}
if ( trim($hrf) && 0 === strpos($hrf, '#') ) {
return $tpl; // anchor links
}
return preg_replace_callback('/href=http://wordpress.stackexchange.com/i', function($m2){
return sprintf('target="_blank" %s', array_shift($m2));
}, $tpl);
}, $content);
return $content;
}
add_filter('the_content', 'open_links_in_new_tab', 999);
The patterns might need a little improvements. Hope that helps!
Related Posts:
- Default or Preset Content for Custom Post Types
- How would go about if I just want a temporary function?
- str_replace function in theme
- How can I modify all existing tags while keeping the urls themselves?
- Insert image in WordPress with HTML5 tag and caption function
- Can’t filter wp_get_attachment_link
- login_headertitle is deprecated since version 5.2.0
- The_content and Preg_replace in loaded Iframe [closed]
- Problem with images URL after filter applying
- Save_post – Warning: Cannot modify header information
- How Can I Concatenate A String With One Of My Custom Field Value Before Saving The Post?
- How to make an If Else on Excerpt Filter
- Shortcode to eliminate and replace with
- How to Add A Rel Filter to All Links?
- child_of not working while searching
- remove empty paragraphs from the_content?
- Add multiple custom fields to the general settings page
- Set JPEG compression for specific custom image sizes
- Define custom Page Template without its own .php file
- Is it possible to remove the “standard” post format?
- Issue with get_theme_mod returning a blank value instead of the saved value
- How to remove row-actions from pages table?
- Auto delete WordPress images/thumbnails (all sizes) and featured after X days/hours, or similar?
- Convert hyphen to underscore in permalinks
- How to fix a theme with page.php Default Template that accidentally deleted?
- Override parent theme function that is not hooked or in the functions.php file
- Programmatically Add Font-Awesome Icons to Category Widget
- Change comments form title on a page by page basis
- Remove bulk actions based on user role or capabilities
- How do I change parameters without changing the core
- Problem with extract() with custom shortcode
- Removing title from page
- add sidebar area to header of child theme
- Adding body class when post contains a specific shortcode
- How to make unique add_filter to the_content of specific page template files – so each template gets its own addition
- How to get Custom Post ID by adding filter to child theme’s function
- Theme-Config tab in admin Pannel?
- Creating loop within functions.php
- Limit filter upgrader_post_install to a single plugin
- How to update feed only 2-3 times a week (for Feedburner email)?
- Manipulate Output of wp_list_something: select menu instead of li’s
- Pass Shortcode Attribute to footer Script
- PHP files included in functions.php don’t seem to work
- How Can I Expand the WordPress Customize window without any Plugin?
- Set quantity of woocommerce product on page visit [closed]
- Can’t check if a post has thumbnail adding filter to get_post_metadata()
- Adding HTML to the end of every post with the Block Editor
- Automatically wrap multiple images in div
- Ajax not working properly
- I want to allow the use of a data-flw attribute in links in comments
- Run wp_kses_decode_entities on atom feed?
- Filtering out the #more anchor link that gets produced by
- append to existing parent theme function
- different style sheet for just one page template
- Override the WordPress core function wp_referer_field
- How to add custom JavaScript in functions?
- How to add_filter html template to middle of content
- Redefine function arguments before rendering
- javascript and css links not working on sever
- Combine embed_oembed_html and oembed_result
- Filter to wp_list_authors
- Add attribute only to first image of every post via functions.php
- Different favicon on different pages
- Add Adsense code between job listings – wp job manager plugin
- Replace header image on all other pages but home – URL issue
- Insert a field with PREG_REPLACE – strange behaviour
- How to echo Widget Title in Custom Frontend-Template Box
- how to display a page before the home page
- loginout function customization
- implementing a centralized content “show-do-not-show” toggle?
- How would I make a function to dynamically determine if user is logged to change navigation menu text?
- How to display this meta data (an array) in form of a function (created with a custom write panel)?
- WordPress – using sessions?
- how can I add filter in specfic field in my website?
- How to remove font awesome from loading in wp theme
- Customizing the wp_video_shortcode output with add_filter
- How to add HTML into error message
- Best practice to call a function from a button-link?
- Removing comment lines from all enqueued js files using a filter function
- Extend WordPress Audio Widget to accept HLS (.m3u8) URLs
- function replace_text for entire page
- Changing author links on homepage to buddypress profiles – without affecting ability to link to author archive throughout the site
- add product thumbnail to checkout page only and include variation name
- WordPress Ajaxifying not working properly
- WordPress function with attribute pass to template
- Will dequeueing in child theme functions.php file prevent Google Fonts from loading?
- Graphic before title – Specific Category
- Which method is more correct for removing WooCommerce Extensions menu item?
- Help! Need a different logo on my main page from my other pages
- Display page number on custom page title function
- How to replace words in my posts and saved new words
- Why might wptexturize have become re-enabled?
- functions.php filters not applied in AJAX call
- Add functionality to post editor
- Settings in functions.php used by a plugin
- Can’t change excerpt length and more tag
- Why does modifying my functions.php file always result in a server error
- Remove snippets of JS from core
- Vimeo video play button color function
- Warning: call_user_func_array() expects parameter 1 to be a valid callback