after logged in it wont redirect to previous page
It’s because in your loginform()
function, the 'redirect' => home_url()
below sets the redirect URL to the homepage URL:
$args = array('redirect' => home_url(), 'id_username' => 'user','id_password' => 'pass',);
So if you want to redirect to the previous page, then remove the redirect
argument above, or use the code below, which means if redirect_to=<URL>
is present in the current URL as in example.com/login/?redirect_to=https://example.com/some-page/
, then the specified URL will be used instead:
// Define your $args like this:
$args = array( 'id_username' => 'user', 'id_password' => 'pass' );
// Then add the 'redirect', if the URL has the redirect_to=<URL>.
if ( ! empty( $_REQUEST['redirect_to'] ) ) {
$args['redirect'] = $_REQUEST['redirect_to'];
}
See the wp_login_form()
documentation for more details on the redirect
and other arguments.
Additional Notes
Excerpt from codex.wordpress.org/Shortcode_API:
The return value of a shortcode handler function is inserted into the
post content output in place of the shortcode macro. Remember to use
return and not echo – anything that is echoed will be output to the
browser, but it won’t appear in the correct place on the page.
Therefore your loginform()
should actually return the output and not echoing it (or anything else), because in addition to the above issue (shortcode appearing in the wrong place), an echo
in the function would result in a failure in saving a post via the block editor (Gutenberg) which uses the REST API (so if your function echo something, the REST API response will be invalid).
So in that function, change the echo '<p>
to return '<p>
, then in the $args
array, add 'echo' => false
, and finally change the wp_login_form( $args )
to return wp_login_form( $args )
.