0

So I have a Pod set up as a shortcode where it outputs the links for files a user uploads. Here is the following shortcode I made (imagine my shortcode being [getShortCode field="upload_files"]:

function pods_current_user($atts) {
   extract(shortcode_atts(array(
      'currUser' => get_current_user_id(),
   ), $atts)); 
   extract(shortcode_atts(array(
    'field' => "display_name",
 ), $atts)); 
    $message = do_shortcode('[pods name="user" where="ID = '.$currUser.'"] {@'.$field.'}[/pods]');
    return $message;
 }
add_shortcode('getShortCode', 'pods_current_user'); 

At the moment what it outputs is a set of links to the uploaded files, but as a plain text, not hyperlinked. Something like the following:

http://secureservercdn.net/196.71.233.106/7ho.fa0.myftpupload.com/wp-content/uploads/2019/11/Screnshot-5.png?time=1575937207 http://secureservercdn.net/196.71.233.106/7ho.fa0.myftpupload.com/wp-content/uploads/2069/11/Geting-started-with-OneDrive-1.pdf?time=1675037207

(without the hyperlinks). Is there a way to be able to separate the links from my one variable into a scalable array, right in the function for my shortcode? Like maybe detect where the http is and separate the links like that? Thanks.

  • if you just want to make URL clickable, try the function make_clickable – Kaperto Nov 29 '19 at 23:05
  • It was that easy, I'm surprised. Thank you so much. – Ali Mosallaei Nov 29 '19 at 23:32
2

I would look into this question here https://stackoverflow.com/questions/36564293/extract-urls-from-a-string-using-php

You could use the above link detection to do something like this in your shortcode:

<?php
function pods_current_user($atts) {
   extract(shortcode_atts(array(
      'currUser' => get_current_user_id(),
   ), $atts)); 
   extract(shortcode_atts(array(
    'field' => "display_name",
 ), $atts)); 


    $message = do_shortcode('[pods name="user" where="ID = '.$currUser.'"] {@'.$field.'}[/pods]');

    preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $message, $match);

    // Do something with your links via the $match array.

    return $message;
 }
add_shortcode('getShortCode', 'pods_current_user'); 

  • 1
    Yes thank you. This definitely helps me with renaming each of the links with a specific name. make_clickable() also works for me. So thank you! – Ali Mosallaei Nov 29 '19 at 23:33
  • Glad that helped. I wasn't aware of the make_clickable function - thanks for that! – Tom Nov 30 '19 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.