BuddyDev

Search

Need some help with some code

  • Participant
    Level: Initiated
    Posts: 7
    Andrew on #16321

    What I need the code to do
    1. Create a gallery “With my defined UserID”
    2. Upload Images “With my defined UserID”
    3. I am pretty sure the code isnt setting everything up for the gallery “On the fronted the gallery is blank except the popup light box”

     include "rapid-addon.php";
    
    $mediapress_addon = new RapidAddon('Media Press Gallery', 'mediapress_addon');
    
    add_action('pmxi_saved_post', 'setup_mediapress_gallery', 10, 1);
    
    $mediapress_addon->run();
    
    function setup_mediapress_gallery( $post_id ) {
    	$component = array ('_sitewide');
    	$type = array ('_photo');
    	$status = array ('_public');
    	$component_id = 3709;
    
    	wp_set_post_terms( $post_id, $component, mpp_get_component_taxname() );
    	wp_set_post_terms( $post_id, $type, mpp_get_type_taxname() );
    	wp_set_post_terms( $post_id, $status, mpp_get_status_taxname() );
    	mpp_update_gallery_meta( $post_id, '_mpp_component_id', $component_id );
    	do_action( 'mpp_gallery_created', $post_id );
    
    	// Now lets handle the images
    	mediapress_add_images( $post_id );
    
    }
    
    function mediapress_add_images( $gallery_post_id ){
    
    // Find the media library files attached to the gallery
    $children_args = array(
      'numberposts' => -1,
      'post_parent' => $gallery_post_id,
      'post_type' => 'attachment',
     );
    $images = get_children( $children_args );	
    
    	require_once ABSPATH . '/wp-admin/includes/image.php';
    
    	$image_count = 1;
    	foreach ( $images as $image ) {
    
    		$image_url = wp_get_attachment_url( $image->ID );
    		$file = get_attached_file( $image->ID );
    
    		$media_data = array(
    			'title'				=> $image->post_title,
    			'description'		=> '',
    			'gallery_id'		=> $gallery_post_id,
    			'user_id'			=> 3709,
    			'is_remote'			=> false,
    			'type'				=> 'photo',
    			'mime_type'			=> 'image/jpeg',
    			'src'				=> $file,
    			'url'				=> $image_url,
    			'status'			=> 'public',
    			'comment_status'	=> 'open',
    			'storage_method'	=> mpp_get_storage_method(),
    			'component_id'		=> 3709,
    			'component'			=> 'sitewide',
    			'context'			=> 'gallery',
    			'is_orphan'			=> 0,
    		);
    
    		// Add the image to the mediapress gallery
    		$new_media_id = mpp_add_media( $media_data );
    
    		mpp_gallery_increment_media_count( $gallery_post_id );
    
    		$image_count++;
    	}
    }
  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #16326

    Hi Andrew,
    Here are some of my suggestions.

    1. Instead of the WordPress API, Please use MediaPress API. It will make your job very easy and things will be setup automatically

    You can use ‘mpp_create_gallery’ to create a gallery using code. Please search for ‘mpp_create_gallery’ on the forum and you will find many example. Here is one.

    
    
    $component_id = 3709;
    $component = 'sitewide';
    
    $gallery_id = mpp_create_gallery( array(
    	'creator_id'   => get_current_user_id(),
    	'title'        => 'some title',
    	'description'  => '',
    	'status'       => 'public',
    	'component'    => $component,
    	'component_id' => $component_id,
    	'type'         => 'photo',
    ) );
    

    Also, For importing Media to a galelry, we have helper functions like mpp_import_file() , mpp_import_attachment() using which you can import a file from server or an existing WordPress attachment to MediaPress.

    Here is the signature for them

    
    mpp_import_attachment( $attchment_id, $gallery_id, $overrides = array() );
    
    mpp_import_file( $file_abs_path,$gallery_id, $overrides = array() );
    
    

    Both the function returns the imported media id or WP_Error object if there was a problem in importing.

    Example:-

    Import attachment id 32 to gallery id 7

    
    mpp_import_attachment( 32, 7  );
    
    

    That will import the attachment and setup all the properties of media from the given galery(privacy, status, type, component etc).

    If you want to override some propert, you can do it by passing 3rd argument. Let us assume that the gallery 7 is public but we want the imported media to be private, It can be easily accomplished by over riding status as

    
    
    mpp_import_attachment( $attchment_id, $gallery_id, array( 'status' => 'private' ) );
    
    

    Same applies for the mpp_import_file(). You can import any file from server disk. It create an attachment out of the file(Please note that mpp_import_file() only copies the file, does not remove it from the original directory).

    If you use these methods, the media will be setup properly and you won’t have issues in listing.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 7
    Andrew on #16328

    Ok I think I am getting it.

    Can you explain how I would run code like this.

    Say I want to have a php page where I dump a list of

    mpp_import_attachment( 25917, 41981 );
    mpp_import_attachment( 25919, 41981 );
    mpp_import_attachment( 26381, 41981 );
    mpp_import_attachment( 41988, 41981 );

    this would attach the 4 images to the gallery 41981 and set them up. This way I just go into the current setup and find a list of the attachments and re-attach them to the gallery.

    Is it possible to have a php page that I can open up and it executes these statements?

    Ultimately I would generate a list, load them onto a php page, then open the php page to execute.

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #16329

    Hi Andrew,
    My approach and suggestion will depend on how many media you want to import and how often/convenient it should be.

    If it needs human intervention, I will go for a simple approach that provides an UI for moving to gallery.

    If it can be automated, you can either create an array of media, gallery id and use a loop to automate it. Please do note that php time limits may not allow all to be imported in a go.

    Writing a plugin is better approach but you can put the code in a file and include it in functions.php

    Please make sure that you do not run the code before mpp_init action. The most suitable action for running the code will mpp_template_redirect if not using ajax actions.

You must be logged in to reply to this topic.

This topic is: not resolved