0

function getUrls($string)
{
    $regex = '/https?\:\/\/[^\" ]+/i';
    preg_match_all($regex, $string, $matches);
    return ($matches[0]);
}
add_shortcode( 'links_list', 'portfolio_shortcode' );
function portfolio_shortcode( $atts ) {
 ob_start();
 $the_query = new WP_Query('posts_per_page=-1');
 $count =1;
 echo '<ul class="link_list">';
while ($the_query->have_posts())
{
    $the_query->the_post();
    $_post_id = get_the_id();
    $_post_content = get_post_field( 'post_content', $_post_id);
     $urls = getUrls($_post_content);
         foreach($urls as $url)
    {
        //if (substr($url, 0, 24) == "http://stackoverflow.com")
            echo '<li>'.$count.'. <a href="'.$url .'">'.$url.'"</a> <a target="_blank" href="'.get_permalink($_post_id). '">Post Link</a></li>';
            $count++;
    }
 }
echo '</ul>';
wp_reset_postdata();
 $myvariable = ob_get_clean();
    return $myvariable;
}

Jagdish Sarma Asked question January 6, 2022
Add a Comment