BuddyDev

Search

Pragmatically create mediapress Gallery!

Tagged: ,

  • Participant
    Level: Initiated
    Posts: 20
    karamveer on #49656

    Hi Support,

    I’m a WordPress developer & was creating an API for creating Mediapress galleries(Multisite) which are almost developed. But was struggling to map the photos with Mediapress gallery, could you guide me to resolve such challenges?

    My code works fine with a Single site but with a multisite, it is not.

    Code snippet

    $attach_id = wp_insert_attachment( $attachment, $filename,$gallery_id );
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
    wp_update_attachment_metadata( $attach_id, $attach_data );
    mpp_update_media_meta( $attach_id, '_mpp_is_mpp_media', 1 );
    mpp_update_media_meta( $attach_id, '_mpp_component_id', 1 );
    mpp_import_attachment($attach_id,$gallery_id);

    Let me know if any more clarification is required to describe the problem, looking forward to your valuable suggestion as early as possible.

    Regards,
    Karamveer Singh

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #49657

    Hi Karamveer,
    Welcome to BuddyDev support forums.

    Please try using switch_to_blog($blog_id) and restore_current_blog() before/after your code to use the specific blog where MediaPress is active.
    In case of Multisite, MediaPress is probably active on your site/blog where BuddyPress was activated. Most of the time, It is blog with id 1 but may depend if you have a a different sub site/blog as main site of the network.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 20
    karamveer on #49658

    Hi Brajesh,

    Thank you for your quick response—I really appreciate you getting back to me so soon. About your suggestion already tried same in my logic, see below the complete snippet of the function.

    
    function imagecreateprogram(){
    	switch_to_blog(3);
    	$gallery_id = 5;
    	
    	mpp_import_attachment('10',$gallery_id);
    	
    		require_once ABSPATH . 'wp-admin/includes/image.php';
    		
    		$storage = mpp_local_storage();
    		// destination.
    		$uploads = $storage->get_upload_dir( array(
    			'component'    => 'sitewide',
    			'component_id' => 1/* $gallery->component_id */,
    			'gallery_id'   => $gallery_id,
    		) );
    		
    		$imagename = 'karamveer.jpg';
    		
    		$image_url = $uploads['url'] . '/'.$imagename;
    
    		//$image_data = file_get_contents( $image_url );
    		
    		$filename = basename( $image_url );
    
    		// if destination directory does not exists, try creating it.
    		if ( ! file_exists( $uploads['path'] ) ) {
    			wp_mkdir_p( $uploads['path'] );
    		}
    
    		if ( file_exists( $uploads['path'] ) ) {
    			  $file = $uploads['path'] . '/' . $filename;
    		} else {
    			  $file = $uploads['basedir'] . '/' . $filename;
    		}
    
    	
    	$wp_filetype = wp_check_filetype( $filename, null );
    	
    
    	$wp_gallery_id = $gallery_id;
    	$photoTitle = 'Test Title';
    	$customerID = 1;
    	$photoDesc = 'Photo Description';
    	$attachment = array(
    		'post_mime_type' => $wp_filetype['type'],
    		'post_title'	 => $photoTitle,
    		/* 'post_date'		 => $createdDate, */
    		'post_parent'	 => $wp_gallery_id,
    		'post_author'	 => $customerID,
    		'post_content'	 => $photoDesc,
    		'post_status' 	 => 'inherit',
    	);
    			
    	$attachment = sanitize_post( $attachment, 'db' );
    	
    	 $image_subdir_path = $uploads['subdir'] . '/' . $filename;
    	
    	
    	$attach_id = wp_insert_attachment( $attachment, ltrim($image_subdir_path, '/'),$gallery_id );
    	require_once( ABSPATH . 'wp-admin/includes/image.php' );
    	$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
    	wp_update_attachment_metadata( $attach_id, $attach_data );
    	
    	mpp_update_media_meta( $attach_id, '_mpp_is_mpp_media', 1 );
    	mpp_update_media_meta( $attach_id, '_mpp_component_id', 1 );
    	mpp_update_media_meta( $attach_id, '_wp_attached_file', $filename );
    	mpp_import_attachment($attach_id,$gallery_id); // not working
    	return rest_ensure_response($attach_id);
    
    }
    
    • This reply was modified 1 year, 5 months ago by karamveer.
    • This reply was modified 1 year, 5 months ago by karamveer.
  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #49661

    Hi Karamveer,
    You are welcome.
    Thank you for sharing the snippet. I don’t think you need that much code to achieve this task.

    Before I assist you further, please share me the following details:-

    1. Is it a normal multisite with BuddyPress network active or do you have BuddyPress in Multi Network mode(BuddyPress behaves to be available on all sites and directory/profile is available from all of the sites).

    2. Or is BuddyPress just active on a single blog/site on the network?
    3. On which blog BuddyPress and MediaPress are active?
    4. Is MediaPress network active or just active on a single site of the network?

    5. Is the gallery and the media you want to add to the gallery available on same site?
    6. If the answer to previous question is no, What is the source of media? Do you want to import an existing attachment to gallery or do you want to import a media from somewhere on the web to the gallery?

    7. or is there any chance that you have some way to upload your media and you want to add it to gallery after you have handled the upload on you own?

    Please share the details and I will assist.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 20
    karamveer on #49671

    Hi Brajesh,

    Thank you for your continued support. About your queries kindly look underneath my comments and let me know if there is any further information required.

    1. Is it a normal multisite with BuddyPress network active or do you have BuddyPress in Multi Network mode(BuddyPress behaves to be available on all sites and directory/profile is available from all of the sites).
    Answer: Yes our website is running on multisite with BuddyPress in Multi Network mode.Our both plugins(MediaPRess/BuddyPress) are configured from Network Admin.

    2. Or is BuddyPress just active on a single blog/site on the network?
    Answer: No, It’s being activated by the Network admin and enabled under all sites.

    3. On which blog BuddyPress and MediaPress are active?
    Answer: We have 2 blogs(sites) and in both buddypress/mediapress plugin is activated.

    4. Is MediaPress network active or just active on a single site of the network?
    Answer: MediaPress is network active.

    5. Is the gallery and the media you want to add to the gallery available on same site?
    Answer:: Yes, the gallery and the media we want to add, are available on the same site.

    6. If the answer to the previous question is no, What is the source of media? Do you want to import an existing attachment to gallery or do you want to import a media from somewhere on the web to the gallery?
    Answer: Answer to all above question is Yes, We uploaded the media in uploads(see next reply for exact path) via FTP and with API trying to attach it to WordPress Media and Media press gallery.

    7. or is there any chance that you have some way to upload your media and you want to add it to gallery after you have handled the upload on you own?
    Answer: Yes right, we already have media and placed there inside the site folders which we’re mapping by the API to WP. Both 2 sites’ media folders path look like this – \wp-content\uploads\sites\2 , \wp-content\uploads\sites\3

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #49673

    Hi Karamveer,
    Thank you.
    The issue is MediaPress should never be activated network wide. It can only be enabled per site basis.

    MediaPress uses WordPress attachment which are specific to your blogs(site) and the MediaPress settings need to be updated on individual blogs.

    In your use case, MediaPress may not be suitable if you need BuddyPress multi network mode. You can enable it on one of the sites if that works for you.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 20
    karamveer on #49676

    Hi Brajesh,

    Thanks for your reply but sorry to say not getting much of what you’re suggested.

    What I understand MediaPress- is not suitable for MultiSite/Network, please correct me.

    Regards,
    Karamveer Singh

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #49677

    Hi Karamveer,
    MediaPress is compatible with multisite. It does not support network activation or BuddyPress Multi Network mode.
    As a comparison, consider the use case of WooCommerce on multisite, MediaPress behaves in similar way.

    You can activate it on individual sites but not use in network activation mode.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 20
    karamveer on #49678

    Thanks, Brajesh, Let me try your suggested way and asap will get back to you with the results.

  • Participant
    Level: Initiated
    Posts: 20
    karamveer on #49679

    Hello Brajesh,

    As you suggested I enabled the Mediapress on individual site basis and tried media uploading then assigning media/photos to Mediapress Gallery by using switch_to_blog(2);

    it uploading the images to media but still not assigning the media to required Gallery.

    do you have some reference code snippet to achieve it.

    *Activities performed*-

    1. Setup the fresh WP-MU
    2. Created 2 subsites
    3. Visited the individual single site and enabled the media press plugin
    4. Executed my custom script to sync the media but still the same result. Images are saved into the media but not mapped with the Gallery. The gallery is showing empty no media attached/thumbs

    Regards,
    Karamveer Singh

    • This reply was modified 1 year, 5 months ago by karamveer.

You must be logged in to reply to this topic.

This topic is: not resolved