wordpress 扩展user,注册时可以显示这些extend fields

本文介绍如何使用WordPress通过自定义函数扩展用户注册和编辑时的额外字段,并覆盖默认的行为,包括添加自定义JS和CSS,修改注册页面的结构等。
<?php
/**
 *extends user fields(register/user profile)
 */
 
//注册时,扩展的注册表字段
add_action('register_form','f_user_extends_fields');
//注册时,检测这些字段
add_action('register_post','f_check_user_extends_fields',10,3);
//注册时,保存这些字段
add_action('user_register', 'f_user_extends_fields_save',10,1);
//admin 编辑user的信息时,显示这些字段
add_action('edit_user_profile','f_user_edit_extends_fields',10,1);
//user 编辑自己的信息时,显示这些字段
add_action('show_user_profile','f_user_edit_extends_fields',10,1);
//admin 编辑user的信息时,保存这些字段
add_action('edit_user_profile_update', 'f_user_extends_fields_save', 10, 1);
//user 编辑自己的信息时,保存这些字段
add_action('personal_options_update', 'f_user_extends_fields_save', 10, 1);
//注册时,添加自定义的js和css
add_action('login_head', 'f_login_scripts_css');
//修改注册页面的footer,可以在这里实现利用js改变注册(login deng)页面的html结构
add_action('login_footer', 'f_login_footer');
//修改login(注册等)页面的logo的连间
add_filter('login_headerurl', 'f_login_headerurl');
//修改login(注册等)页面的message信息
add_filter('login_message', 'f_login_message');
function f_login_message($message){
	global $action;
	if($action === 'register'){
		return '<p class="message register"><span>'.__('Register').'</span></p>';
	}
}
function f_login_headerurl($url){
	$url = get_option('siteurl');
	return $url; 
}
function f_login_footer(){
	global $action;
?>
	<div class="f_login_footer">
		<div class="f_login_footer copyright">
		xxxx
		</div>
		<div class="f_flow_footer">
			xxxx
		</div>
	</div>
	<script>
	jQuery('#login').html( '<div class="login_box login_box2">' + jQuery('#login').html() + '</div>');
	
	jQuery('#registerform p label').each(function(i,o){
		var r_input_c = jQuery(o).children();
		if(r_input_c[0].tagName == 'BR'){
			jQuery(r_input_c[0]).remove();
			jQuery(o).after(r_input_c[1]);
		}
		
	});
	</script>
<?php
}
function f_login_scripts_css(){
	echo '<script type="text/javascript" src="/wp-includes/js/jquery/jquery.js?ver=1.8.3"></script>';
	echo '<link rel="stylesheet" type="text/css" href="/wp-content/themes/xxxxx/login.css"></link>';
}
function f_user_extends_fields(){
	$http_post  = ('POST' == $_SERVER['REQUEST_METHOD']);
	$nombre     = '';
	if ( $http_post ) {
		$nombre    = trim($_POST['nombre']);
		
	}
?>
<p>
	<label for="nombre"><?php _e('Nombre') ?><br />
	<input type="text" name="nombre" id="nombre" class="input" size="20" value="<?php echo $nombre; ?>" />
	</label>
</p>
<?php
}

function f_check_user_extends_fields($login, $email, $errors){
	$nombre    = trim($_POST['nombre']);
	if( $nombre == ''){
		$errors->add('nombre', "<strong>ERROR</strong>: Nombre is Null");
	}
}

function f_user_extends_fields_save($user_id){
	$nombre    = trim($_POST['nombre']);
	
	update_user_meta($user_id, 'nombre', $nombre);
}

function f_user_edit_extends_fields($profileuser){
	$nombre     = get_user_meta($profileuser->ID, 'nombre', true);
?>

<table class="form-table">
<tr>
	<td colspan="2"><?php _e('Personal') ?></td>
</tr>
<tr>
	<th>
	<label for="nombre"><?php _e('Nombre') ?></label></th>
	<td>
	<input type="text" name="nombre" id="nombre" class="input" size="20" value="<?php echo $nombre; ?>" />

	</td>
</tr>
</table>
<?php
}

