• Resolved traveler

    (@visualeight)


    I have a need to create a custom plugin that will run in a custom post type’s admin/edit screen. Each of the custom post types corresponds to a client, and they each have the ACF image gallery field on their post admin/edit screen so images can be uploaded to this cpt and have a parent attachment relationship.

    I would like to add a simple plugin that allows me to select 6-8 images associated with this parent attachment (each client will own their own images, one client’s images will not be used with any other client), and have a button that will submit the selected images and create a different custom post type with the images selected. I feel comfortable retrieving the images on the front end, but I was wondering where the best place to start would be.

    ACF confirmed that this could be done if both post types shared the same field types, using this filter:

    
    <?php
    function my_acf_load_value( $value, $post_id, $field ) {
    $closetID = "some_value";
    if (!$value) {
    //get value from closet
    $value = get_field("closet_images", $closetID);
    update_field( 'order_images', $value, $post_id );
    
    //remove value from closet field
    update_field( 'closet_images', '', $closetID);
    }
    return $value;
    }
    add_filter('acf/load_value/name=order_images', 'my_acf_load_value', 10, 3);
    ?>
    

    So the client post would have 8 ACF image fields, and so would the other custom post type created from the images. Once the images are selected, one by one, using the ACF fields in the client page, I need to call the above filter that takes the images from one post type and creates a new post with the images. The add_filter() above makes sense to me in how it works, but I’m confused on how exactly to call this filter so that it takes the values and creates the other post.

    I know it’s not a typical workflow, but if it could work it would make my colleagues very happy. Any direction or advice I appreciate.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m unsure what that ACF filter is for, it sounds like it allows us to arbitrarily alter the gallery image order. If your goal is to create another post which uses images from that gallery, I’m not convinced it is the right filter.

    You speak of creating a new post one by one as images are selected. Don’t you want a single post for all the selected images? There is already an individual attachment post for each image. The selected images would be a subset of the entire gallery, correct?

    To save a selected subset of gallery images into a new post of whatever post type, here’s what I would do: Add jQuery code that executes anytime there’s a selection change among the gallery images. jQuery collects IDs of what is currently selected, along with the current post’s ID, and sends them to the server via Ajax. Also include the “whatever” post ID contained in a different custom field, if one exists. The Ajax handler then updates the whatever post’s ACF gallery field based on the passed IDs.

    If the source post has no “whatever” post ID value, the Ajax handler should create a new whatever post, add an ACF gallery field, and set the field’s values. The whatever post’s ID is also set in the current post’s custom field established for the purpose of relating the two different post types to each other.

    In short, changing what is selected triggers an Ajax request which does what’s needed based on the current selection.

    Thread Starter traveler

    (@visualeight)

    Hey bcworkz, thank you for clarifying that.

    Yes, you are correct, a single post would be created to hold the selected images (6-8 images selected at a time). The images will be added to the ACF gallery for each client post, and also using the ACF gallery feature that only allows you to select images that were uploaded to the client’s post to keep client image galleries separate.

    The post containing the images, if this post is not yet created thus no post ID exists, the Ajax call to wp_insert_post() will automatically create the new post ID, correct?

    So the workflow would be:
    1. create client closet
    2. add images to ACF gallery field in the client closet post
    3. use the ACF image field, there would need to be 8 total (6-8 img/new post) to select the images
    4. have a jquery listener set up to grab the image ID, the current client post ID, and send it in an Ajax call to the server
    5. receive the data from the Ajax call and process it with wp_insert_post()
    6. add a callback function to hook into the new post being created to issue alerts to the admin, perhaps as simple as a js popup, and also initiate a system email to the client

    Does that seem about right?

    Questions:
    1. would you suggest using the ACF gallery field and the ACF image field for this function, or would you go about it in a more custom fashion and not use those fields?

    My goal is to create this so that the admin can work almost exclusively within the client closet post to create/select the images and create a new post of a different post type. It’s a stylist’s website, the clients essentially purchase outfit planning and the “outfits” will be the other post type that is created with the images. So a single outfit would contain 6-8 images (shoes, pants, shirt, jacket, bag, hat, belt, jewelry).

    So ideally the admin would go to a client’s closet post, upload that clients images and tag them so they belong to that client post (post title is client name), and then from within that client’s closet go through the added images and create the outfits. My goal is to make this as intuitive as possible. Also, despite the direction I’ve gone with it so far, if you would recommend going about it in a different way I’d love to hear your advice.

    Thanks.

    • This reply was modified 4 years, 2 months ago by traveler.
    Moderator bcworkz

    (@bcworkz)

    It sounds like you have a good grasp of what’s needed. wp_insert_post() will create new if no ID is supplied, update if an ID is supplied. A real world example of application is very helpful in understanding the programmatic needs. Thanks for explaining.

    I wouldn’t be quick to abandon the ACF UI unless it was simply unworkable. I’m always one to latch onto any existing UI when possible. I hate developing UIs ?? I’m assuming reliably targeting ACF elements with jQuery will not be a problem, I’ve never actually tried. You still might need added UI elements if handling everything after every selection isn’t performative. It all might perform better if there were “Begin Outfit” and “Finish Outfit” buttons (or one contextual button) which trigger appropriate scripts.

    Thread Starter traveler

    (@visualeight)

    Thanks bcworkz, I appreciate your advice. I had not thought about a two button trigger system but that would make it easier I think.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Admin plugin to select images from media gallery’ is closed to new replies.