File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -220,3 +220,42 @@ function osi_the_page_dates() {
220220 }
221221 }
222222}
223+
224+ /**
225+ * Force external links to open in new tab.
226+ *
227+ * @param string $content The content to process.
228+ *
229+ * @return string The processed content.
230+ */
231+ function osi_the_content ( $ content ) {
232+ if ( ! em_is_event_rsvpable () ) {
233+ // Instantiate the processor.
234+ $ processor = new \WP_HTML_Tag_Processor ( $ content );
235+
236+ // Get the domain of the site without scheme.
237+ $ site_domain = wp_parse_url ( site_url (), PHP_URL_HOST );
238+
239+ // Loop through all the A tags and parse href to see if it's an external link.
240+ while ( $ processor ->next_tag ( 'A ' ) ) {
241+ $ href = $ processor ->get_attribute ( 'href ' );
242+ $ root_domain = wp_parse_url ( $ href , PHP_URL_HOST );
243+
244+ // If root domain is null, it's an internal link (no host), skip.
245+ if ( null === $ root_domain ) {
246+ continue ;
247+ }
248+
249+ // If the root domain is not the same as the site domain, it's an external link.
250+ if ( $ root_domain !== $ site_domain ) {
251+ $ processor ->set_attribute ( 'target ' , '_blank ' );
252+ }
253+ }
254+
255+ return $ processor ->get_updated_html ();
256+ }
257+
258+ return $ content ;
259+ }
260+
261+ add_filter ( 'the_content ' , 'osi_the_content ' );
You can’t perform that action at this time.
0 commit comments