Powered By Blogger
Showing posts with label user. Show all posts
Showing posts with label user. Show all posts

Wednesday, April 19, 2017

Get current logged in user details in buddypress OR get profile field data for loggin user

//get login user sex
global $bp;
$the_user_id = $bp->loggedin_user->userdata->ID;
$loginUserSex = bp_get_profile_field_data( 'field=sex&user_id=' . $the_user_id );

Monday, March 20, 2017

dynamically add username in the URL

/* dynamically add username in the URL
 ================================================== */
add_filter( 'nav_menu_link_attributes', 'menu_override', 10, 3 );
function menu_override( $atts, $item, $args ) {
    $user = wp_get_current_user();
    $newlink = str_replace("--username--", $user->user_login, $atts['href']);
    $atts[href] = $newlink;
    return $atts;
}

Tuesday, March 14, 2017

set profile data for a specific field for a specific user. OR update user extend profile fields

http://buddypress.wp-a2z.org/oik_api/xprofile_set_field_data/

                $interest = isset($_POST['interest'])?$_POST['interest']:''; //value
                $field = 'Interest'; // field name
$field_id = xprofile_get_field_id_from_name( $field );
$field           = new BP_XProfile_ProfileData();
$field->field_id = $field_id;
$field->user_id  = $user_id;
$field->value    = maybe_serialize( $interest );
$field->save();





function xprofile_set_field_data( $field, $user_id, $value, $is_required = false ) {

  if ( is_numeric( $field ) ) {
    $field_id = $field;
  } else {
    $field_id = xprofile_get_field_id_from_name( $field );
  }

  if ( empty( $field_id ) ) {
    return false;
  }

  $field          = xprofile_get_field( $field_id );
  $field_type     = BP_XProfile_Field::get_type( $field_id );
  $field_type_obj = bp_xprofile_create_field_type( $field_type );

  
/**
 * Filter the raw submitted profile field value.
 *
 * Use this filter to modify the values submitted by users before
 * doing field-type-specific validation.
 *
 * @since 2.1.0
 *
 * @param mixed                  $value          Value passed to xprofile_set_field_data().
 * @param BP_XProfile_Field      $field          Field object.
 * @param BP_XProfile_Field_Type $field_type_obj Field type object.
 */
  $value = apply_filters( 'bp_xprofile_set_field_data_pre_validate', $value, $field, $field_type_obj );

  // Special-case support for integer 0 for the number field type.
  if ( $is_required && ! is_integer( $value ) && $value !== '0' && ( empty( $value ) || ! is_array( $value ) && ! strlen( trim( $value ) ) ) ) {
    return false;
  }

  
/**
 * Certain types of fields (checkboxes, multiselects) may come through empty.
 * Save as empty array so this isn't overwritten by the default on next edit.
 *
 * Special-case support for integer 0 for the number field type
 */
  if ( empty( $value ) && ! is_integer( $value ) && $value !== '0' && $field_type_obj->accepts_null_value ) {
    $value = array();
  }

  // If the value is empty, then delete any field data that exists, unless the field is of a type
  // where null values are semantically meaningful.
  if ( empty( $value ) && ! is_integer( $value ) && $value !== '0' && ! $field_type_obj->accepts_null_value ) {
    xprofile_delete_field_data( $field_id, $user_id );
    return true;
  }

  // For certain fields, only certain parameters are acceptable, so add them to the whitelist.
  if ( $field_type_obj->supports_options ) {
    $field_type_obj->set_whitelist_values( wp_list_pluck( $field->get_children(), 'name' ) );
  }

  // Check the value is in an accepted format for this form field.
  if ( ! $field_type_obj->is_valid( $value ) ) {
    return false;
  }

  $field           = new BP_XProfile_ProfileData();
  $field->field_id = $field_id;
  $field->user_id  = $user_id;
  $field->value    = maybe_serialize( $value );

  return $field->save();
}
 

Saturday, November 5, 2016

other than admin user block wp-admin

add_action( 'admin_init', 'redirect_non_admin_users' ); /** * Redirect non-admin users to home page * * This function is attached to the 'admin_init' action hook. */ function redirect_non_admin_users() { if ( ! current_user_can( 'manage_options' ) && '/wp-admin/admin-ajax.php' != $_SERVER['PHP_SELF'] ) { wp_redirect( home_url() ); exit; } }

