Analytify offers a great feature of implementing an automatic redirect to the Analytify dashboard upon user login in WordPress. This feature is particularly useful for administrators who want instant access to Analytify’s powerful analytics immediately upon logging in.
In this article, we’ll show the process step-by-step, ensuring a smooth integration of the provided code into your theme’s functions.php file.
Requirements:
Ensure you have Analytify installed and activated on your WordPress site.
How to Add the Redirect Code?
Below is a step-by-step process on where and how to add the provided code in your theme’s functions.php file.
1. Access Theme’s functions.php File:
In your WordPress site, navigate to Appearances >>Theme Editor.
Access the functions.php file within your active theme’s directory, as shown in the screenshot below:
2. Paste the Code Snippet:
You would need to carefully add the following code at the bottom of your theme’s functions.php file:
Note: This code will allow redirect for administrator role only.
function analytify_dashboard_redirect( $redirect_to, $request, $user ) {
if ( ! is_wp_error( $user ) ) {
return ( is_array( $user->roles ) && in_array( 'administrator', $user->roles ) ) ? admin_url( 'admin.php?page=analytify-dashboard' ) : $redirect_to;
}
}
add_filter( 'login_redirect', 'analytify_dashboard_redirect', 10, 3 );
However, if you want to add redirect for your editor, use the following code:
function analytify_dashboard_redirect( $redirect_to, $request, $user ) {
if ( ! is_wp_error( $user ) ) {
// Redirect to 'analytify-dashboard' page for administrators or editors, otherwise, use the original redirect.
return ( is_array( $user->roles ) && ( in_array( 'administrator', $user->roles ) || in_array( 'editor', $user->roles ) ) ) ? admin_url( 'admin.php?page=analytify-dashboard' ) : $redirect_to;
}
}
add_filter( 'login_redirect', 'analytify_dashboard_redirect', 10, 3 );4
In case, you want to add any other role you can simply replace “editor” with the specific role.
3. Save Changes:
Update the file to save the functions.php file.
4. Testing the Redirect:
Log out of your WordPress dashboard. Then, log In with the Administrator account.
5. Verify Redirect:
After successful login, you should automatically be redirected to the Analytify dashboard.