I have a Wordpress website
www.example.com
Once a user goes to URL he sees Wordpress login page and below URL in the browser address bar.
www.example.com/wp-admin/....
I want to change that so when people go www.example.com they see the login page and the url in the browser address bar remains same www.example.com or in other words I dont want that slug and redirect text seen in the address bar. Is that possible to do?
I don't know that you can rewrite wp-admin, but you could create your own login code to display on the Home page. To start with, wp_login_form() displays a login form:
<?php wp_login_form(); ?>
You could add this code to a page template, or create a shortcode so it can be added via the Dashboard editor like this:
add_action( 'init', 'stackoverflow_login_shortcode' );
function stackoverflow_login_shortcode() {
add_shortcode( 'stackoverflow-login-form', 'stackoverflow_login_form_shortcode' );
}
function stackoverflow_login_form_shortcode() {
if ( is_user_logged_in() ) {
return '<p>You are already logged in!</p>';
}
return wp_login_form( array( 'echo' => false ) );
}
Now you can add [stackoverflow-login-form] into a page.
Related
I have created a registration form using Elementor Page Builder. Now, I want to redirect the user to a different page if he/she is trying to access that registration page after logging in.
Is there any Elementor hook available for that? I know the WordPress function called is_user_logged_in().
function my_logged_in_redirect() {
if ( is_user_logged_in() && is_page( 12 ) )
{
wp_redirect( get_permalink( 32 ) );
die;
}
}
add_action( 'template_redirect', 'my_logged_in_redirect' );
You should get the ids of the page where the form is and the id of the page you want to redirect the user to.
Code goes in your child theme functions.php file
Reference: here
The 'Content Area Not Found' error might appear on Elementor designed sites when you use that snippet and try to edit page of ID 12 (in your example) in certain cases.
To avoid this, add the following code before the if-statement of your snippet:
if ( \Elementor\Plugin::$instance->preview->is_preview_mode() ) {
return;
}
I have a Wordpress site where I keep track of publications with an archive of custom post types called "publications". Each publication post shouldn't have its own page, it just needs to appear in the archive page. Right now when I create a publication called "test publication" there is a page created at www.mysite.com/publications/test-publication. I've seen the suggestion of changing the post option public to false, but then I can't access the archive itself, it just redirects to the home page. If I add publicly_queryable => true in addition to public => false then I can get to the archive page, but the dedicated page for each publication shows up again. I need it to give me a 404 if I try to visit www.mysite.com/publications/test-publication but still allow me to access the archive. Help, am I missing something obvious?
You can have template redirect added so that singular link if accessed redirects to Archive page:
add_action( 'template_redirect', 'disable_singular_publications' );
function disable_singular_publications()
{
if ( ! is_singular( 'publications' ) )
return;
wp_redirect( get_post_type_archive_link( 'publications' ), 301 );
exit;
}
You may add above function in functions.php , code is not tested so you may need to check on any typos or syntax errors.
I am using Buddypress and a plugin called Teambooking. The Teambooking plugin doesn't know of the existence of Buddypress and it uses the default Wordpress author query string (/?author="id", where id is the user id number) to create and display a link to an author's page.
But Buddypress provides profile pages for users.
So I want to be able to catch when someone clicks on the standard author page link (/?author="id") and re-direct the page to Buddypress user profile page. I thought I could use the hook 'template-redirect' to catch this event, but that doesn't work, because the hook only get's triggered when the destination page is being loaded, and what is happening is that Wordpress is automatically redirecting the URL that has the /?author="id" query string to the index.php page, because there is no author.php page.
I cannot use $_SERVER["QUERY_STRING"] either to parse the URL , because as I said, Wordpress automatically redirects to index.php, dropping the query string.
I also was thinking that I could create an author.php page, so that WordPress stops redirecting to index.php, and then use 'template_redirect' hook to redirect the author.php to the buddypress profile page, but that didn't work either. I created author.php on my theme's directory but Wordpress continues to redirect to index.php.
Any ideas on how to this redirection from the /?author="id" query to the proper buddypress profile page of the user?
This must happen all the time with plugins that are not aware of Buddypress being installed .
thanks
-Malena
It turns out that the free version of Yoast SEO plugin was redirecting a query for the author page (?author=id) to index.php automatically, and I wasn't aware of it. So I had to deactivate Yoast SEO plugin, and then used is_author() to detect when the author archive page was being requested (?author=id query string in URL ) inside a template-redirect hook in order to redirect the call to the appropriate BuddyPress user profile. Here is the code I used in functions.php, which does the redirection:
/* Redirect author page to BuddyPress page */
function my_page_template_redirect()
{
/** Detect if author archive is being requested and redirect to bp user profile page */
if( is_author() )
{
global $wp_query;
if(isset($wp_query->query_vars['author'])) {
$userID = urldecode($wp_query->query_vars['author']);
}
$url = bp_core_get_user_domain($userID);
wp_redirect( $url );
exit();
}
add_action( 'template_redirect', 'my_page_template_redirect' );
Buddypress doesn't work with the default permalink structure, so it probably isn't the author id that you're looking for. If your permalink structure were set to Post name for instance, then this would work.
/* Redirect author page to buddypress page */
function my_page_template_redirect()
{
if( is_author() )
{
global $wp_query;
$user = get_user_by( 'slug', $wp_query->query['author_name'] );
$url = bp_core_get_user_domain($user->ID);
wp_redirect( $url );
exit();
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
In wordpress i want a different homepage for logged-in users and those who are logged-out (simple changing the redirect url when somebody clicks on the title of my website). Is there a way of doing this by adding a code snippet in my themes functions.php? I tried:
add_filter('get_the_permalink','my_permalink_redirect');
function my_permalink_redirect($permalink) {
if( is_user_logged_in() ) {
global $post;
if ($post->ID == 1) {
$permalink = 'http://localhost/homepage-for-logged-users';
}
}
return $permalink;
}
unfortunately it doesn't work and it would be unhandy to change the function from localhost to my domain name when i upload the site to a live hosting. can somebody give me some advice? Thanks!
edit:I got it working with the pluging "login with ajax" where I could define redirects after logins and I putted this snippet in my functions.php.
// Redirect users who arent logged in and on a 404 to the home page for logged out users
function login_redirect() {
// Check to see if user in not logged in and on the home page for logged users
if(!is_user_logged_in() && is_404()) {
// If user is, Redirect to home page for logged out users.
wp_redirect(home_url('home-logged-out'));
exit;
}
}
// add the block of code above to the WordPress template
add_action( 'wp', 'login_redirect' );
*I made the pages for logged-in users private so logged-out users will see 404. Not a very clean method, but it works for now...
Set your default home page to the page logged in users should see.
Add this code to functions.php:
add_action('init', 'redirect_user');
// for users not logged in
function redirect_user(){
if( !is_user_logged_in() ) {
wp_redirect( 'http://www.YourSite.com/SomePage');
exit;
}
}
Logged in users will see what they should see, and those not logged in will be redirected.
What I am trying to do is have the Edit Profile option in the Admin bar at the top of a wordpress page go to another url.
Basically instead of it going to the /wp-admin directory
I am trying to get it to go the the /editprofile.php page.
You can use the filter edit_profile_url:
add_filter( 'edit_profile_url', 'custom_profile_link_so_19216787', 10, 3 );
function custom_profile_link_so_19216787( $url, $user, $scheme )
{
return site_url( 'editprofile' );
}
I suppose that /editprofile.php is a page using this template and that its address is example.com/editprofile. If that's not the case, put a full URL instead of site_url($slug).
Related: Where to put my code: plugin or functions.php?