This topic contains 15 reply and 2 voices, and was last updated by towhid 4 years ago
Viewing 15 Posts - 1 through 15 (of 15 total)
Author Posts
July 30, 2015 at 4:04 pm 64386
towhid 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?
August 1, 2015 at 5:02 pm 64706
towhid towhid

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

August 3, 2015 at 1:01 pm 64957
MareVisie MareVisie

Hello Towhid,

The code causes my website to stop working.

August 3, 2015 at 6:27 pm 65048
towhid towhid

Hello MareVisie,

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

Or, is the whole site stop working?

Thanks

August 3, 2015 at 6:30 pm 65052
MareVisie MareVisie

The entire website stops working if I implement the code.

August 4, 2015 at 3:35 pm 65215
towhid towhid

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

August 4, 2015 at 4:22 pm 65233
MareVisie MareVisie

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

August 4, 2015 at 5:21 pm 65246
MareVisie MareVisie

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 );
?>

August 4, 2015 at 5:29 pm 65247
MareVisie MareVisie

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?

August 4, 2015 at 5:33 pm 65249
towhid towhid

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.

August 4, 2015 at 5:43 pm 65251
MareVisie MareVisie

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 );
?>
August 4, 2015 at 5:45 pm 65252
MareVisie MareVisie

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

August 5, 2015 at 11:23 am 65358
towhid towhid

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

August 5, 2015 at 1:00 pm 65383
MareVisie MareVisie

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.

August 5, 2015 at 3:18 pm 65413
MareVisie MareVisie

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 ;).

August 5, 2015 at 6:28 pm 65472
towhid towhid This reply has been marked as private.
Viewing 15 Posts - 1 through 15 (of 15 total)