How to opto isloate a pin and still use it as either an output or input at will
There are bidirectional digital isolators. But if you ask about an optical solution specifically, I am not sure if there is an integrated solution for this.
You could build your own bidirectional optoisolator:
Two LEDs facing each other in an otherwise lightshielded environment. When you set one side to be a high impedance input, the other side can illuminate the input LED and cause it to develop a photovoltage. Don't expect a very high data rate, because you can only use rather large pull-downs in parallel to each LED.