Multiple instances, only one works
-
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.phpfunction 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 WORKBut its only my guess.
Please take care about this request, its very important issue :/
- The topic ‘Multiple instances, only one works’ is closed to new replies.