Question

I'm having a problem that doesn't make any sense. Why is my html being parsed incorrectly? Simply changing the way it's assigned in PHP seems to fix it, is it a PHP/Browser bug?

When my shortcode looks like this:

[gallerybl image='http://dev.netcoding.net/aristocrat-v2/wp-content/uploads/2013/06/Mirage-Yellow-Birch-Montana11.jpg' href="#" title='Living Space']Epediti solore, solorep rectota il eat receriandia seque non pa ititissi solestenit omnimeture, sus sonata corpore rcimoll enissequi custiam.[/gallerybl]

WordPress (using everything as default, I turned every filter off), calls the below function and $image1 is parsed incorrectly as (notice the first quote in the a tag):

<a href="#' title='Living Space'><span class='gallery_bl_img'><img src='http://dev.netcoding.net/aristocrat-v2/wp-content/uploads/2013/06/Mirage-Yellow-Birch-Montana11.jpg"  alt='Living Space' /></span></a>

Here is the function being called:

function get_gallery_block( $atts, $content = "" ) {

    extract( shortcode_atts( array(
        'image' => '',
        'href' => '',
        'title' => ''
    ), $atts ) );

    $image  = "<span class='gallery_bl_img'><img src='{$image}' alt='{$title}' /></span>";
    $image1 = empty( $href )    ? $image : "<a href='{$href}' title='{$title}'>{$image}</a>";
    $image2 = empty( $href )    ? $image : '<a href="' . $href . '" title="' . $title . '">' . $image . "</a>";
    $htitle = empty( $title )   ? ""     : "<span class='gallery_bl_title'>{$title}</span>";
    $clink  = empty( $content ) ? ""     : ( empty( $href ) ? "" : "<a href='#' title='{$title}'>READ MORE</a>" );
    $desc   = empty( $content ) ? ""     : "<span class='gallery_bl_content'>{$content} {$clink}</span>";

    $format = ( $htitle . $image1 . $desc );
    return "<div class='gallery_bl_container'>{$format}</div>";

}
add_shortcode( 'gallerybl', 'get_gallery_block' );

Browser: Google Chrome 32.0.1700.76 m, if that helps.

PHP Version: 5.4.24 (CGI/FastCGI)

Was it helpful?

Solution

The whole thing needs a clean-up (mostly for readability). Try this...

add_shortcode('gallerybl', function($atts, $content = null) {
    extract(shortcode_atts(array(
        'image' => null,
        'href'  => null,
        'title' => null
    ), $atts));

    // escape all atts for HTML
    $image = htmlspecialchars($image);
    $title = htmlspecialchars($title);
    $href = htmlspecialchars($href);

    $imgMarkup = sprintf('<span class="gallery_bl_img"><img src="%s" alt="%s"></span>',
        $image, $title);
    if ($href) {
        $imgMarkup = sprintf('<a href="%s" title="%s">%s</a>',
            $href, $title, $imgMarkup);
    }

    ob_start();
    ?>
    <div class="gallery_bl_container">
        <?php if ($title) : ?>
            <span class="gallery_bl_title"><?= $title ?></span>
        <?php endif ?>
        <?= $imgMarkup ?>
        <?php if ($content) : ?>
            <span class="gallery_bl_content">
                <?= do_shortcode($content) ?>
                <?php if ($href) : ?>
                    <a href="#" title="<?= $title ?>">READ MORE</a>
                <?php endif ?>
            </span>
        <?php endif ?>
    </div>
    <?php
    return ob_get_clean();
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top