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.
Assume that $A$ has nontrivial subalgebras $S$ and $T$ for which there is an isomorphism $\iota:S\to T$ that is not the restriction of an automorphism of $A$. From this, I will argue that there is an operation on $A$ that preserves the subalgebras and automorphisms of $A$, but which is not representable by a term.
Order the distinct elements of $S$ in a tuple $\sigma:=(s_1,\ldots,s_k)$, and then order the elements of $T$ as $$ \tau:=(t_1,\ldots,t_k) = (\iota(s_1),\ldots,\iota(s_k)). $$ The nontriviality of $S$ and $T$ means that these tuples have length at least $2$.
Let $G$ be the automorphism group of $A$, and let $G$ act diagonally on $A^k$. Write $[\rho]$ to denote the $G$-orbit of $\rho\in A^k$. The fact that there is no automorphism of $A$ that restricts to $\iota$ means exactly that $[\sigma]\neq [\tau]$.
Now define a function $F:A^k\to A$ as follows: $F(x_1,\ldots,x_k) = x_1$ if $(x_1,\ldots,x_k)\notin [\tau]$, and $F(x_1,\ldots,x_k) = x_2$ if $(x_1,\ldots,x_k)\in [\tau]$.
The fact that $F$ is defined to be first projection on a union of $G$-orbits of $A^k$, and defined to be second projection on the remaining $G$-orbits implies that $F$ is conservative and $G$-equivariant, hence $F$ preserves all subalgebras and automorphisms of $A$. (In fact, $F$ preserves all $\underline{\rm subsets}$ and automorphisms of $A$.)
But $F$ cannot be represented by a term, because all terms are respected by $\iota$ and $F$ is not: $$ \iota(F(s_1,\ldots,s_k))=\iota(s_1)=t_1\neq t_2=F(t_1,\ldots,t_k)=F(\iota(s_1),\ldots,\iota(s_k)). $$