Change link to other post to shortlink in the editor

Curently if we use the insert link dialog to insert a link to another post, WordPress always use the permalink of that post. The problem is when we change permalink structure, that link will be broken.

I’d like to ask if there is a way to change the link from permalink to shortlink like ?p=123 to make it work in any circumstance.

Read More

Thanks.

Related posts

Leave a Reply

1 comment

  1. The links in that dialog are made by wp_ajax_wp_link_ajax() (see wp-admin/includes/ajax-actions.php, there is no page on Codex or queryposts.com for that function).

    To change the links filter 'page_link', 'post_type_link', 'post_link' and maybe 'attachment_link' after check_ajax_referer() was called for the action 'internal-linking'.

    Okay, sounds a little bit complicated, but it is really easy. 🙂
    Plugin on GitHub: https://gist.github.com/3731739

    add_action( 'check_ajax_referer', 't5_temporary_internal_links', 10, 1 );
    
    /**
     * Turn permalinks into dynamic links.
     *
     * @param   string $action_or_link Action when called per 'check_ajax_referer',
     *                                later the permalink.
     * @param   object|integer $post
     * @wp-hook check_ajax_referer
     * @wp-hook page_link
     * @wp-hook attachment_link
     * @wp-hook post_type_link
     * @wp-hook post_link
     * @since   2012.09.16
     * @return  string
     */
    function t5_temporary_internal_links( $action_or_link, $post = 0 )
    {
        if ( 'check_ajax_referer' === current_filter()
            and 'internal-linking' === $action_or_link
        )
        {
            add_filter( 'page_link',       __FUNCTION__, 10, 2 );
            // You cannot search for attachments in this dialog,
            // but a plugin might have changed that, so …
            add_filter( 'attachment_link', __FUNCTION__, 10, 2 );
            add_filter( 'post_type_link',  __FUNCTION__, 10, 2 );
            add_filter( 'post_link',       __FUNCTION__, 10, 2 );
            return;
        }
    
        $id = is_object( $post ) ? $post->ID : $post;
        return home_url( "?p=$id" );
    }
    

    But … when you change permalinks you have to create redirects in a server configuration file anyway to redirect existing URLs. So I am not sure if this plugin is really needed.