BuddyDev

Search

[Resolved] Adding a gallery automatically on post creation

  • Participant
    Level: Master
    Posts: 413
    Venutius on #12642

    Hi Brajesh, Happy New Year!

    This winter I have decided t get much more into programming in order to better customise my sites. I’ve written my first plugin which adds a new custom post type with custom fields to my archaeological website together with additional taxonomies specifically so I can add site specific posts to my site.

    I am considering trying to create another plugin to work with these customer posts in order to better integrate Mediapress, so I’m looking for your advice as to how easy this will be to create.

    The new plugin would provide a checkbox within a newly created post – “Add Gallery for this Site”.

    The idea is that when the user first creates a post for a new site, checking this box would automatically create a new gallery with the name name as the post, and automatically link the gallery to the post so that immediately following the publication of the new site post the gallery would be displayed at the end of the post content.

    Currently to create the same effect our users have to manually create a new gallery and then add the shortcode to the site post they have created. I’d like to avoid this.

    As I see it there are two aspects of this process I’d have to deal with:

    1. The automatic creation of the Mediapress Gallery
    2. Linking the gallery to the new site post.

    I’m looking for your advice as to how easy this would be to do? I’m thinking I’d need to create a script to create the gallery and add a correctly formatted shortcode into a custom field created specifically to hold that data.

    What do you think, would this be a reasonably straightforward task?

    Thanks in advance.

  • Keymaster
    (BuddyDev Team)
    Posts: 21510
    Brajesh Singh on #12647

    Hi George,
    Happy New year 🙂

    Welcome to the world of Programming. I am sure It will be an awesome experience and very helpful for your suture projects.

    Also, Congratulations on creating your first plugin 🙂

    About the Gallery creation, You will need to decide the following:-

    component:-
    members – the gallery appears on user profile on WordPress
    groups- The gallery will appear on a group depending on component_id
    sitewide – It is site gallery. Suitable for non BuddyPress sites.

    component_id:-
    – It depends on the component. It should be the user id for the ‘members’ or ‘sitewide’ and group id for the ‘groups’ gallery

    type :- possible values ‘photo’, ‘video’, ‘doc’, ‘audio’ determines which type of gallery you want to create

    status:- any of the available/applicable statuses. Could be ‘public’, ‘loggedin’ etc.

    and then there are attributes for the title/slug etc.

    If you can determine the above, you can use mpp_create_gallery() to automatically create a gallery. We do it for activity uploads.

    You can see an example here.
    https://github.com/buddydev/mediapress/blob/master/modules/buddypress/activity/mpp-activity-hooks.php#L166

    Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 413
    Venutius on #12661

    Thanks for the reply Brajesh, yes that does not look anywhere near as scary as I was expecting.

    Component – Sitewide
    Type – photo
    Status – this will be variable, however I may deal with this in a different way. One of the features I am trying to introduce with this is the idea of site research posts as having the option of access to the content being restricted to only the author or the author and a few members. I’m investigating using the User Specific Content plugin to achieve this. So I think the gallery would be created as public and then I’d modify User Specific Content to include MPP in it’s content filters. Unless you can suggest an alternative?

    Slug – Created from the post title I assume like the slug for the post would be?

  • Keymaster
    (BuddyDev Team)
    Posts: 21510
    Brajesh Singh on #12666

    Hi George,
    You are welcome.

    The API for MediaPress actions are very simple(and I have spent a lot of time making things easier to extend).

    Yes, we can use the slug from the post’s slug. Should I post an example ?

    Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 413
    Venutius on #12668

    Would you believe just as I started writing the code my hosts FTP server has failed! 🙁

    Yes a code example would be great. I’m guessing the first thing I need to understand is how to get the new gallery id as part of the new post setup in anticipation of creating the new gallery.

  • Keymaster
    (BuddyDev Team)
    Posts: 21510
    Brajesh Singh on #12677

    Sorry about the FTP, I am sure it is back now and it was hopefully just a coincidence 🙂

    I will post an example today.

    Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 413
    Venutius on #12679

    Yep it’s back now

  • Participant
    Level: Master
    Posts: 413
    Venutius on #12708

    Hi Brajesh, just checking in to see if you have had a chance to look at this? While I’ve been waiting I’ve just about completed forking another plugin – I’ve created a version of duplicate title validate that supports MediaPress and any other custom post type with options on exactly how it validates post titles. a simple plugin but none the less a steep learning curve for me. I’m getting keen to get to work on my MediaPress project now.

    All the best!

    George

  • Keymaster
    (BuddyDev Team)
    Posts: 21510
    Brajesh Singh on #12711

    Hi George,
    I am sorry, I could not post the code earlier. Will do today(in next couple of housr when I get to work on my system again).

    Congratulations. Learning is fun and I am sure you are enjoying it 🙂

    Will love to see you extend MediaPress and will be ready to assist all the time 🙂

    Best regards
    Brajesh

  • Keymaster
    (BuddyDev Team)
    Posts: 21510
    Brajesh Singh on #12717

    Hi George,
    Here is an example that creates a gallery each time a post is published

    
    
    /**
     * Auto Create Custom photo gallery when new post is created.
     *
     * @param int $post_id post id.
     * @param WP_Post $post post object.
     */
    function mpp_custom_create_update_associated_gallery( $post_id, $post ) {
    	// Only test for already published posts.
    	if ( 'publish' != $post->post_status ) {
    		return;
    	}
    
    	// we only want to create gallery for 'post' post type
    	// change 'post' with your own post type name.
    	if ( 'post' != $post->post_type ) {
    		return;
    	}
    
    	$associated_gallery_id = get_post_meta( $post_id, '_mppc_associated_photo_gallery_id', true );
    
    	$user_id = $post->post_author;// or should we use get_current_user_id() ?
    	if ( $associated_gallery_id ) {
    		// wht do you want to do?
    		//
    		return;
    	}
    
    	// if we are here, the gallery does not exist, let us create it.
    
    	$gallery_id = mpp_create_gallery( array(
    		'creator_id'   => $user_id,
    		'title'        => $post->post_title,
    		'description'  => '',
    		'status'       => 'public',
    		'component'    => 'sitewide',
    		'component_id' => $user_id,
    		'type'         => 'photo',
    	) );
    
    	// save a reference to post in gallery meta?
    	// and a reference of gallery to post meta?
    	if ( $gallery_id ) {
    		mpp_update_gallery_meta( $gallery_id, '_mppc_associated_post_id', $post_id );
    		update_post_meta( $post_id, '_mppc_associated_photo_gallery_id', $gallery_id );
    	}
    }
    
    add_action( 'save_post', 'mpp_custom_create_update_associated_gallery', 10, 2 );
    

    You can update it to match for your own post type.
    Hope it helps.

    Regards
    Brajesh

The topic ‘ [Resolved] Adding a gallery automatically on post creation’ is closed to new replies.

This topic is: resolved