Skip to content

Instantly share code, notes, and snippets.

@sabbir1991
Last active February 6, 2019 20:57
Show Gist options
  • Save sabbir1991/19adb22e9719e6a0e47cb019da891dcb to your computer and use it in GitHub Desktop.
Save sabbir1991/19adb22e9719e6a0e47cb019da891dcb to your computer and use it in GitHub Desktop.
Added shop url into wp admin new user interface
<?php
// Add this actions
add_action( 'user_new_form', 'add_seller_store_field', 10 );
add_action( 'edit_user_created_user', 'add_shop_url_for_seller' , 10, 2);
// Add this filter
add_filter( 'user_profile_update_errors', 'check_fields_add_new_user', 10, 3 );
function add_shop_url_for_seller( $user_id, $notify ) {
if ( ! $user_id ) {
return;
}
$user = new WP_User( $user_id );
if ( isset( $_POST['dokan-admin-add-new-user-store-url'] ) && $_POST['dokan-admin-add-new-user-store-url'] == 'yes' ) {
if ( in_array( 'seller', $user->roles ) ) {
if ( isset( $_POST['shopurl'] ) && !empty( $_POST['shopurl'] ) ) {
wp_update_user( array( 'ID' => $user_id, 'user_nicename' => sanitize_title( $_POST['shopurl'] ) ) );
}
}
}
}
function check_fields_add_new_user( $errors, $update, $user ) {
$role = $_POST['role'];
if ( isset( $_POST['dokan-admin-add-new-user-store-url'] ) && $_POST['dokan-admin-add-new-user-store-url'] == 'yes' ) {
if ( $role == 'seller' ) {
if ( isset( $_POST['shopurl'] ) && empty( $_POST['shopurl'] ) ) {
$errors->add( 'shop_url', __( '<strong>ERROR</strong>: Shop url required for seller.', 'dokan' ) );
}
}
}
return $errors;
}
function add_seller_store_field( $flag ) {
if ( 'add-new-user' != $flag ) {
return;
}
?>
<table class="form-table seller-store-table">
<tr>
<th><label for="seller-url"><?php _e( 'Shop URL', 'dokan' ); ?></label></th>
<td class="form-row">
<input type="text" class="input-text form-control" name="shopurl" id="seller-url"/>
<input type="hidden" name="dokan-admin-add-new-user-store-url" value="yes">
<strong id="url-alart-mgs"></strong>
<small><?php echo home_url() . '/' . dokan_get_option( 'custom_store_url', 'dokan_general', 'store' ); ?>/<strong id="url-alart"></strong></small>
</td>
</tr>
</table>
<style>
table.seller-store-table td.form-row small{
display: block;
margin-top: 4px;
}
table.seller-store-table td.form-row #url-alart-mgs.text-danger {
margin-left: 4px;
color: #b94a48;
}
table.seller-store-table td.form-row #url-alart-mgs.text-success {
margin-left: 4px;
color: #468847;
}
</style>
<script>
;(function($){
var xhr ,timeout;
$('table.seller-store-table').on('keyup', '#seller-url', function(e) {
$('#url-alart').text( $(this).val() );
});
$('table.seller-store-table').on('keyup', '#seller-url', function() {
var self = $(this),
ajaxUrl = '<?php echo admin_url( 'admin-ajax.php' ); ?>'
data = {
action : 'shop_url',
url_slug : self.val(),
_nonce : '<?php echo wp_create_nonce( 'dokan_reviews' ); ?>'
};
if ( self.val() === '' ) {
$( 'input#createusersub' ).attr('disabled', false);
$( '#url-alart-mgs' ).html('');
return;
}
$( 'input#createusersub' ).attr('disabled', true);
// var row = self.closest('.form-row');
// row.block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } });
clearTimeout(timeout);
if( xhr ) {
xhr.abort();
}
timeout = setTimeout(function(){
xhr = $.post( ajaxUrl, data, function(resp) {
if ( resp == 0){
$('#url-alart').removeClass('text-success').addClass('text-danger');
$('#url-alart-mgs').removeClass('text-success').addClass('text-danger').text('Not Available');
} else {
$('#url-alart').removeClass('text-danger').addClass('text-success');
$('#url-alart-mgs').removeClass('text-danger').addClass('text-success').text('Available');
$( 'input#createusersub' ).attr('disabled', false);
}
} );
},150);
});
})(jQuery)
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment