BuddyDev

Search

[Resolved] Custom Gallery From BuddyPress Fields

  • Participant
    Level: Initiated
    Posts: 12
    Paul on #2792

    I’ve looked around for a way to programmatically add images and other info to a mediapress galleryt from buddypress fields. I’ve looked into mpp_media_query but I’m not completely sure if that is the correct way or not and when I try to use it I get wsod. I am a experienced Drupal developer but I’ve been given this WordPress task so maybe there is something very obvious I’m missing. Where would I put the code for MPP_Media_Query? It breaks the site in a custom plugin and functions.php. If using MPP_Media_Query is not the best way to go about this I would love to hear any suggestions. Thank You.

  • Keymaster
    (BuddyDev Team)
    Posts: 24231
    Brajesh Singh on #2796

    Hi Paul,
    Welcome to BuddyDev.

    I am sorry but I don’t understand the question completely.

    MPP_Media_Query is used to list the media.

    To add a media you will use mpp_add_media() and to add a gallery you will use mpp_create_gallery()

    If you can explain a little more what you are trying to accomplish, I am be able to help better.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 12
    Paul on #2802

    Brajesh, thank you so much for the quick reply. I have a buddypress site with members via memberpress. I need to make sure they are a paying member and if they are add images and a few other fields from their buddypress profile to a custom gallery. In a new plugin I have tried the following code as a test but it breaks the site:
    add_action(‘mpp_loaded’, ‘add_member_images’);
    function add_member_images() {

    $args = array(
    ‘gallery_id’ => 292,
    ‘title’ => ‘testing’,
    ‘type’ => ‘image’,
    );

    $ret = mpp_add_media($args);

    }

    I assume I pass the image and other fields as args? If so how do I specify the image? Maybe I way off here? I would appreciate any help.

  • Keymaster
    (BuddyDev Team)
    Posts: 24231
    Brajesh Singh on #2805

    Hi Paul,
    Thank you for the deails.

    Here is an example how we add the media

    
    $media_data = array(
    				'title'				=> $title,
    				'description'		=> $content,
    				'gallery_id'		=> $gallery_id,
    				'user_id'			=> get_current_user_id(),
    				'is_remote'			=> false,
    				'type'				=> $media_type,
    				'mime_type'			=> $type,
    				'src'				=> $file,
    				'url'				=> $url,
    				'status'			=> $status,
    				'comment_status'	=> 'open',
    				'storage_method'	=> mpp_get_storage_method(),
    				'component_id'		=> $component_id,
    				'component'			=> $component,
    				//'context'			=> $context,
    				'is_orphan'			=> 0,
    			);
    
    			$id = mpp_add_media( $media_data );
    
    

    where
    – type can be ‘photo’, ‘audio’, ‘video’ etc
    – component can be ‘members’, ‘groups’ etc
    -context csn be ignored

    -component_id – is user id or group id
    -src is the absolute path of the file on the file system
    -url – is absolute url of the file
    -mime_type is the mime type
    -status – can be ‘public’, ‘private’ etc

    Please do not call the mpp_add_media on mpp_loaded. Post types are only registered after mpp_init
    So, It will be better to call at ‘mpp_actions’ or anything after mpp_init’ or WordPress init hook.

    Hope that helps.

  • Participant
    Level: Initiated
    Posts: 12
    Paul on #2839

    Hello Brajesh,

    Thank you for the very helpful reply. The following code does add the image to the gallery but with a few problems.

    1. Clicking on the image does not work, I get an “The page isn’t redirecting properly” error.
    2. I get wsod when running the script. After I comment out $id = mpp_add_media( $media_data ); and return to the gallery the image is there.

    I will also need to roll through the images in the gallery to remove unwanted images.

    add_action(‘init’, ‘add_member_images’);
    function add_member_images() {

    $title = ‘testing image’;
    $content = ‘checking this out’;
    $gallery_id = 292;
    $media_type = ‘photo’;
    $type = ‘image/jpeg’;
    $url = ‘http://sisters.dev/wp-content/uploads/profiles/4/large_22097.jpg’;
    $file = ‘/wp-content/uploads/profiles/4/large_22097.jpg’;
    $status = ‘public’;
    $component_id = 1;
    $component = ‘members’;

    $media_data = array(
    ‘title’ => $title,
    ‘description’ => $content,
    ‘gallery_id’ => $gallery_id,
    ‘user_id’ => get_current_user_id(),
    ‘is_remote’ => false,
    ‘type’ => $media_type,
    ‘mime_type’ => $type,
    ‘src’ => $file,
    ‘url’ => $url,
    ‘status’ => $status,
    ‘comment_status’ => ‘open’,
    ‘storage_method’ => mpp_get_storage_method(),
    ‘component_id’ => $component_id,
    ‘component’ => $component,
    //’context’ => $context,
    ‘is_orphan’ => 0,
    );

    //$id = mpp_add_media( $media_data );

    }

    Thank You,
    Paul

    • This reply was modified 8 years, 2 months ago by Paul.
  • Keymaster
    (BuddyDev Team)
    Posts: 24231
    Brajesh Singh on #2841

    Hi Paul,
    Here is a completely working code that you can use. It is tested to work.

    
    
    function add_member_images() {
    	//Include image.php for helper functions
    	require_once ABSPATH . 'wp-admin/includes/image.php';
    
    	$title        = 'Nature is great';
    	$content      = 'checking this out';
    	$gallery_id   = 292; //change
    	$media_type   = 'photo';
    	$type         = 'image/jpeg';
    	$url          = 'http://localhost/4.4.2/wordpress/wp-content/uploads/15-beach-sea-photography.jpg';
    	$file         = '/wp-content/uploads/15-beach-sea-photography.jpg';
    	$status       = 'public';
    	$component_id = 1;
    	$component    = 'members';
    
    	$media_data = array(
    		'title'          => $title,
    		'description'    => $content,
    		'gallery_id'     => $gallery_id,
    		'user_id'        => get_current_user_id(),
    		'is_remote'      => false,
    		'type'           => $media_type,
    		'mime_type'      => $type,
    		'src'            => $file,
    		'url'            => $url,
    		'status'         => $status,
    		'comment_status' => 'open',
    		'storage_method' => mpp_get_storage_method(),
    		'component_id'   => $component_id,
    		'component'      => $component,
    		//'context' => $context,
    		'is_orphan'      => 0,
    	);
    
    	$id = mpp_add_media( $media_data );
    }
    
    add_action('mpp_actions', 'add_member_images');
    
    

    Please update the fields as you need. It was giving fatal error because we did not include image.php which has some required functions needed for processing media.

    Hope that helps.

  • Participant
    Level: Initiated
    Posts: 12
    Paul on #2849

    Thank you so much for the thorough and quick replies. Exactly what I needed.

  • Keymaster
    (BuddyDev Team)
    Posts: 24231
    Brajesh Singh on #2852

    You are most welcome. Glad it is working. Marking the topic as resolved.

The topic ‘ [Resolved] Custom Gallery From BuddyPress Fields’ is closed to new replies.

This topic is: resolved