Remove anchor element around Wordpress images using a filter (or jquery)

I have an anchor element like this:

<a href="/link-to-image/" rel="attachment wp-att-7076"><img src="/uploads/img.jpg" alt="" title="" width="1268" height="377" class="alignnone size-full wp-image-7076" /></a>

(This is the standard Wordpress method for embedding uploaded images in a message.)

I want to remove the snapping around the image element, but save the image. I just want the image to be displayed without clicking.

This can be done either with a filter for the content of the message in Wordpress, or after loading the page using javascript. Filtering in Wordpress is preferable. I do not know how to make either of these two options.

+5
source share
2 answers

Go to the WP theme folder, edit "functions.php". Add the following code:

function remove_anchor($data)
{
    // the code for removing the anchor here

    // (not sure if you need help there, too).  

    // you will work on the $data string using DOM or regex
    // and then return it at the end


    return $data;
}

// then use WP filter/hook system like this:
add_filter('the_content', 'remove_anchor');

add_filter , , , remove_anchor.

jQuery, , , ( )

$(document).ready(function()
{
    $('#post a.some-class-name').click(function()
    {
        return false;
    }
});
+3

:

, .

.. /your _theme/ functions.php :

function remove_anchor($content) {
    // the code for removing the anchor here
    $content =
        preg_replace(
            array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}', '{</a>}'),
            array('<img',''),
            $content
        );
    return $content;
}

// then use WP filter/hook system like this:
add_filter('the_content', 'remove_anchor');
+4

All Articles