WordPress.org

Make WordPress Core

Opened 13 years ago

Closed 12 years ago

Last modified 6 years ago

#9372 closed defect (bug) (worksforme)

"Preview" button in edit page button does not work.

Reported by: webstoc Owned by: azaozz
Milestone: Priority: high
Severity: major Version: 2.7.1
Component: Administration Keywords: reporter-feedback
Focuses: Cc:

Description

I'm running multiple instances of wordpress for a group of customers in a variety of blogs, and they all have been reporting for awhile that the preview button in the edit post section of their blog is broken. I finally took time to chase down the section of the code that's responsible for this.

In wp-admin/js/post.js (line 385 for my version - 2.7.1), there exists the following code block:

$('#post-preview').click(function(e){

if ( 1 > $('#post_ID').val() && autosaveFirst ) {

autosaveDelayPreview = true;
autosave();
return false;

}

$('input#wp-preview').val('dopreview');
$('form#post').attr('target', 'wp-preview').submit().attr('target', );
$('input#wp-preview').val(
);
return false;

});

This jquery code retrieves a hidden field in the form generated by edit-form-advanced.php, populates it, submits the form, and clears the value. It then returns false, which is of course why the button is not working for my users. The question is, why is the form submission not working? The ff error console is unenlightening, but that's not surprising; I've found jQuery does have a tendency to suppress errors.

I've checked what's being returned by the various jquery selectors in that instance, and everything seems on the up and up. I do note that commenting out that block, including the false return value, yields a preview page in a separate tab that works just fine. E.g.:

$('#post-preview').click(function(e){

if ( 1 > $('#post_ID').val() && autosaveFirst ) {

autosaveDelayPreview = true;
autosave();
return false;

}
/*
$('input#wp-preview').val('dopreview');
$('form#post').attr('target', 'wp-preview').submit().attr('target', );
$('input#wp-preview').val(
);
return false;
*/

});

Not knowing why this is being done by form rather than by just hyperlink, coupled with the fact that I'd be off the reservation when it came to updates, makes me loathe to make this sort of fix for my users. I didn't see any instance of this particular problem in the trac so far. Can advise?

Change History (10)

#1 @webstoc
13 years ago

Addendum: the above workaround only works if the new post has been saved as a draft first, or published.

#2 @azaozz
13 years ago

When JavaScript is enabled the preview button shows previews for not yet saved posts. Also when editing published posts it shows the preview with the latest changes included (before the post is saved and the changes go "live").

If js is disabled it only shows preview for drafts or the actual published post when editing. By commenting that js code you're making it behave as if js is disabled.

It sounds like something is interfering with the submission of the form from js. Perhaps run jQuery('form#post').submit() in Firebug's console to try to debug it.

#3 @webstoc
13 years ago

I see. Well, I don't use firebug that much; in the past when I tried it, I had the same cache problem with it as the fellow who filed this bug report. Maddening. So, I gave it a try per your suggestion, but I may not be using it correctly.

I did a minor rewrite of the function, like so:

$('#post-preview').click(function(e){

                //convenience function
		function _d(msg){
			dump(msg + "\n"); 
		};

		if ( 1 > $('#post_ID').val() && autosaveFirst ) {
			autosaveDelayPreview = true;
			autosave();
			return false;
		}
		var myform = $('form#post');		
		_d("target: " + myform[0].target);
		myform.attr('target', 'wp-preview');
		_d("target: " + myform[0].target);
		_d("submit: " + myform.submit);
		//bind submit function
		myform.submit(function(){
			_d('form submitted.');
		});
		myform.submit();
		myform.attr('target', '');
		return false;
		/*
		$('input#wp-preview').val('dopreview');
		$('form#post').attr('target', 'wp-preview').submit().attr('target', '');
		$('input#wp-preview').val('');
		return false;
		*/
	});

The output of which in the console looks like so:

target: 
target: wp-preview
submit: function (a) {
    return a ? this.bind(b, a) : this.trigger(b);
}
form submitted.

So, the post-submit action of the form *is* being triggered. In the firebug console however, I noted *no* action on the "Net" tab (subsequent to button press), implying the post is likely not actually happening, as you suggested. A false return, perhaps? That would account for the behavior.

Ducking into edit-form-advanced.php, the target form is found (for me) on line 520. Adding an onsubmit event here:

<form name="post" action="post.php" method="post" id="post" onsubmit='dump("foo\n");'>

Gives this:

target: 
target: wp-preview
submit: function (a) {
    return a ? this.bind(b, a) : this.trigger(b);
}
form submitted.
foo

I see no inline binding of submit events here, and none in post.js. Is there another location that might return false?

#4 @Denis-de-Bernardy
13 years ago

  • Keywords needs-patch added; edit preview post removed
  • Milestone changed from Unassigned to 2.8

Cross posting, possibly related: #9433

#5 @janeforshort
12 years ago

  • Milestone changed from 2.8 to Future Release

Punting due to feature freeze. Reconsider with next release.

#6 @Denis-de-Bernardy
12 years ago

  • Milestone changed from Future Release to 2.8
  • Priority changed from normal to high
  • Severity changed from normal to major

Confirmed in 2.8. Just got this myself while looking into #9433, and this one is rather major. The functionality is simply broken. This just can't wait until a future release.

#7 @ryan
12 years ago

  • Owner changed from anonymous to azaozz

#8 @hakre
12 years ago

  • Keywords reporter-feedback added

Please provide simple steps to reproduce.

#9 @Denis-de-Bernardy
12 years ago

  • Keywords needs-patch removed
  • Resolution set to worksforme
  • Status changed from new to closed

I can no longer manage to. I think this has been fixed by r11117.

Please re-open with steps to reproduce if it's still broken.

#10 @DrewAPicture
6 years ago

  • Milestone 2.8 deleted
Note: See TracTickets for help on using tickets.