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
- I want to allow the use of a data-flw attribute in links in comments
- how can I just add to parent theme function(s) instead of redeclaring whole the function
- Adding an external HTML link to a post thumbnail
- Replace a menu with widget or a custom template file programmatically
- woocommerce change currency per category [closed]
- Move Custom Fields on General Settings Page to New Location
- Skeleton Child Theme Add Icon Bar to Header Flex Grid
- Run wp_kses_decode_entities on atom feed?
- Filtering out the #more anchor link that gets produced by
- Redirect to another page using contact form 7? [closed]
- append to existing parent theme function
- different style sheet for just one page template
- Add Element as a Filter to the_content
- Override the WordPress core function wp_referer_field
- Modify post filter to set custom number of posts per page and exclude child posts
- Make wp_link_pages() suitable for Twitter Bootstrap markup
- How can I add a filter for specific categories on functions.php?
- How to add custom JavaScript in functions?
- Setting a default text for excerpts of a particular category
- How to edit the Tags within the image file URLs?
- How would I go about replacing this function in my child theme located in inc/template-tags.php
- Condition OR for current user ID
- Retrieve a value from Yoast SEO to use to set a default twitter card image honoring overrides
- Get arguments from URL
- Can’t properly set the_title add_filter to show short_URL
- Add_filter when value is no variable?
- How to retrieve the current post’s generated featured image size?
- How to add_filter html template to middle of content
- (Woocommerce) Order by price when entering specific category
- Filter an WordPress Function in (general-template.php)
- Custom Behavior when Adding New Custom Post Type in Dashboard
- Where do I store custom functions that call custom page template files?
- Redefine function arguments before rendering
- Why There Is No “get content by ID” Function
- How to edit classes in body tag?
- Custom HTML markup
- Use a shortcode to display custom meta box contents
- javascript and css links not working on sever
- Filter nav menu items HTML tags and wrap inner text with span
- Wp-query Order By problem
- Possible to hook into Media Library preview File column and use a custom image?
- How can I add a class to a nav li depending on URL?
- Create page template via functions.php?
- Create custom function for hero image
- Randomizing wp_tag_cloud() in child theme
- Modify gform_other_choice_value for specific form and specific field in Gravity Forms
- Combine embed_oembed_html and oembed_result
- Using Static HTML instead of the `home_url()` WP function
- Set “woocommerce_is_purchasable” to false for specific “$product->is_stock_status”
- How to append classname to body tag if guest user
- How to display the_archive_title() and the_archive_description() – “weird” interaction
- Where do I go to edit this area? [closed]
- Filter to wp_list_authors
- How to add custom li item to wordpress menu
- Replace plugin function with a custom function?
- Add attribute only to first image of every post via functions.php
- Native gallery custom html output
- Different favicon on different pages
- How to edit/replace Parent functions.php function in Child Theme to add “Walker” class
- Functions Filter Question [closed]
- Is it possible to use add_filter in an included file in the child theme’s functions.php?
- Insert Content Before div#main from the functions.php File
- Add Adsense code between job listings – wp job manager plugin
- remove_action not working, even after changing priority [duplicate]
- remove/hide wp-editor
- two search forms on the same page
- Function to replace comment’s accented characters before posting
- How to convert raw url to hyperlink?
- 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
- Add a class to post if it has been recently updated
- New checkbox in custom widget isn’t saving data
- Linking Javascript in functions.php file
- how to display a page before the home page
- search form leads to 404
- loginout function customization
- how to add_filter to non hook function
- Function to check if custom page is used by page?
- Changing the text of Upload/Insert on Posts and Pages Screen
- 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?
- Any adverse effects of adding apply_filters to a function?
- Conditional custom menu?
- How to display this meta data (an array) in form of a function (created with a custom write panel)?