Add tag inside all
  • with matching class name using PHP
  • Question

    This is for a WordPress site i am working with for practice. After 3 days i found a similar question which goes as close to what i need. WordPress, add a tag to all links

    the CODE:

    function wrap_anchor_text_with_span( $content ) {
    if ( ! is_admin() && preg_match( '~<a(.*?)>(.*?)</a>~', $content ) ) {
        $content = preg_replace_callback( '~<a(.*?)>(.*?)</a>~', '_add_span', $content );
    }
    return $content;}
    add_filter('the_content', 'wrap_anchor_text_with_span', 10);
    function _add_span( $matches ) {
    if ( ! ( $title = strip_tags( $matches[2] ) ) ) { // If we only have an image inside the anchor
        return '<a' . $matches[1] . '>' . $matches[2] . '</a>';
    } else {
        return '<a' . $matches[1] . '><span data-title="' . esc_attr( $title ) . '">' . $matches[2] . '</span></a>';
    }
    

    it would work i guess if i had to do the exact same thing BUT i need to change the code.

    SO it matches the li with class="item" and add <div> with its own children.

    FOR now i have this HTML –

    <li class="item">               
        <div class="image" ></div>
        <span class="title"></span>
        <div class="content" ></div>
     </li>
    

    AND i need to create it like this –

    <li class="item">               
        <div class="image" ></div>
        <span class="title"></span>
        <div class="content" ></div>
        <div class="mainbox" id="mainbox">
            <img src="" alt="" class="imgbox">
            <img src="" alt="" class="imgbox">
            <img src="" alt="" class="imgbox">
            <img src="" alt="" class="imgbox">
        </div>
    </li>
    

    please help fix the issue.

    0
    suryavansh 4 months 0 Answers 14 views 0

    Leave an answer