Can A Shortcode Get HTML/Text From Content And Return It Twice With Added HTML?

I’m trying to cut down on some work for myself and I was hoping someone might know if this is possible.

These are images setup in a slider that shows a text overlay on the image on the next slide in a multi-page post.

Here’s what I’m trying to do. Take this:

<h2>Image Title</h2>
<img src="https://wordpress.stackexchange.com/questions/227368/http">
text for slide

And output them like this:

<div class="singe_slide">
<h2>Image Title</h2>
<img src="https://wordpress.stackexchange.com/questions/227368/http">
</div>
<!--nextpage-->
<div class="singe_slide">
<h2>Image Title</h2>  //Same Image Next Page
<img src="https://wordpress.stackexchange.com/questions/227368/http">
<div class="slide_caption"><div class="captExt">
text for slide
</div></div>
<!--nextpage-->

This is what I’m currently using for my shortcodes but it takes just as long to just manually add the div’s and it’s very messy. It looks like this:

[Slide]<h2>Image Title</h2>
<img src="https://wordpress.stackexchange.com/questions/227368/http">[/Slide]
<!--nextpage-->
[Slide]<h2>Image Title</h2> //Same Image Next Page
<img src="https://wordpress.stackexchange.com/questions/227368/http">
[SlideCap]text for slide[/SlideCap][/Slide]
<!--nextpage-->

And these are my 2 shortcodes:

function Slide($atts, $content = null) {
    $content = wpautop(trim($content));
    return '<div class="singe_slide">' . do_shortcode($content) . '</div>';
}
add_shortcode('Slide', 'Slide');

function SlideCap($atts, $content = null) {
    $content = wpautop(trim($content));
   return '<div class="slide_caption"><div class="captExt">' . do_shortcode($content) . '</div></div>';
}
add_shortcode('SlideCap', 'SlideCap');

I’m open to suggestions on an easier way to do this. This is my very first shortcode so I’m new to it.

Is it possible to get something between shortcodes as a variable?
[shortcode]This Part Here[/shortcode]

1 Answer
1

Okay, I think I’ve figured out what you’re actually asking. You’re asking what the variable for the bit between the shortcodes. If that is all you are asking, then the answer is the parameter variable $content. You are already processing it in your code.

Also, in your example, you seem to have a third shortcode, that you aren’t processing. Is that deliberate? It doesn’t look like you really want to produce final html with that in, so this solution assumes you don’t want that.

I’m going to assume that what you want is a simple to use shortcode format such as:

[slide=ImageTitle][slideimg]http://some.domain/slide.jpg[/slideimg][slidecap]This is the caption.[/slidecap][/slide]

Which you can then process simply by adjusting your functions like so:

function shortcode_slide($atts, $content = null) {
    if(empty($atts)) {
        $img_title="";
    } else {
        // [slide=Title]...[/slide]
        // [slide="Multi word title"]...[/slide]
        $atts = $this->attributefix( $atts );
        $img_title="<h2>".trim(array_shift($atts),'="').'</h2>'; //Remove quotes and equals.
    }

    $content = wpautop(trim($content));
    return '<div class="singe_slide">'.$img_title. do_shortcode($content) . '</div>';
}
add_shortcode('slide', 'shortcode_slide');

function shortcode_slidecap($atts, $content = null) {
    $content = wpautop(trim($content));
    return '<div class="slide_caption"><div class="captExt">' . do_shortcode($content) . '</div></div>';
}
add_shortcode('slidecap', 'shortcode_slidecap');

function shortcode_slideimg($atts, $content = null) {
    $content = wpautop(trim($content));
    return '<img src="'.$content.'" />';
}
add_shortcode('slideimg', 'shortcode_slideimg');

This should produce working html.

Leave a Comment