Home Forums Plugin Support WP User Frontend Pro User Role Action Hook

This topic is: resolved

This topic contains 15 replies, has 2 voices, and was last updated by  towhid 3 years, 9 months ago.

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #64386

    MareVisie
    Member
    Post count: 83

    Hi All,

    I am trying to add an Action Hook that gets the User Role of the current logged user and displays it in a non-editable field on a profile page. My first question is where to put this code, as I am reluctant to use a child theme, seeing it messes up my Polylang translation. What's the alternative?
    Furthermore, I have questions concerning the actual code of the hook. The documentation focusses on Posts, rather than users so I'm not sure how to extrapolate to User Role(s). I was thinking along the lines of, but I really need some help:

    <?php
    /**
    * Add the user role field to the profile page
    *
    * @param int $form_id
    * @param null|int $post_id
    * @param array $form_settings
    */
    function render_user_role_hook( $form_id, $post_id, $form_settings ) {
      $value = '';
      $user = new WP_User( $user_id );
    
      foreach( $user->roles as $role ) {
        $role = get_role( $role );
        if ( $role = 'member' )
          $value = 'Thor';
        elseif ( $role = 'oldmember')
          $value = 'Old Engineer';
      }
      ?>
      <div class="wpuf-label">
        <label>Ledenstatus</label>
      </div>
     
      <div class="wpuf-fields">
        <input type="text" name="user_role_field" value="<?php echo esc_attr( $value ); ?>" disabled>
      </div>
      <?php
    }
    
    add_action( 'user_role_hook', 'render_user_role_hook', 10, 3 ); //I am really unsure what this 10 and 3 should be..
    
    //Do I still need to save the input?
    //If so: do I just put the code in the same file, or does it go somewhere else?
    #64706

    towhid
    Participant
    Post count: 4505

    Hello MareVisie,

    Can you please put this code in the theme's functions.php file and see what happen then and let me know the result.

    Thanks

    #64957

    MareVisie
    Member
    Post count: 83

    Hello Towhid,

    The code causes my website to stop working.

    #65048

    towhid
    Participant
    Post count: 4505

    Hello MareVisie,

    Please specify, which section create the offense on the website?

    Or, is the whole site stop working?

    Thanks

    #65052

    MareVisie
    Member
    Post count: 83

    The entire website stops working if I implement the code.

    #65215

    towhid
    Participant
    Post count: 4505

    Hello MareVisie,

    Can you please send me a screenshot.

    Also, please put this code in the child theme and see what happen then and let me know the result.

    Thanks

    #65233

    MareVisie
    Member
    Post count: 83

    It is working now that I have put it in the child theme of a test server.

    However, the code doesn't return a $value

    #65246

    MareVisie
    Member
    Post count: 83

    I got it! The code I used is below:

    
    <?php
    /**
    * Add the user role field to the profile page
    *
    * @param int $form_id
    * @param null|int $post_id
    * @param array $form_settings
    */
    function render_user_role_hook( $form_id, $post_id, $form_settings ) {
    	global $current_user;
    	$user_roles = $current_user->roles;
    	$user_role = array_shift($user_roles);
    	switch($user_role) {
    		case('member') :
    			$value = 'Thor';
    			break;
    		case('oldmember') :
    			$value = 'Old Engineer';
    			break;
    		case('administrator');
    			$value = 'Administrator';
    			break;
    	};
    	?>
    	<div class="wpuf-label">
    	<label>Ledenstatus</label>
    	</div>
    
    	<div class="wpuf-fields">
    	<input type="text" name="user_role_field" value="<?php echo $value; ?>" disabled>
    	</div>
    	<?php
    }
    add_action( 'user_role_hook', 'render_user_role_hook', 10, 3 );
    ?>
    
    
    #65247

    MareVisie
    Member
    Post count: 83

    If I upload the code from my localhost (where it is working) to the actual server. The code doesn't work.

    The thing is I can't use a child theme because it corrupts my translation using Polylang. Therefore, I have added the code to the functions.php of my Zerif Lite theme, where it causes a server-wide crash (displays a white screen).

    Is there a different way to implement this code? Maybe make a plugin that contains all my custom code?

    #65249

    towhid
    Participant
    Post count: 4505

    Hello Marre,

    Yes, you can create a plugin and see what happen. There might have a chance for making the things work for you.

    Thanks.

    #65251

    MareVisie
    Member
    Post count: 83

    I tried making a plugin on both my localserver and the online server. However, on the online server the plugin doesn't show up in the Plugin list.

    
    <?php
    /*
    Plugin Name: My Functions Plugin
    Author: Sjors Hijgenaar
    Version: 0.1
    */
    
    /**
    * Add the user role field to the profile page
    *
    * @param int $form_id
    * @param null|int $post_id
    * @param array $form_settings
    */
    function render_user_role_hook( $form_id, $post_id, $form_settings ) {
    	global $current_user;
    	$user_role = $current_user->roles[0];
    	switch($user_role) {
    		case('member') :
    			$value = 'Thor';
    			break;
    		case('oldmember') :
    			$value = 'Old Engineer';
    			break;
    		case('administrator');
    			$value = 'Administrator';
    			break;
    	};
    	?>
    	<div class="wpuf-label">
    	<label>Ledenstatus</label>
    	</div>
    
    	<div class="wpuf-fields">
    	<input type="text" name="user_role_field" value="<?php echo $value; ?>" disabled>
    	</div>
    	<?php
    }
    add_action( 'user_role_hook', 'render_user_role_hook', 10, 3 );
    ?>
    
    #65252

    MareVisie
    Member
    Post count: 83

    Uploading through a .zip gives the following WordPress error:

    The package could not be installed. PCLZIP_ERR_BAD_FORMAT (-10) : Unable to find End of Central Dir Record signature

    #65358

    towhid
    Participant
    Post count: 4505

    Hello MareVisie,

    Make sure you have properly keep the file in the zip folder. Or you can manually keep the file in the plugin folder no installation.

    Thanks

    #65383

    MareVisie
    Member
    Post count: 83

    I tried both ways, both have failed.

    If I make the plugin folder and upload the files, the plugin doesn't appear in my Plugin list in the admin panel.

    If I make a .zip and try to upload it, it gives the error stated in my previous message.

    #65413

    MareVisie
    Member
    Post count: 83

    I have solved the issue. I used chown, chgrp and chmod to change the permissions of the files and directories. The action hook is now working properly AND I have a plugin that I can use to add functionality to my WordPress installation.

    Thank you for all the support ;).

Viewing 15 posts - 1 through 15 (of 16 total)

The topic ‘User Role Action Hook’ is closed to new replies.