• Resolved Bazan

    (@bazan)


    I need use 3 instances of Attachments, on some pages.

    First, i made with ACF (advanced custom fields) plugin a metaboxes for my pages where i set some values.

    These values are:

    Show on homepage? (yes / no)
    If “Show on homepage” is yes, then u can set a format
    standard: “text on left side”
    lefty: “text on right side”
    other: “another format”

    And now.

    I want to show one of instances on homepage (is_front_page()) to add images for my own designed slider. And it works well.

    The problem start on next instance.

    I want show one of Attachments instance on pages who have set a “Show on homepage” for yes, and “format” (custom field) is different of other (so format is set to standard or lefty).

    And the problem is, that when i added some files to this instances, it dont save any :/ I can add image, set a title to this, but when i press update post, files of this Attachments instance are blank, there are no files added :/

    The only problem is when i have set the attachments_location_{my_instance} filters.

    The code ive used:
    slider_meta_box.php included in functions.php

    function slides( $attachments )
    {
      $args = array(
    
        // title of the meta box (string)
        'label'         => __('Slajdy','broker'),
    
        // all post types to utilize (string|array)
        'post_type'     => array( 'page' ),
    
        // allowed file type(s) (array) (image|video|text|audio|application)
        'filetype'      => array('image'),  // no filetype limit
    
        // include a note within the meta box (string)
        'note'          => __('Dodaj slajdy tutaj','broker'),
    
        // text for 'Attach' button in meta box (string)
        'button_text'   => __( 'Wybierz obrazek', 'broker' ),
    
        // text for modal 'Attach' button (string)
        'modal_text'    => __( 'Dodaj', 'broker' ),
    
        /**
         * Fields for the instance are stored in an array. Each field consists of
         * an array with three keys: name, type, label.
         *
         * name  - (string) The field name used. No special characters.
         * type  - (string) The registered field type.
         *                  Fields available: text, textarea
         * label - (string) The label displayed for the field.
         */
    
        'fields'        => array(
          array(
            'name'  => 'title',                          // unique field name
            'type'  => 'text',                           // registered field type
            'label' => __( 'Tytuł slajdu', 'broker' ),     // label to display
          ),
    
        ),
    
      );
    
      $attachments->register( 'slides', $args ); // unique instance name
    
    }
    
    add_action( 'attachments_register', 'slides' );
    
    add_filter("attachments_location_slides",'slide_for_post_type',10,2);
    
    function slide_for_post_type($input,$instance){
    
        global $post_id;
    
        $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
          // check for a template type
        if ($instance == 'slides') {
            if ($template_file == 'homepage.php')
            {
                $input=true;
            } else {
                $input = false;
            }
        }
    
        return $input;
    
    }
    
    define( 'ATTACHMENTS_DEFAULT_INSTANCE', false );

    and home_image_meta_box.php included in functions.php after the previous file.

    function my_attachments( $attachments )
    {
      $fields         = array(
        array(
          'name'      => 'image_title',                         // unique field name
          'type'      => 'text',                          // registered field type
          'label'     => __( 'Title', 'attachments' ),    // label to display
        ),
      );
    
      $args = array(
    
        // title of the meta box (string)
        'label'         => 'My Attachments',
    
        // all post types to utilize (string|array)
        'post_type'     => array( 'post', 'page' ),
    
        // meta box position (string) (normal, side or advanced)
        'position'      => 'normal',
    
        // meta box priority (string) (high, default, low, core)
        'priority'      => 'high',
    
        // allowed file type(s) (array) (image|video|text|audio|application)
        'filetype'      => null,  // no filetype limit
    
        // include a note within the meta box (string)
        'note'          => 'Attach files here!',
    
        // by default new Attachments will be appended to the list
        // but you can have then prepend if you set this to false
        'append'        => true,
    
        // text for 'Attach' button in meta box (string)
        'button_text'   => __( 'Attach Files', 'attachments' ),
    
        // text for modal 'Attach' button (string)
        'modal_text'    => __( 'Attach', 'attachments' ),
    
        // which tab should be the default in the modal (string) (browse|upload)
        'router'        => 'browse',
    
        // fields array
        'fields'        => $fields,
    
      );
    
      $attachments->register( 'home_images', $args ); // unique instance name
    }
    
    add_action( 'attachments_register', 'my_attachments' );
    
    add_filter("attachments_location_home_image",'home_image_location',10,2);
    
    function home_image_location($input,$instance){
    
        global $post_id;
    
        $home= get_field('show_home');
       $format = get_field('format');
    
          // check for a template type
        if ($instance == 'home_images') {
            if (($home == 'yes') && ($format != 'other') )
            {
                $input=true;
            } else {
                $input = false;
            }
        }
    
        return $input;
    
    }

    The last instance have only one difference

    if (($home == 'yes') && ($format == 'other') )

    Instances are shown well on specific pages, it means, if its normal page, with “show on homepage” set to “no” it does not show any instance in dashboard.

    If “show on homepage” is set to “yes” then it show an instance in dashboard which should be shown. Problem is that this instance does not save any files.

    @edit
    I think, there is some problem in core of plugin, and it works like:

    When first checked instance (in my case, first instance is “slides”) cant be displayed on page (via location limit filter), then any later instance wont work.

    In other way, when u got ie. 5 instances, and:
    first – displayed and work,
    second – displayed and work
    third – not displayed
    fourth – displayed and NOT WORK
    fifth – displayed and NOT WORK

    But its only my guess.

    Please take care about this request, its very important issue :/

    http://wordpress.org/plugins/attachments/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hey,

    if you’re still running into the same problem or if somebody else bothers: You figured the problem out quite right. As soon as you have two filters registered, every metabox after the first will stop saving.

    The problem is also quite simple: In function meta_box_init a nonce is set after the first metabox and not cleared for the next ones. So just add a new line 677 before:

    // facilitate more fine-grained meta box positioning than post type
                        $applicable         = apply_filters( "attachments_location_{$instance}", true, $instance );

    and write:

    $nonce_sent = false;

    It did work for me. Maybe the author will consider this as a bugfix.

    Best,

    Christian

    Plugin Author Jon Christopher

    (@jchristopher)

    Very sorry for the delay on this! This issue has been fixed here https://github.com/jchristopher/attachments/commit/7743db5b377a8c08c02684eb669073b8494dbc0f and will be implemented in the next release.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Multiple instances, only one works’ is closed to new replies.