Paramasivan
Paramasivan

Reputation: 791

Extract url from the post

I have blog post in wordpress for audio podcast like

[audio mp3="http://www.andrewbusch.com/wp-content/uploads/show_6195505.mp3"][/audio]
We talk the latest with forward guidance in the Federal Reserve with Wall Street Journal reporter, Jon Hilsenrath. 

I want to make a download link also for the above mp3 url.

How to extract the url from the above content?

I tried like the below but it is not working

<div class="banner-text">
<h1><a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a></h1>
<?php the_content(); ?>
<?php
$content = get_the_content( $more_link_text, $stripteaser );
preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $content, $match);
$match = $match[0];
?>
<div class="banner-download"><a href="<?php echo $match; ?>"><span class="displace">Download</span></a></div>
</div>

Any help ?

Upvotes: 1

Views: 157

Answers (1)

Hobo
Hobo

Reputation: 7611

Interesting question. My instinct would be to use as much of WordPress's built in shortcode functionality as possible to do the work. The get_post_galleries function seems to do something similar. So something like this seems to do what you want:

<?php
// Assumes $content is already set

preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER );

foreach ($matches as $match) {
    if ( 'audio' === $match[2] ) {
        $tags = shortcode_parse_atts( $match[3] );
        echo $tags['mp3'];
    }
}

You might need to refine it depending on how you want to handle it if there are multiple audio shortcodes.

Upvotes: 1

Related Questions