How can I add an image field to BuddyPress Extended Profile Fields? [closed]
IT Nursery
May 27, 2022
0
I’m working with the BuddyPress extended profile component and would like to add image fields to the WordPress user profile. Is there code that I can add to my theme’s functions.php so it inserts the “Image” option into the plugin?
I have searched the BuddyPress and WordPress Codex forums and haven’t come across code for intercepting the plugin’s operation without editing core files.
1 Answer 1
I figured out a way to go about adding the image type field without editing core BuddyPress files. The solution involves adding code to the theme’s functions.php that intercepts the plugin’s loading, display and saving. It also updates the WordPress upload location in order to direct the profile images to a subdirectory of the the uploads directory. The full solution is here.
Step 4.
Access the WordPress registered hook functions to remove the BuddyPress profile saving handler and insert a new one. This is necessary in order to handle the saving of the custom field before passing control back to BuddyPress.
Step 7.
Create a javascript file that shall hold code for updating the field type selection drop-down to insert the new field type when the profile front-end is displayed. Let’s call the file xprofile-image.js and save it in the same location as the theme’s functions.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);