WordPress.org

Ready to get started?Download WordPress

Forums

Custom fuction to modify <!--more--> excerpts (5 posts)

  1. gwinnie
    Member
    Posted 4 years ago #

    Greetings, hive mind! :-)

    I run a photoblog with a bit of text and an image in the post excerpt, which is separated from the rest by the <!--more--> tag. The post title and read more box link to the full post permalink, but not the image, which links to a larger size of itself.

    I would like the image to link to large itself only in the full post, not the post excerpt, where it should link to the post permalink as well. So I want to register a custom function as a filter, for the post excerpt that is returned as the part before <!--more-->

    Unfortunately, the only solutions I found were for manual excerpts, and I was unable to find a filter name (http://codex.wordpress.org/Plugin_API/Filter_Reference) that would identify this part of the post, so that I can use regexp to strip the link and add a new one around each img tag.

    Please advise!

    --gw

  2. Mohammad Jangda
    Member
    Posted 4 years ago #

    Give this a shot:

    function switch_img_link( $content ) {
        if( !is_single() ) {
            // We're not viewing a post, so switch the link to a permalink
        } else {
            // We're viewing the post, so do nothing
        }
        return $content;
    }
    add_filter('the_content', 'switch_img_link');
  3. gwinnie
    Member
    Posted 4 years ago #

    This is perfect, thanks! :-D 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;
    }
  4. gwinnie
    Member
    Posted 4 years ago #

    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.

  5. gwinnie
    Member
    Posted 4 years ago #

    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. :-)

Topic Closed

This topic has been closed to new replies.

About this Topic