Support Forum

Link formatting when upgrading 4.5 to 5.1.3

PK pkthree
pkthree
Member

I’m working on upgrading from 4.5 to 5.1.3.

It looks like links that were auto shortened and now formatted like this…

<a href=”http://www.theglobeandmail.com/globe-investor/personal-finance/problem-with-post-edit-buttonigher-fees-borrowing-costs-welcome-to-booming-big-six-banking/article4521956/” rel=”nofollow” target=”_blank”>http://www.theglobeandmail.com…..le4521956/</a>

… are not properly shortened anymore. They instead show up as just the part before the periods and thus the actual link is broken. Would installing a forum plugin (such as the TinyMCE one) fix the issue or is it an inherent issue with new auto-shortening logic?

23 Answers

New Answer

PK pkthree
pkthree
Member

In fact, the above post is a perfect illustration of the issue, as is the test below :)

Testing by editing the HTML source: http://www.theglobeandmail.com/globe-investor/personal-finance/problem-with-post-edit-buttonigher-fees-borrowing-costs-welcome-to-booming-big-six-banking/article4521956/

MP Mr Papa
Mr Papa
Member

It seems somewhere between 4.5.1 and 5.x, we made a change in the url shortening… earlier, it incorrectly was saving shortened urls in the database vs shortening them on display… we corrected that in 5.0 but does have some knock off on earlier shortenings due to a bug in wp where the make clickabled function chokes on a dot/period in a url anchor text and thus dorks it up… We are trying to find another make clickable function besides that wp one that works properly, but have not found one yet that does not mess up elsewhere… a catch 22 situation at the moment…

but new posts wont have this problem…

PK pkthree
pkthree
Member

Here’s a quick script to clean up old style links! It could probably use Simple:Press API functions for fetching and updating posts (speaking of which, is there documentation for manipulating the various content elemenest?).

<?php

/*
    For migrating from Simple:Press (WordPress forum plugin) 4.5 to 5.1.3 and cleaning up link shortening
    In 4.5, links were shortened on save
    In 5.1.3, links are shortened on display

    Run this script in the root of your WordPress directory: php clean_up_links.php

    Source: https://simple-press.com/support-forum/sp5-general-topics/link-formatting-when-upgrading-4-5-to-5-1-3
*/

include ‘wp-load.php’;

$postCount = $wpdb->get_var( ‘SELECT COUNT(*) FROM wp_sfposts’ );
$limit = 100;

print “n Total post count: $postCount”;
print “n Looping through $limit at a time”;
print “n”;

for( $offset = 0; $offset < $postCount; $offset += $limit )
{
    $posts = $wpdb->get_results( “SELECT post_id, post_content FROM wp_sfposts LIMIT $limit, $offset ORDER BY post_id ASC” );
    foreach( $posts as $post )
    {
        /*
            Matches would be:
            0 = The entire string
            1 = The link in the <a> tag
            2 = The rest of the parameters in the <a> tag (nofollow, target, etc.)
            3 = The part after the 5 periods

            So then we want to replace 0 with 1 for each result

            We are assuming that the links have 5 consecutive periods
        */

        $postContent = $post->post_content;
        $postID = $post->post_id;

        preg_match_all( “/<a href=”(.*?)”(.*?)…..(.*?)</a>/is”, $postContent, $linkMatches );

        if( !empty( $linkMatches[0] ) )
        {
            foreach( $linkMatches[0] as $index => $stringMatch )
            {
                $postContent = str_replace( $stringMatch, $linkMatches[1][$index], $postContent );
                $wpdb->query( $wpdb->prepare( “UPDATE wp_sfposts SET post_content = %s WHERE post_id = %d LIMIT 1;”, $postContent, $postID ) );
            }
        }
    }

    // Basic feedback to the user
    print ‘+’;
}

?>

MP Mr Papa
Mr Papa
Member

hey Peter, welcome back!

wow, going to have to take a closer look at that when I get a chance…  might be something we want to including in the upgrade script from 4.5…

sorry, what do you mean by content elements?  the display template functions?  have you checked out the codex?  http://codex.simple-press.com

its far from complete but we continue to populate it with more data as we go… sorry, just a limit to what the 3 of us can do! ;)

PK pkthree
pkthree
Member

Regarding “manipulating various content elements”, I mean code like:

sp_update_post( $postID, $postContent );

… so that we don’t have to directly write SQL.

I think there was code like that in 4.5.  It might be obvious in 5.1.3, although I don’t see it in the codex.  Then again, I might just need to get familiar with the “new” version :)

YS Yellow Swordfish
Yellow Swordfish
Member

As Steve said above we are trying hard to get the Codex API pages documented but it is a time thing with such a small team. In the meantime we suggest to users that if they need some API clarification on any point, they ask here and we will endeavour to give detailed explanations, advice and pointers and help walk them through things.

There just are not enough hours in a day!

PK pkthree
pkthree
Member

I can’t edit my original code post, so here’s a small correction: the database query should show:

LIMIT $limit, $offset

instead of:

LIMIT $offset, $limit

YS Yellow Swordfish
Yellow Swordfish
Member

Well maybe I am missing something this early in the morning! But your SQL statement above looks right to me…

PK pkthree
pkthree
Member

Oops, I flipped the correction :)

Should be:

LIMIT $offset, $limit

instead of:

LIMIT $limit, $offset

YS Yellow Swordfish
Yellow Swordfish
Member

Ooooh. So you did. And I missed that too!

TR tracychilders
tracychilders
Member

Hi Guys,

We’re also having this issue when checking old posts. Is it ok if we can use @peter script to automate this? Old links from 4.5 version are broken and its a lot.

Thanks!

MP Mr Papa
Mr Papa
Member

Actually, we would appreciate it if you could try it out. Please note, while Peter has contributed good code in the past, we have not had a chance to test it out ourselves. There is no reason to believe there will be any issue but would recommend a db backup before starting.

Any feedback would be great. We do intend to test and include Peters code in our upgrade script this week for our 5.1.4 release next week.

TR tracychilders
tracychilders
Member

Ok gotcha. btw may I know what class or php file handles the rewrite of content that has a links? I’ve check one post to our database and it’s seems the link is correct.

database post content

<a href=”http://longlinkhere.com” target=”_blank”>http://longlinkhere…..xxxxx.zip</a>

but when viewing the post the link becomes 

<p>

<a href=”http://longlinkhere.com” target=”_blank”></a>

<a href=”http://longlinkhere.com” rel=”nofollow” target=”_blank”>http://longlinkhere</a>…..xxxxx.zip

</p>

Thanks!

YS Yellow Swordfish
Yellow Swordfish
Member

The problem is actually in the WP core routine make_clickable(). We have searched and tried numerous third party algorithms that purposrt to do the same thing as make_clickable() and they all seem to have the same problem whenever there is a full point in the link.

we are hoping, of course, that the WP guys will fix the bug so it just goes away…

By the way – if you do try Peters code note the SQL error he points out later in the thread.