WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

bellow is custom gallery code.

function custom_gallery_shortcode( $attr = array(), $content = '' )


        $attr['itemtag']        = "li";
        $attr['icontag']        = "";
        $attr['captiontag']     = "p";

        // Run the native gallery shortcode callback:    
        $html = gallery_shortcode( $attr );
        // Remove all tags except a, img,li, p
        $html = strip_tags( $html, '<a><img><li><p>' );
        // Some trivial replacements:
        $from = array(  

            "class='gallery-icon landscape'", 
            'a href=', 


        $to = array( 




            'a data-caption="" class="ilightbox" href=', 


        $html = str_replace( $from, $to, $html );

        // Remove width/height attributes:

        $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
        // Wrap the output in ul tags:

        $html = sprintf( '<ul class="gallery">%s</ul>', $html );

        return $html;


add_shortcode( 'gallery', 'custom_gallery_shortcode' );
add_filter( 'use_default_gallery_style', '__return_false' );

but now i want to add image caption inside a. look at my code 'a data-caption="" class="ilightbox" href=', there data-caption="image caption i want to add" now image caption coming outside a

like this way

  • <a data-caption="" class="ilightbox" href="http://xxxxxxx.com/Snack-bar-1024x682.jpg"><img src="http:xxxxxxxxxx.com/Snack-bar-1024x682-150x150.jpg" alt="testing caption"></a>
                    <p class="wp-caption-text gallery-caption">
                    testing caption
  • share|improve this question
    up vote 0 down vote accepted

    You can try this code to add the new attribute in the html code:

    $dom = new DOMDocument();
    foreach ($dom->getElementsByTagName('a') as $item) {
        $item->setAttribute('data-caption', 'This is an anchor tag');
        echo $dom->saveHTML();
    share|improve this answer

    Your Answer


    By posting your answer, you agree to the privacy policy and terms of service.

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