An easy-to-understand solution is
%! TEX program = pdflatex
\documentclass{ltxdoc}
\usepackage[T1]{fontenc} % pdflatex only!! in other engine omit this line or specify encoding TU
% in pdflatex without this line the vertical bar would not be in typewriter font
\begin{document}
\texttt{ab\textbar cd}
\end{document}
You will not be able to use |ab\textbar cd| here because it's verbatim and will print \textbar verbatim (nevertheless |ab\textbar cd| can be used inside another command's argument if it's a "real argument" but I don't recommend relying on this inconsistent behavior, would be confusing)
For the sake of overcomplicating things, this is a solution that allows you to escape the | by doubling it.
%! TEX program = pdflatex
\documentclass{ltxdoc}
\ExplSyntaxOn
\makeatletter
\char_set_catcode_active:N |
\AtBeginDocument{
\char_set_active_eq:NN | __usersixdigits_vertical_bar_active_do:w
}
\char_set_catcode_other:N |
\cs_new_protected:Npn __usersixdigits_vertical_bar_active_do:w {
\begingroup
\let\do@makeother
\dospecials
__usersixdigits_grab_following_argument:w
}
\cs_new_protected:Npn __usersixdigits_grab_following_argument:w #1 | {
\endgroup % reset the catcode of the following character (which might not be |)
\texttt{#1} % typeset the text collected so far
% the following peek will tokenize the following argument, so it's important to |\endgroup| above
\peek_meaning_remove:NT __usersixdigits_vertical_bar_active_do:w % we assume nothing other than the active | has this meaning
{
\texttt{|} % assuming the current font has the | character in the correct slot (which means either T1/TU encoding or OT1 typewriter font)
__usersixdigits_vertical_bar_active_do:w
}
}
\makeatother
\ExplSyntaxOff
\begin{document}
some text |12||34|
vertical bar ||||
vertical bar |||| abc
ab|x||y|cd
ab|x||y|{cd
\end{document}
Output is as you expect.

$\vert$. Also clumsy, as you said :( – yegor256 Nov 18 '22 at 05:09\usepackage[T1]{fontenc} \texttt{ab\textbar cd}. Anything else (that I can think of) involves a nontrivial amount of TeX programming/understanding. – user202729 Nov 18 '22 at 05:17