• Resolved Graicifyd

    (@graicifyd)


    Hello,

    How can I make an automatic list of all outgoing links in each post at the bottom of the post? In other words, I want to list all the hyperlinks I have used in a post at the bottom of the post.

    Thank you for your support.

Viewing 6 replies - 1 through 6 (of 6 total)
  • I don’t know of a plugin that makes this possible, but I have designed a little code here that solves your question:

    function filter_the_content_in_the_main_loop( $content ) {
    	// Check if we're inside the main loop in a single Post.
    	if ( is_singular() && in_the_loop() && is_main_query() ) {
    		$pattern = '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#';
    		//Perform the regular expression match to get all links from post content
    		preg_match_all( $pattern, $content, $matches );
            if( !empty($matches) ) {
                $urls = [];
                $list = '';
                foreach( $matches[0] as $url ) {
                    if( false === strpos($url, get_option('siteurl')) ) {
    	                $urls[ $url ] = $url;
                    }
                }
    	        foreach( $urls as $url ) {
                    $list .= '<li><a href="'.$url.'" target="_blank">'.$url.'</a></li>';
    	        }
    	        return $content . '<ul>'.$list.'</ul>';
            }
    	}
    	return $content;
    }
    add_filter( 'the_content', 'filter_the_content_in_the_main_loop', 9999 );

    You would have to add this to your child theme, your custom plugin or via code snippet plugin. Please note that this may require further adjustments in your environment, e.g. in terms of styling. I tested it with a WordPress standard theme and Gutenberg as editor.

    And please: as important as your theme is to you, you should not capitalise the title completely.

    Thread Starter Graicifyd

    (@graicifyd)

    Thank you so much for your response, I am sorry about the title.

    This worked perfectly, thanks a lot.

    Is it possible to have the anchor texts displayed by the side of the URLs as well?

    Updated code:

    function filter_the_content_in_the_main_loop( $content ) {
    	// Check if we're inside the main loop in a single Post.
    	if ( is_singular() && in_the_loop() && is_main_query() ) {
    		$pattern = '/<a\s+.*?href=[\"\']?([^\"\' >]*)[\"\']?[^>]*>(.*?)<\/a>/i';
    		//Perform the regular expression match to get all links from post content
    		preg_match_all( $pattern, $content, $matches );
            if( !empty($matches) ) {
                $urls = [];
                $list = '';
                foreach( $matches[1] as $i => $url ) {
                    if( false === strpos($url, get_option('siteurl')) ) {
    	                $urls[] = ['url' => $url, 'title' => $matches[2][$i]];
                    }
                }
    	        foreach( $urls as $url ) {
                    $list .= '<li><a href="'.$url['url'].'" target="_blank">'.$url['title'].'</a></li>';
    	        }
    	        return $content . '<ul>'.$list.'</ul>';
            }
    	}
    	return $content;
    }
    add_filter( 'the_content', 'filter_the_content_in_the_main_loop', 9999 );
    Thread Starter Graicifyd

    (@graicifyd)

    Thank you so much.

    Is there any chance I can output this as a shortcode, instead of after content?

    The first code is preferable, the second one picked some links from buttons.

    • This reply was modified 2 years, 2 months ago by Graicifyd.

    Try this:

    function filter_the_content_via_shortcode() {
        $content = get_the_content();
    	if ( is_singular() && in_the_loop() && is_main_query() ) {
    		$pattern = '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#';
    		preg_match_all( $pattern, $content, $matches );
    		if( !empty($matches) ) {
    			$urls = [];
    			$list = '';
    			foreach( $matches[0] as $url ) {
    				if( false === strpos($url, get_option('siteurl')) ) {
    					$urls[$url] = $url;
    				}
    			}
    			foreach( $urls as $url ) {
    				$list .= '<li><a href="'.$url.'" target="_blank">'.$url.'</a></li>';
    			}
    			return '<ul>'.$list.'</ul>';
    		}
    	}
        return '';
    }
    add_shortcode('linklist', 'filter_the_content_via_shortcode' );

    The shortcode is simply: [linklist]

    Thread Starter Graicifyd

    (@graicifyd)

    Perfect.

    You’re amazing.

    Thanks a million.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘OUTPUT A LIST OF ALL OUTGOING URLS’ is closed to new replies.