Definitions: Let $k$ be an arbitrary field, $I$ a prime ideal of $k[x_1, \dots, x_n]$, $J$ a prime ideal of $k[y_1, \dots, y_m]$, $X = \{ x \in k^n: \forall f \in I, \ f(x) = 0\ \} \subset k[x_1, \dots, x_n]$, $Y=\{y \in k^m: \forall g \in J, \ g(y)=0 \}$, $\mathcal{O}_X = k[x_1,\dots,x_n]/I$, and $\mathcal{O}_Y = k[y_1, \dots, y_m]/J$.
(I.e. $X$ and $Y$ are affine varieties, and $\mathcal{O}_X$ and $\mathcal{O}_Y$ are their respective coordinate rings.)
Both $\mathcal{O}_X$ and $\mathcal{O}_Y$ are commutative rings with a multiplicative identity, so when considering a ring homomorphism $\varphi: \mathcal{O}_Y \to \mathcal{O}_X$ we will mean a function $\mathcal{O}_Y \to \mathcal{O}_X$ such that for all $g_1,g_2 \in \mathcal{O}_Y$ $\varphi(g_1+g_2)=\varphi(g_1)+ \varphi(g_2)$, $\varphi(g_1g_2)=\varphi(g_1)\varphi(g_2)$, and $\varphi(1+J)=1+I$.
Question: Under what conditions on the field $k$ is any ring homomorphism $\varphi: \mathcal{O}_Y \to \mathcal{O}_X$ a $k$-algebra homomorphism? I.e. under what conditions on the field $k$ does any ring homomorphism $\varphi: \mathcal{O}_Y \to \mathcal{O}_X$ commute with scalar multiplication, for all $c \in k, g \in k[y_1, \dots, y_m]$, $$\varphi(cg+J)=c\varphi(g+J)? $$ Because $k \subset k[x_1, \dots, x_n]$, $k\subset k[y_1, \dots, y_m]$, the above is equivalent to requiring, for all $c \in k$, that: $$\varphi(c+J) = c+I\,, $$ with the previously stated property following from the fact that $\varphi$ commutes with multiplication.
Attempt: In special cases, e.g. $k = \mathbb{Q}$ or $k=\mathbb{R}$, this should be true (provided in the latter case that $\varphi$ is also continuous, which I don't see why we can assume in general). For more general $k$, I don't see at all why this should be true, even if $\varphi$ is continuous.
For $k=\mathbb{Q}$, I think this follows from the facts that $\varphi(1+J)=1+I$, $\varphi(g_1g_2+J)=\varphi(g_1+J)\varphi(g_2+J)$, and that $\varphi((g_1+g_2)+J)= \varphi(g_1+J)+\varphi(g_2+J)$. Namely, $$\varphi(2+J) = \varphi((1+1)+J)=\varphi(1+J)+\varphi(1+J)=(1+I)+(1+I)=2+I\,, $$ and then by using induction we get that $\varphi(n+J)=n+I$ for all $n \in \mathbb{N}$.
Then for any $n \in \mathbb{Z}$, we get that $\varphi(n+J)=n+I$ because $\varphi(J)=I$ and $I=\varphi(J)=\varphi((n+(-n))+J)=\varphi(n+J)+\varphi(-n+J) \implies \varphi(n+J)=-\varphi(-n+J)$.
Then for any $q = \frac{m}{n} \in \mathbb{Q}$, we have that $$m+I=\varphi(m+J)=\varphi\left(\left(\frac{m}{n}\right)n+J\right)=\varphi\left(\frac{m}{n}+J\right)\varphi(n+J)=\varphi\left(\frac{m}{n}+J\right)(n+I)\\ \implies \varphi\left(\frac{m}{n}+J\right)=\frac{m}{n}+I\,.$$
Then, assuming that $\varphi$ is continuous, since any real $r \in \mathbb{R}$ is a limit of some sequence of rational numbers $\{q_n\}$, and continuous functions commute with limits of sequences, we get: $$ \varphi(r+J)=\varphi\left(\lim_{n \to \infty} (q_n+J)\right) = \lim_{n \to \infty} \varphi(q_n +J) = \lim_{n \to \infty} (q_n + I) = r+I\,. $$ Thus $\varphi(c+J)=c+I$ for all $c \in k$ for $k=\mathbb{R}$ (or $k=\mathbb{Q}$). (Related)
For $k=\mathbb{C}$, since $-1 \in \mathbb{Z}$, we have that: $$-1+I= \varphi(-1 +J)= \varphi(i^2 +J)= \varphi(i+J)^2 \implies \varphi(i+J) = \pm i +I \,. $$ Since the Galois group of $\mathbb{R}[i]$ is non-trivial, I don't see how it could be possible to exclude the possibility that $\varphi(i+J)=-i+I$. Similar problems would arise with other algebraic field extensions of $\mathbb{Q}$ or $\mathbb{R}$ (I imagine).
For fields without straightforward relationships to $\mathbb{Q}$, I imagine that this could fail to be true in even more dramatic ways, but I don't know, hence my question.
Context: This question is inspired by Problem 4.18.5 of Algebraic Geometry: A Problem Solving Approach by Garrity et al., which asks one to show that each ring homomorphism $\mathcal{O}_Y \to \mathcal{O}_X$ corresponds to a morphism between the affine varieties $X \to Y$, for $k$ an arbitrary field. The only way I could get a proof to work was with the additional assumption that $\varphi(c+J)=c+I$ for all $c\in k$, which as I noted above is equivalent to $\varphi$ commuting with scalar multiplication and thus being a $k$-algebra homomorphism. This seems to be corroborated by Problem 4.8.4 of the same book, Wikipedia, Theorem 4.14 here, and other references I remember finding.
Thus, I want to understand the extent of the strength of this additional assumption, and whether or not it actually follows for any fields $k$ from the fact that $\varphi: \mathcal{O}_Y \to \mathcal{O}_X$ is a ring homomorphism.