Let me start by introducing some terminology which might not be seen as standard for some people.
The discriminator function on an algebra $\mathbf{A}$ is the function $f:A^3 \to A$ defined by
$$
f(x,y,z) =
\begin{cases}
x &, \text{ if } x \neq y\\
z &, \text{ if } x = y.
\end{cases}
$$
We say that $\mathbf{A}$ has a discriminator term if the discriminator function can be represented by a term of $\mathbf{A}$.
For example, the ring $\mathbb{Z}_p$, for $p$ a prime number, has the following discriminator term:
$$d(x,y,z) = (x-y)^{p-1} \cdot x + (1-(x-y)^{p-1}) \cdot z,$$
since $x^{p-1} = 1$ if $x \neq 0$.
A finite algebra with a discriminator term is said to be quasiprimal.
An algebra $\mathbf{A}$ is said to be demi-semi-primal if it is quasiprimal and each isomorphism between nontrivial subalgebras of $\mathbf{A}$ can be extended to an automorphism of $\mathbf{A}$.
Apparently these algebras were first defined in the paper
Demi-semi-primal algebras and Mal'cev-type conditions
Quackenbush, R.W. Math Z (1971) 122: 166-176.
https://doi.org/10.1007/BF01110090
I don't have access to that paper but I suspect that the solution to the following problem is there:
(Exercise IV.10.5, in Burris and Sankappanavar, Universal Algebra)
Show that a finite algebra $\mathbf{A}$ is demi-semi-primal iff every $n$-ary function (with $n \geq 1$) on $\mathbf{A}$ which preserves subalgebras of $\mathbf{A}$ and subuniverses of $\mathbf{A}^2$ consisting of automorphisms of $\mathbf{A}$ can be represented by a term.
I was able to prove the forward implication (if $\mathbf{A}$ is demi-semi-primal, then any function under the stated conditions is representable).
For the reverse implication, I was able to prove that if those functions are representable then $\mathbf{A}$ is quasiprimal, but I don't even see how to tackle the proof that isomorphisms between nontrivial subalgebras are extendible to automorphisms.
Any hints?
I might as well reproduce here the proofs I made (though this is already a long post).
Thanks in advance.