0

I have a shortcode ( outputURL ) that is used to output a value into a URL. The value is retrieved from a shortcode ( valueURL ). The valueURL output is text wrapped in a div:

<div class="">textForURL</div>

I need only the textForURL value, excluding the div tags. Thus far, I have only been able to output the div tags and text but not the clean version of just text.

Here is my shortcode function:

function ouputURL(){

$content = '[valueURL id="5"]';
$clean = preg_replace("#<div.*?>.*?</div>#", "", $content);
echo do_shortcode( $clean );
}

add_shortcode('output', 'ouputURL');

I researched and was able to put this together but I am unsure as to whether I am heading in the right direction of executing it correctly. Any help to achieve my goal will be appreciated.

EDIT:

This is to further clarify my problem.

  1. I am using a WordPress plugin which manages all embedded video URLs within a post;

  2. As a feature, the plugin allows for a single video id ( i.e. vid=X23dw34 ) to be added through it's admin page which is viewed in a playlist prior to viewing the primary video;

  3. I want to use an ad rotator plugin to manage multiple video id's and serve a single video id based upon the ad serving parameters and called by a shortcode ( i.e. [valueURL id="5"] );

  4. The ad rotator plugin wraps a DIV around the video id:

<div class="">X23dw34</div>

  1. Using my function, I have been able to modify the plugin which manages the embedded video URLs to populate the video id including the div wrapper retrieved by the ad rotator shortcode. Unfortunately, the output result is:

"...video.com/embed/<div class="">X23dw34</div>..."

  1. My trouble has been to parse out the DIVs and only populate the video id to produce this:

"...video.com/embed/X23dw34..."

I am not sure if my limited understanding of shortcode and parsing is the issue or if my ultimate objective can not be handled by a simple function.

Please let me know if you require further information.

EDIT 2:

Here is the entire iframe, I removed extra tabs that to format it correctly:

<iframe width="100%" height="459" src="http://www.youtube.com/embed/oiE5qs8opqo?rel=0&autoplay=0&showinfo=0&controls=0&cc_load_policy=0&modestbranding=1&iv_load_policy=3&wmode=transparent&version=3&autohide=1" frameborder="0" allowfullscreen="true" allownetworking="internal"></iframe>

8
  • I can't understand what do you want to achieve: could you please add an example? Commented Apr 16, 2015 at 17:59
  • I added further details. Hopefully I was able to clarify my main issue. Thank you. Commented Apr 17, 2015 at 0:15
  • This worked perfectly. Thank you! I was able to modify it a bit for other use cases as well. I did hit one road block, one use case calls for parsing a YouTube iframe for the 'src' and only retrieving the url up to the '?'. I came up with this: /src="([^"]+)"/ but I feel like I am missing something. Thanks in advance. Commented Apr 17, 2015 at 22:08
  • I'm glad it worked. could you give me an example of an iframe tag you want to parse? I've just tried your regexp and i seems to work fine Commented Apr 17, 2015 at 22:27
  • Here is the entire iframe, not sure if the formatting is an issue: <iframe width="100%" height="459" src="youtube.com/embed/oiE5qs8opqo? rel=0 &amp;autoplay=0 &amp;showinfo=0 &amp;controls=0 &amp;cc_load_policy=0 &amp;modestbranding=1 &amp;iv_load_policy=3 &amp;wmode=transparent &amp;version=3 &amp;autohide=1 " frameborder="0" allowfullscreen="true" allownetworking="internal"></iframe> Commented Apr 17, 2015 at 23:07

1 Answer 1

0

I didn't fully understand but this should hopefully put you on track:

add_shortcode('output', 'ouputURL');
function ouputURL ( $atts ) {
    if(!isset($atts['id']))
        return;
    $valueURL = do_shortcode("[valueURL id={$atts['id']}]");
    preg_match("#<div.*?>(.*?)</div>#", $valueURL, $matches);
    if(!isset($matches[1]))
        return;
    return $matches[1]; // the ID
}

So if inside the content of a post/page you put [output id=5] it will retrieve the result of [valueURL id=5], find the content of the div tag and return it to be printed on the page.

You can modify it to return something more than just the ID.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.