Friday, October 21, 2016

get user information in front end

https://codex.wordpress.org/Function_Reference/the_author_meta


<h2><?php the_author_meta( 'display_name' ); ?></h2>

<p><?php echo the_author_meta('description'); ?></p>

<a href="http://twitter.com/<?php echo the_author_meta('twitter'); ?>" target="_blank"><?php echo the_author_meta('twitter'); ?> sdcfsdf</a>

how to create custom user roles in wordpress

 http://stackoverflow.com/questions/18060156/wp-user-create-setting-a-role-wordpress


$user_id = wp_create_user( $user_name, $random_password, $user_email );
$user_id_role = new WP_User($user_id);
$user_id_role->set_role('contributor');


Wednesday, October 19, 2016

add custom fields user registration wordpress

http://blog.ashfame.com/2010/11/add-custom-field-registration-wordpress/



<?php
/*
Plugin Name: Custom Profile field as a Registration Field
Plugin URI: http://blog.ashfame.com/2010/11/add-custom-field-registration-wordpress/
Description: Sample plugin for adding a custom profile field to WordPress and on the registration page
Author: Ashfame
Version: 0.1
Author URI: http://blog.ashfame.com/
*/

/**
 * Add additional custom field
 */

add_action ( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action ( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_show_extra_profile_fields ( $user )
{
?>
<h3>Store information</h3>
<table class="form-table">
<tr>
<th><label for="twitter">Twitter</label></th>
<td>
<input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter your Twitter username.</span>
</td>
</tr>
<tr>
<th><label for="sname">Store Name</label></th>
<td>
<input type="text" name="sname" id="sname" value="<?php echo esc_attr( get_the_author_meta( 'sname', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter your Store name.</span>
</td>
</tr>
</table>
<?php
}

add_action ( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action ( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id )
{
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
/* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
update_usermeta( $user_id, 'twitter', $_POST['twitter'] );
update_usermeta( $user_id, 'sname', $_POST['sname'] );
}

/**
 * Add cutom field to registration form
 */

add_action('register_form','show_first_name_field');
add_action('register_post','check_fields',10,3);
add_action('user_register', 'register_extra_fields');

function show_first_name_field()
{
?>
<p>
<label>Twitter<br/>
<input id="twitter" type="text" tabindex="30" size="25" value="<?php echo $_POST['twitter']; ?>" name="twitter" />
</label>
</p>
<p>
<label>Store Name<br/>
<input id="sname" type="text" tabindex="30" size="25" value="<?php echo $_POST['sname']; ?>" name="sname" />
</label>
</p>
<?php
}

function check_fields ( $login, $email, $errors )
{
global $twitter;
if ( $_POST['twitter'] == '' )
{
$errors->add( 'empty_realname', "<strong>ERROR</strong>: Please Enter your twitter handle" );
}
else
{
$twitter = $_POST['twitter'];
}

global $sname;
if ( $_POST['sname'] == '' )
{
$errors->add( 'empty_realname', "<strong>ERROR</strong>: Please Enter your Store name" );
}
else
{
$sname = $_POST['sname'];
}
}

function register_extra_fields ( $user_id, $password = "", $meta = array() )
{
update_user_meta( $user_id, 'twitter', $_POST['twitter'] );
update_user_meta( $user_id, 'twitter', $_POST['sname'] );
}

?>

Friday, July 1, 2016

hide user profile fields wordpress

add_action('admin_init', 'user_profile_fields_disable');

function user_profile_fields_disable() {

    global $pagenow;

    // apply only to user profile or user edit pages
    if ($pagenow!=='profile.php' && $pagenow!=='user-edit.php') {
        return;
    }

    // do not change anything for the administrator
    if (current_user_can('administrator')) {
        return;
    }

    add_action( 'admin_footer', 'user_profile_fields_disable_js' );

}


/**
 * Disables selected fields in WP Admin user profile (profile.php, user-edit.php)
 */
function user_profile_fields_disable_js() {
?>
    <script>
        jQuery(document).ready( function($) {
            var fields_to_disable = ['email', 'role', 'submit'];
            for(i=0; i<fields_to_disable.length; i++) {
                if ( $('#'+ fields_to_disable[i]).length ) {
                    $('#'+ fields_to_disable[i]).css("display", "none");
                }
            }
        });
    </script>
<?php
}