Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter gwinnie

    (@gwinnie)

    Amazing, sir! This does the trick. Wow, it would take me ages to find out on my own.

    Thank you very much indeed. ??

    Thread Starter gwinnie

    (@gwinnie)

    OK, so I have the final function right here. ??

    I got rid of the deprecated functions, and it all seemed a little easier from there on. I’m more used to PERL kind of regexp. ?? So here:

    function img_permalink($content)
    {
            global $post;
            if(!is_single()) {
                    $content = preg_replace('/<a [^\>]*><img ([^\>]*)><\/a>/im', '<img $1>', $content);
                    $content = preg_replace('/(<img [^>]*>)/im', '<a href="' . get_permalink() . '" rel="bookmark" class="imagelink" title="Link to ' . $post->post_title . '">$1</a>', $content);
            } else {
            }
    
            return $content;
    }

    And thanks to this approach, it should be easily possible to merge these two into a single regexp. I’ll just keep it this way for easier readability and extensibility. Hope this helps someone a bit. ??

    Thread Starter gwinnie

    (@gwinnie)

    Actually, the function above has two problems. When removing hyperlinks, it leaves the closing tag, ruining validity of the document.

    Easily fixable by adding this simple substitution:
    $content = eregi_replace('(</a>)', '', $content);

    And another one, that it also strips the links to the post from the “read more” text. So I’ve decided to only make it remove hyperlinks that lead to an image. Fixed by replacing the first command by:

    $content = eregi_replace('(<a [^>]*.jpg[^>]*>)', '', $content);

    This, however, creates another problem, because if all the tags are removed, these won’t be closed. ?? Bah, I’m such a PHP newbie, is it possible to use some XML libs for this? I’ll post a solution when I find out.

    Thread Starter gwinnie

    (@gwinnie)

    This is perfect, thanks! ?? Such a workaround didn’t occur to me, mostly cause I wasn’t aware of the is_single() function…

    So for anyone who’d like to do the same, the whole function now looks like this:

    function img_permalink($content)
    {
            global $post;
            if( !is_single() ) {
                    // first it removes existing hyperlink that leads to the image itself
                    $content = eregi_replace('(<a [^>]*>)', '', $content);
                    // then it wraps every 'img' tag in a hyperlink to the post permalink (stolen from some plugin)
                    $content = eregi_replace('(<img [^>]*>)', '<a href="' . get_permalink() . '" rel="bookmark" class="imagelink" title="Link to ' . $post->post_title . '">' . '\1</a>', $content);
            } else {
                    // leave it alone
            }
    
            return $content;
    }
Viewing 4 replies - 1 through 4 (of 4 total)