Your idea of using an optocoupler might work if you used an "analog" optocoupler. This method often requires a feed back system to keep the control linear. You would first need to determine if the working range of the optocoupler would be compatible with the existing circuit being controlled by the potentiometer (for e.g.: the voltage, current, and power range). An analog optocoupler would not be suited for a circuit requiring a high power adjustment. This type of analog optocoupler (LED/Photo Diode) is best suited for adjusting a simple DC voltage and keeping it isolated from the controlling circuit. If you are trying to directly adjust a varying audio signal this type of optocoupler may not work well at all, (the LDR type optocoupler referenced below may work better with a limited range audio signal).
To make up for the 3 wire connection of the original potentiometer you might add one or more fixed resistors above and/or below the analog optocoupler to give a voltage divider arrangement with a center connection. Just note that with a fixed resistor and the analog optocoupler you may not be able to achieve the absolute full range of the original potentiometer.

simulate this circuit – Schematic created using CircuitLab
.
A few examples of analog optocouplers:
HCNR200/201
L300
For an analog optocoupler that acts more like a real resistor there are some that use a light dependent resistor (LDR). e.g.: NSL-32SR2
Here's an older Stackexchange question related to why an analog optocoupler was used: What is the purpose of using an analog optocoupler? .
A digital potentiometer (chip) may also be suitable in some situations. Here is an application note from Analog Devices describing their use and limitations. https://www.analog.com/media/en/technical-documentation/application-notes/an-1121.pdf
If you ultimately needed to retain the physical potentiometer there are still methods such as driving a servo to mechanically adjust a potentiometer shaft. Look up servo control using a raspberry pie.