Make WordPress Core

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#22758 closed defect (bug) (duplicate)

menu_order is 0 for new galleries so no backward compatibility

Reported by: vickybiswas's profile vickybiswas Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Gallery Keywords:
Focuses: Cc:

Description

Shows solved in 22985 and connected tickets but I still see no menu_order update in trunk.

How to reproduce

  1. Add new post
  2. Add Media
  3. Create Gallery
  4. Upload Files
  5. Publish Post

The Menu order wont be populated in the DB it will all be 0

also

If you update the gallery re order or do anything it still wont update the same.

This is very needed to maintain the old code to keep functioning seamlessly.

Attachments (1)

menu_order.patch (1.4 KB) - added by vickybiswas 12 years ago.
Patch from Menu Order

Download all attachments as: .zip

Change History (12)

#1 @vickybiswas
12 years ago

The function we are using to handle the same is as below

function pmc_gallery_menu_order_fix($id) {
        $regex_pattern = get_shortcode_regex();
        preg_match ('/'.$regex_pattern.'/s', stripslashes($_POST['content']), $regex_matches);
        if ($regex_matches[2] == 'gallery') :
            $attribureStr = str_replace (" ", "&", trim ($regex_matches[3]));
            $attribureStr = str_replace ('"', '', $attribureStr);
            $attributes = wp_parse_args ($attribureStr);
        endif;
        $ids = explode(',', $attributes[ids]);
        $images = get_posts( array(
		'post_parent' => $post->ID,
		'numberposts' => '-1',
		'post_status' => 'inherit',
		'post_type' => 'attachment',
		'post_mime_type' => 'image',
		'orderby' => 'menu_order ID',
		'order' => 'ASC'
	) );
	if ( empty($images) ) {
		// no attachments here
	} else {
		foreach ( $images as $attachment_id => $attachment ) {
			if (in_array($attachment->ID, $ids)) {
				$update_post = array();
				$update_post['ID'] = $attachment->ID;
				$update_post['menu_order'] = array_search($attachment->ID, $ids);
				wp_update_post( $update_post );
			};
		}
	}
}
add_action('pre_post_update', 'pmc_gallery_menu_order_fix');

#2 @vickybiswas
12 years ago

Cant update using ajax as its related to post and after alteration using AJAX if the post is not saved it would be incorrect to save the menu_order.

A patch is added for the same

@vickybiswas
12 years ago

Patch from Menu Order

#3 follow-up: @nacin
12 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Since you commented to http://wordpress.org/support/topic/35-rc1-menu_order-not-saved-to-the-database-for-attached-images, could you read the rest of the thread?

As a hidden feature, you can go to the "Uploaded to this post" filter in the media library tab and sort those attachments. menu_order will then save. #22608

#4 in reply to: ↑ 3 @vickybiswas
12 years ago

Replying to nacin:

Since you commented to http://wordpress.org/support/topic/35-rc1-menu_order-not-saved-to-the-database-for-attached-images, could you read the rest of the thread?

As a hidden feature, you can go to the "Uploaded to this post" filter in the media library tab and sort those attachments. menu_order will then save. #22608

Yes but with that feature I am not able to update it once I create
Can you please guide me if I am missing something

#5 follow-up: @nacin
12 years ago

Insert [gallery] manually into your post. Then rearrange "Uploaded to this post".

There is no way to use the new gallery interface and have it obey menu_order without messing with the [gallery] shortcode in some way. You can use new-style galleries, or you can continue to rearrange "Uploaded to this post" and run queries based on menu order. Can't have both.

#6 in reply to: ↑ 5 @vickybiswas
12 years ago

Replying to nacin:

Insert [gallery] manually into your post. Then rearrange "Uploaded to this post".

There is no way to use the new gallery interface and have it obey menu_order without messing with the [gallery] shortcode in some way. You can use new-style galleries, or you can continue to rearrange "Uploaded to this post" and run queries based on menu order. Can't have both.

The code I am attaching allows editors on my VIP sites to use the new gallery interface and at the same time maintain the menu_order so that our galleries dont break.

The aim is to only maintain the backward compatibility and at the same time use the wonderful new changes.

/**
 * WP 3.5 doesn't update menu_order for ordering attachments, so this function updates the menu_order so that you can orderby menu_order
 * @param int $id
 */
function pmc_gallery_menu_order_fix($id) {
	global $post;
	$regex_pattern = get_shortcode_regex();
	preg_match ('/'.$regex_pattern.'/s', stripslashes($_POST['content']), $regex_matches);
	if ( ! $regex_matches ) {
		return false;
	}
	if ($regex_matches[2] == 'gallery') {
		$attribure_str = str_replace(" ", "&", trim ($regex_matches[3]));
		$attribure_str = str_replace('"', '', $attribure_str);
		$attributes = wp_parse_args($attribure_str);
	}
	$ids = explode(',', $attributes['ids']);
	$images = get_posts( array(
		'post_parent' => $post->ID,
		'numberposts' => '-1',
		'post_status' => 'inherit',
		'post_type' => 'attachment',
		'post_mime_type' => 'image',
		'orderby' => 'menu_order ID',
		'order' => 'ASC'
	) );
	if ( $images ) {
		foreach ( $images as $attachment_id => $attachment ) {
			if (in_array($attachment->ID, $ids)) {
				$update_post = array();
				$update_post['ID'] = $attachment->ID;
				$update_post['menu_order'] = array_search($attachment->ID, $ids);
				wp_update_post( $update_post );
			};
		}
	}
}
add_action('pre_post_update', 'pmc_gallery_menu_order_fix');


#7 @Viper007Bond
12 years ago

FYI Vicky's code is a cool way to set menu_order based off of the ids shortcode parameter but it's missing this before the $ids declaration:

if ( empty( $attributes['ids'] ) )
    return;

#8 follow-ups: @nacin
12 years ago

In 3.5, posts can have more than one arbitrary, custom gallery. This is why we don't do exactly this. (And for this code, a second gallery in a post would get missed.)

#9 in reply to: ↑ 8 ; follow-up: @Viper007Bond
12 years ago

Replying to nacin:

In 3.5, posts can have more than one arbitrary, custom gallery. This is why we don't do exactly this. (And for this code, a second gallery in a post would get missed.)

Totally. This is certainly code that belong alongside any custom gallery code in a plugin but not in core.

#10 in reply to: ↑ 8 @vickybiswas
12 years ago

Replying to nacin:

In 3.5, posts can have more than one arbitrary, custom gallery. This is why we don't do exactly this. (And for this code, a second gallery in a post would get missed.)

Nacin I agree and appreciate the second gallery concept however still dont understand how would the second gallery be missed

#11 in reply to: ↑ 9 @vickybiswas
12 years ago

Replying to Viper007Bond:

Replying to nacin:

In 3.5, posts can have more than one arbitrary, custom gallery. This is why we don't do exactly this. (And for this code, a second gallery in a post would get missed.)

Totally. This is certainly code that belong alongside any custom gallery code in a plugin but not in core.

Viper you are correct if it hampers any of the aims of the core but if not it would be good to add for backward compatibility.

Note: See TracTickets for help on using tickets.