我找到了一种方法,可以在不编辑核心BuddyPress文件的情况下添加图像类型字段。解决方案包括向主题函数添加代码。拦截插件加载、显示和保存的php。它还更新WordPress上载位置,以便将配置文件图像定向到上载目录的子目录。The full solution is here.
第一步。添加新字段类型:
function bpd_add_new_xprofile_field_type($field_types){
$image_field_type = array(\'image\');
$field_types = array_merge($field_types, $image_field_type);
return $field_types;
}
add_filter( \'xprofile_field_types\', \'bpd_add_new_xprofile_field_type\' );
第2步。在WordPress管理面板中处理新字段类型的呈现
function bpd_admin_render_new_xprofile_field_type($field, $echo = true){
ob_start();
switch ( $field->type ) {
case \'image\':
?>
<input type="file" name="<?php bp_the_profile_field_input_name() ?>" id="<?php bp_the_profile_field_input_name() ?>" value="" />
<?php
break;
default :
?>
<p>Field type unrecognized</p>
<?php
}
$output = ob_get_contents();
ob_end_clean();
if($echo){
echo $output;
return;
}
else{
return $output;
}
}
add_filter( \'xprofile_admin_field\', \'bpd_admin_render_new_xprofile_field_type\' );
第3步。在WordPress前端处理新字段类型的呈现
function bpd_edit_render_new_xprofile_field($echo = true){
if(empty ($echo)){
$echo = true;
}
ob_start();
if ( bp_get_the_profile_field_type() == \'image\' ){
$imageFieldInputName = bp_get_the_profile_field_input_name();
$image = WP_CONTENT_URL . bp_get_the_profile_field_edit_value();
?>
<label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php _e( \'(required)\', \'buddypress\' ); ?><?php endif; ?></label>
<input type="file" name="<?php echo $imageFieldInputName; ?>" id="<?php echo $imageFieldInputName; ?>" value="" <?php if ( bp_get_the_profile_field_is_required() ) : ?>aria-required="true"<?php endif; ?>/>
<img src="<?php echo $image; ?>" alt="<?php bp_the_profile_field_name(); ?>" />
<?php
}
$output = ob_get_contents();
ob_end_clean();
if($echo){
echo $output;
return;
}
else{
return $output;
}
}
add_action( \'bp_custom_profile_edit_fields\', \'bpd_edit_render_new_xprofile_field\' );
第4步。访问WordPress注册的钩子函数以删除BuddyPress配置文件保存处理程序并插入一个新的处理程序。为了在将控制权传递回BuddyPress之前处理自定义字段的保存,这是必需的。
function bpd_override_xprofile_screen_edit_profile(){
$screen_edit_profile_priority = has_filter(\'bp_screens\', \'xprofile_screen_edit_profile\');
if($screen_edit_profile_priority !== false){
//Remove the default profile_edit handler
remove_action( \'bp_screens\', \'xprofile_screen_edit_profile\', $screen_edit_profile_priority );
//Install replalcement hook
add_action( \'bp_screens\', \'bpd_screen_edit_profile\', $screen_edit_profile_priority );
}
}
add_action( \'bp_actions\', \'bpd_override_xprofile_screen_edit_profile\', 10 );
第5步。创建自定义字段保存功能
function bpd_screen_edit_profile(){
if ( isset( $_POST[\'field_ids\'] ) ) {
if(wp_verify_nonce( $_POST[\'_wpnonce\'], \'bp_xprofile_edit\' )){
$posted_field_ids = explode( \',\', $_POST[\'field_ids\'] );
$post_action_found = false;
$post_action = \'\';
if (isset($_POST[\'action\'])){
$post_action_found = true;
$post_action = $_POST[\'action\'];
}
foreach ( (array)$posted_field_ids as $field_id ) {
$field_name = \'field_\' . $field_id;
if ( isset( $_FILES[$field_name] ) ) {
require_once( ABSPATH . \'/wp-admin/includes/file.php\' );
$uploaded_file = $_FILES[$field_name][\'tmp_name\'];
// Filter the upload location
add_filter( \'upload_dir\', \'bpd_profile_upload_dir\', 10, 1 );
//ensure WP accepts the upload job
$_POST[\'action\'] = \'wp_handle_upload\';
$uploaded_file = wp_handle_upload( $_FILES[$field_name] );
$uploaded_file = str_replace(WP_CONTENT_URL, \'\', $uploaded_file[\'url\']) ;
$_POST[$field_name] = $uploaded_file;
}
}
if($post_action_found){
$_POST[\'action\'] = $post_action;
}
else{
unset($_POST[\'action\']);
}
}
}
if(!defined(\'DOING_AJAX\')){
if(function_exists(\'xprofile_screen_edit_profile\')){
xprofile_screen_edit_profile();
}
}
}
第6步。覆盖WordPress上载目录位置以提供自定义图像保存位置
function bpd_profile_upload_dir( $upload_dir ) {
global $bp;
$user_id = $bp->displayed_user->id;
$profile_subdir = \'/profiles/\' . $user_id;
$upload_dir[\'path\'] = $upload_dir[\'basedir\'] . $profile_subdir;
$upload_dir[\'url\'] = $upload_dir[\'baseurl\'] . $profile_subdir;
$upload_dir[\'subdir\'] = $profile_subdir;
return $upload_dir;
}
第7步。创建一个javascript文件,该文件应包含更新字段类型选择下拉列表的代码,以便在显示概要文件前端时插入新的字段类型。让我们调用文件xprofile image。并将其保存在与主题函数相同的位置。php
(
function(jQ){
//outerHTML method (http://stackoverflow.com/a/5259788/212076)
jQ.fn.outerHTML = function() {
$t = jQ(this);
if( "outerHTML" in $t[0] ){
return $t[0].outerHTML;
}
else
{
var content = $t.wrap(\'<div></div>\').parent().html();
$t.unwrap();
return content;
}
}
bpd =
{
init : function(){
//add image field type on Add/Edit Xprofile field admin screen
if(jQ("div#poststuff select#fieldtype").html() !== null){
if(jQ(\'div#poststuff select#fieldtype option[value="image"]\').html() === null){
var imageOption = \'<option value="image">Image</option>\';
jQ("div#poststuff select#fieldtype").append(imageOption);
var selectedOption = jQ("div#poststuff select#fieldtype").find("option:selected");
if((selectedOption.length == 0) || (selectedOption.outerHTML().search(/selected/i) < 0)){
var action = jQ("div#poststuff").parent().attr("action");
if (action.search(/mode=edit_field/i) >= 0){
jQ(\'div#poststuff select#fieldtype option[value="image"]\').attr("selected", "selected");
}
}
}
}
}
};
jQ(document).ready(function(){
bpd.init();
});
}
)(jQuery);
第8步。加载js文件(xprofile image.js)
function bpd_load_js() {
wp_enqueue_script( \'bpd-js\', get_bloginfo(\'stylesheet_directory\') . \'/xprofile-image.js\',
array( \'jquery\' ), \'1.0\' );
}
add_action( \'wp_print_scripts\', \'bpd_load_js\' );
就是这样!WordPress用户配置文件现在通过BuddyPress扩展配置文件组件支持其他图像字段。
如果可以将其转换为插件,这将非常有用。
UPDATE:我开始创建插件,它可以在WordPress Plugins 页码: