Can’t properly set the_title add_filter to show short_URL

There’s a second argument passed to the_title filter, which is the ID of the post the filter is currently operating on. This is important, as you’ve discovered, because the filter runs any time a title is output- in a menu item, widget, any secondary query, etc., so you need to check if that post ID matches the ID of the post you’re currently viewing.

Your other problem is a syntax issue where you set $aftertitle. You’ve got php open/close braces and an echo inside there where you should be using string concatenation, like the way you join $title . $aftertitle on the next line. Anyway, we can do that a bit differently.

So here’s a new version of your function. We can combine the single and post type checks into one, and I think it’s safe to assume that wp_get_shortlink exists in this context. We then use php sprintf to generate the string, this is just a personal preference.

function shorturl_after_title( $title, $post_id ) {
    if ( is_singular( 'post' ) && $post_id == get_queried_object_id() ) {
        $text = "%s<br><span class="post-shortlink">رابط مختصر:<input type="text" value="%s" onclick='this.focus(); this.select();' /></span>";           
        $title = sprintf( $text, $title, wp_get_shortlink( $post_id ) );
    }
    return $title;
}
add_filter( 'the_title', 'shorturl_after_title', 10, 2 );