Creating a WooCommerce reseller plugin involves several steps. Below is a structured guide to help you build a basic reseller system. This guide assumes you have a working knowledge of WordPress/WooCommerce development, PHP, and MySQL. --- ### **1. Plugin Structure** Create a new folder in `wp-content/plugins/` (e.g., `reseller-plugin`), and add the following files: - `reseller-plugin.php` (Main plugin file) - `includes/` (Folder for helper functions) - `admin/` (Admin-facing code) - `public/` (Frontend-facing code) --- ### **2. Core Features** Your plugin should include: - **Reseller Registration/Management** - **Commission Calculation** - **Product Assignment to Resellers** - **Order Tracking** - **Dashboard for Resellers** - **Payment Handling** --- ### **3. Step-by-Step Implementation** #### **Step 1: Register a Reseller User Role** Add a custom user role (`reseller`) with specific capabilities. ```php // In reseller-plugin.php function register_reseller_role() { add_role( 'reseller', 'Reseller', array( 'read' => true, 'edit_posts' => false, 'delete_posts' => false, 'manage_woocommerce' => true, 'view_woocommerce_reports' => true, ) ); } register_activation_hook(__FILE__, 'register_reseller_role'); ``` --- #### **Step 2: Add Reseller Commission Settings** Allow resellers to set their commission rate (e.g., in their profile). ```php // Add commission field to user profile function reseller_commission_field($user) { if (in_array('reseller', $user->roles)) { ?> <h3>Reseller Settings</h3> <table class="form-table"> <tr> <th><label for="commission_rate">Commission Rate (%)</label></th> <td> <input type="number" name="commission_rate" id="commission_rate" value="<?php echo esc_attr(get_user_meta($user->ID, 'commission_rate', true)); ?>" class="regular-text" min="0" max="100" step="0.1"> </td> </tr> </table> <?php } } add_action('show_user_profile', 'reseller_commission_field'); add_action('edit_user_profile', 'reseller_commission_field'); // Save commission rate function save_reseller_commission_field($user_id) { if (current_user_can('edit_user', $user_id)) { update_user_meta($user_id, 'commission_rate', sanitize_text_field($_POST['commission_rate'])); } } add_action('personal_options_update', 'save_reseller_commission_field'); add_action('edit_user_profile_update', 'save_reseller_commission_field'); ``` --- #### **Step 3: Assign Products to Resellers** Add a meta box to WooCommerce products to link them to a reseller. ```php // Add reseller dropdown to product editor function reseller_product_meta_box() { add_meta_box( 'reseller_product_meta', 'Reseller Settings', 'reseller_product_meta_callback', 'product', 'side', 'default' ); } add_action('add_meta_boxes', 'reseller_product_meta_box'); function reseller_product_meta_callback($post) { $resellers = get_users(array('role' => 'reseller')); $selected_reseller = get_post_meta($post->ID, '_reseller_id', true); ?> <label for="reseller_id">Assign to Reseller:</label> <select name="reseller_id" id="reseller_id" class="widefat"> <option value="">None</option> <?php foreach ($resellers as $reseller) : ?> <option value="<?php echo $reseller->ID; ?>" <?php selected($selected_reseller, $reseller->ID); ?>> <?php echo $reseller->display_name; ?> </option> <?php endforeach; ?> </select> <?php } // Save reseller assignment function save_reseller_product_meta($post_id) { if (isset($_POST['reseller_id'])) { update_post_meta($post_id, '_reseller_id', absint($_POST['reseller_id'])); } } add_action('save_post_product', 'save_reseller_product_meta'); ``` --- #### **Step 4: Calculate Commission on Order Completion** Hook into WooCommerce order completion to calculate commissions. ```php function calculate_reseller_commission($order_id) { $order = wc_get_order($order_id); foreach ($order->get_items() as $item) { $product_id = $item->get_product_id(); $reseller_id = get_post_meta($product_id, '_reseller_id', true); if ($reseller_id) { $commission_rate = get_user_meta($reseller_id, 'commission_rate', true); $total = $item->get_total(); $commission = ($total * $commission_rate) / 100; // Store commission in a custom table or option update_user_meta($reseller_id, 'pending_commission', $commission, true); } } } add_action('woocommerce_order_status_completed', 'calculate_reseller_commission'); ``` --- #### **Step 5: Create a Reseller Dashboard** Add a shortcode for resellers to view their earnings and products. ```php // Shortcode for reseller dashboard function reseller_dashboard_shortcode() { if (!current_user_can('reseller')) return; $reseller_id = get_current_user_id(); $commission = get_user_meta($reseller_id, 'pending_commission', true); $products = get_posts(array( 'post_type' => 'product', 'meta_key' => '_reseller_id', 'meta_value' => $reseller_id, )); ob_start(); ?> <div class="reseller-dashboard"> <h2>Your Commission: $<?php echo $commission; ?></h2> <h3>Your Products</h3> <ul> <?php foreach ($products as $product) : ?> <li><?php echo $product->post_title; ?></li> <?php endforeach; ?> </ul> </div> <?php return ob_get_clean(); } add_shortcode('reseller_dashboard', 'reseller_dashboard_shortcode'); ``` --- #### **Step 6: Handle Payouts (Manual for Now)** Create an admin page to mark commissions as paid. ```php // Add admin menu for reseller payouts function reseller_payouts_menu() { add_submenu_page( 'users.php', 'Reseller Payouts', 'Payouts', 'manage_options', 'reseller-payouts', 'reseller_payouts_page' ); } add_action('admin_menu', 'reseller_payouts_menu'); function reseller_payouts_page() { // Logic to display and process payouts } ``` --- ### **4. Extend Functionality** - **Automated Payouts**: Integrate PayPal/Stripe API for automatic payments. - **Reports**: Use WooCommerce's `WC_Admin_Report` class for sales reports. - **Multi-Level Resellers**: Add support for tiers (e.g., resellers can have sub-resellers). --- ### **5. Testing & Security** - Test with different user roles and product types. - Sanitize all inputs and use nonces for forms. - Use WordPress transients for caching commission data. --- ### **6. Documentation** - Provide setup instructions for users. - Explain how resellers can manage their products and track earnings. --- ### **Final Notes** This is a basic framework. For a production plugin, you’ll need to: - Add error handling. - Optimize database queries. - Follow WordPress coding standards. - Consider using namespaces and autoloading. Would you like me to elaborate on any specific part (e.g., automated payouts, reporting, or security)? 中文翻译原文输出
07-31
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值