I know in PHP I can just put action equal to random.php file and process the data there, but how in wordpress can I use already existing page with a custom template to submit a form to, so that after submitting a form on one page, user will be redirected to another page in wordpress with all his inputted credentials still available?
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
<input type="hidden" name="action" value="your_action_name">
Add these in your form. Where admin-post.php will process your form. In that case based on the value of your_action_name that is provided by you, an action hook will get involved. Say for example if you add a hook like the following in
functions.php of your theme or in your plugin
add_action( 'admin_post_nopriv_your_action_name', 'your_function_to_process_form' );
then for non logged in user
// process your form here
will be called. From there you can process your form. For logged in user you need to rename your action to
admin_post_nopriv_ are available in admin-post.php to
do_action appropriate action. Whatever you append at the end of
admin_post_ will formulate a action hook. That needs to implemented by
add_action(). If you pass
contactform as a hidden action then your action hook will be either
admin_post_contactform or both.