Horje

This is just a first idea: Check if it is the AJAX request.



if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) return $slug;

or:



if ( isset( $_POST['action'] ) && 'sample-permalink' === $_POST['action'] ) return $slug;

depending on the level of detail you need.

If you put this in the beginning of the function (maybe below the first test if ($slug) return $slug; then the function should just quit.

If you still want to filter that input as well, for the AJAX request, you need to retrieve it from $_POST['new_title'] instead of $_POST['post_title'] . That's the reason why it deletes your input via AJAX editing.


More Solution
Reffered: https://wordpress.org/