Wordpress custom link for every user - wordpress

How can i create a page without creating an actual page? I want to create websitelink.com/username where username will be the username of every user on my site. I want a profile page of every user and they can view other profile too. User dont have admin privileged. Thank you. I dont want to use plugin because I need to customize the view. I also want to view user profile even not logged in.

You can create custom page template and then write a code to get user by username. So your link will be like websitelink/users/{username} username will be dynamic.
<?php
/**
* Template Name: User Profile Page
**/
// Getting the username from the url
$link = $_SERVER['PHP_SELF'];
$link_array = explode('/',$link);
$loginname = end($link_array);
// Getting user info by wordpress default function
$user = get_userdatabylogin($loginname);
if($user){
echo $user;
}
$user will have all the info which you will need.

Related

Check User profile When change page Symfony2

I create User with FOSUserBundle, and when user created I create also a profile
User And Profile
I'd like that whenever the user changes the page, actually check that the user is navigating is authenticated and has also the profile
for now in my Controller i do
$user = $this->getUser();
if (!is_object($user)) { //i have to add || null !== $user->getProfile()
throw new AccessDeniedException('This user does not have access to this section.');
}
But I would not repeat this code in all controllers, and I would also check if the user has a profile, otherwise redirect to the home page
you can use JMSSecurityExtraBundle to add annotation for each action:
/**
* #Secure(roles="ROLE_USER, ROLE_FOO, ROLE_ADMIN")
*/
or without using of the bundle:
if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) {
throw new AccessDeniedException();
}
its more simple. Maybe theres another solution to make it for all your controller but i don't know it.
You can use the request listener :
http://symfony.com/doc/current/cookbook/service_container/event_listener.html#request-events-checking-types
Just add your magic and redirect through the event to the homepage

WordPress plugin to add user role using textbox and form submit

I'm trying to create a plugin where an admin (custom defined user role, not regular administrator) can access an options page, enter a specific username into a textbox, hit submit, and by doing so add a second user role to that user. I know that this code:
$user = new WP_User( null, 'username' );
$user->add_role( 'admin' );
will add the "admin" role to someone with the username "username." So in my plugin's settings page, I have a textbox:
Add an Admin: <input type="text" size="57" name="sscaur_options[txt_admin]" value="<?php echo $options['txt_admin']; ?>" />
and then this at the end:
function sscaur_add_admin($textbox) {
$options = get_option('sscaur_options');
$textbox = $options['txt_admin'];
$user = new WP_User( null, '{$textbox}' );
$user->add_role( 'admin' );
return $textbox;
}
I know that's totally wrong, but don't know how to begin to make it right.
I know that adding second user roles can be done with the User Role Editor plugin, but that's hardcoded to only give access to actual administrators, and I need my custom user roles to be able to add roles to specific users without being full administrators.

Wordpress : Page that can be viewed only by members and if not a member than show them a registration page

I am trying to create a sampling process , where a user need to register or login to view my sample pdf documents , and once they register the user should be given a role as a sampler .
I am not a pro at wordpress but am learning . Can u guys suggest me some plugin or ideas on how to do it , i am using wordpress WordPress 3.3.1 .Thanks
http://codex.wordpress.org/Function_Reference/is_user_logged_in
http://codex.wordpress.org/Function_Reference/wp_login_form
As per your latest question, you will need to use these in tandem with each other, and make sure to define your redirect within your argument array to make sure the user is directed back to the page from which they logged in.
FINAL UPDATE: Enter this in your functions.php file and utilize as you see fit. This will allow you to output information according to the actual role of your user. This is also assuming that your plugin is creating actual custom roles. If not, you will have to utilize the Add Role Function:
function get_user_role()
{
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
Then in the template of the page you want:
<?php
if(is_user_logged_in())
{
switch(get_user_role())
{
case 'customer' :
//CUSTOMER DISPLAY
break;
case 'subscriber' :
//SUBSCRIBER DISPLAY
break;
default :
//OTHER TYPE OF USER DISPLAY
}
}
else
{
//NO LOGGED USER DISPLAY
}
?>

How to add the user role in the registration form

I want to display the user role field in the wordpress user registration form.
I did not see any option form the admin side.
How will I do this? So that the user select his role in the registration form. If he is a student then he will select student role otherwise staff.
Any help?
In Wordpress, the roles are stored in $wp_roles global variable. So, if you want to display all the roles try this:
global $wp_roles;
foreach ($wp_roles->roles as $role) {
echo $role['name'];
}

Drupal: redirecting users with specific role to another page

How can I redirect only users with a specific role to the admin page when they login ?
Well, you need to get the global $user variable. And then check if the user has the desired role, (in this case I assumed the desired role is 'authenticated user'), and the current page is not the admin page (so you don't get a redirection problem), then redirect him to the admin page, or a page of your choice inside drupal_goto('admin');
There you go:
<?php
global $user;
if(in_array('authenticated user', $user->roles) && arg(0) != 'admin') {
drupal_goto('admin');
}
?>

Resources