L
L
ligisayan2018-09-10 16:46:04
WordPress
ligisayan, 2018-09-10 16:46:04

Why can't I redirect to the main page in wordpress after sending the email?

Hello! There is a site on wordpress. After sending the letter, I want to redirect to the main page, but I get an error 500 as a result. Why and how can I fix this?

mail($to, $subject, $msg, $headers);
  wp_redirect( home_url() );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2018-09-10
@ligisayan


PHP Fatal error: Call to undefined function wp_redirect() in /home/a/public_html/templates/42/wp-content/themes/chrimson-child/contact.php on line 21
And how can I fix this?

So your contact.php file, of course, does not know about the existence of WordPress and its functions, and vice versa, which is why I asked you to show how you call it at all.
You need to rewrite the form submission. The easiest option is to use a plugin like Contact Form 7 (there are js events on which you can hang a redirect after submitting the form). Or write your own ajax handler, but not in an arbitrary file, but in function.php or in the file that is connected to it.
https://wp-kama.ru/id_2018/ajax-v-wordpress.html
Well, or the worst option is to use your handler, just DO NOT use WP functions in it. Including, home_url()
www.php.net/manual/en/function.header.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question