Skip to content

Instantly share code, notes, and snippets.

@ocean90
Created November 28, 2011 18:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ocean90/1401490 to your computer and use it in GitHub Desktop.
Save ocean90/1401490 to your computer and use it in GitHub Desktop.
WP Editor: Scroll to old position after saving or updating a post
<?php
/**
* Adds a hidden input field for scrolltop value.
*
* @return void
*/
function ds_scrollto_input() {
$position = ! empty( $_GET['scrollto'] ) ? $_GET['scrollto'] : 0;
printf( '<input type="hidden" id="scrollto" name="scrollto" value="%d"/>', esc_attr( $position ) );
// Print Javascript data
add_action( 'admin_print_footer_scripts', 'ds_scrollto_js', 55 ); // Print after Editor JS.
}
add_action( 'edit_form_advanced', 'ds_scrollto_input' );
/**
* Prints Javascript data.
* Saves on form submit the scrollTop value into the hidden input field.
* Includes callback for TinyMCE scrolling.
*
* @return void
*/
function ds_scrollto_js() {
?>
<script>
( function( $ ) {
$( '#post' ).submit( function() {
// TinyMCE or HTML Editor?
scrollto =
$('div.wp-editor-wrap').hasClass('tmce-active') ?
$('#content_ifr').contents().find( 'body' ).scrollTop() :
$('#content' ).scrollTop();
// Save scrollto value
$( '#scrollto' ).val( scrollto );
} );
// Only HTML editor: scroll to scrollto value
$( '#content' ).scrollTop( $( '#scrollto' ).val() );
} )( jQuery );
/*
* Callback function for TinyMCE setup event
* See http://www.tinymce.com/wiki.php/API3:event.tinymce.Editor.onInit
*/
function rich_scroll( ed ) {
ed.onInit.add( function() {
jQuery( '#content_ifr' ).contents().find( 'body' ).scrollTop( jQuery( '#scrollto' ).val() );
} );
};
</script>
<?php
}
/**
* Extend TinyMCE config with a setup function.
* See http://www.tinymce.com/wiki.php/API3:event.tinymce.Editor.onInit
*
* @param array $init
* @return array
*/
function ds_extend_tiny_mce( $init ) {
if ( wp_default_editor() == 'tinymce' )
$init['setup'] = 'rich_scroll';
return $init;
}
add_filter('tiny_mce_before_init', 'ds_extend_tiny_mce');
/**
* Don't loose the scrollto value.
*
* @param string $location
* @return string
*/
function ds_add_scrollto_as_query_arg( $location ) {
if( ! empty( $_POST['scrollto'] ) )
$location = add_query_arg( 'scrollto', (int) $_POST['scrollto'], $location );
return $location;
}
add_filter( 'redirect_post_location', 'ds_add_scrollto_as_query_arg');
?>
@bueltge
Copy link

bueltge commented Nov 28, 2011

aufwendige Lösung für recht wenig Benefit, oder?
Für WP müsste man noch an den Codex den Core ran holen.

@ocean90
Copy link
Author

ocean90 commented Nov 28, 2011

@bueltge
Ja leider, für rein den HTML Editor ist es weit weniger. Ich arbeite wenn eigentlich nur mit dem Core.

@bueltge
Copy link

bueltge commented Nov 29, 2011

Aber nach G+ zu urteilen, scheint es ja Bedarf zu geben.
Wobei ich dann den JS-part extra haben wollte um ihn zusammenfassen zu lassen; so ist er ja immer hart im Footer, nicht packbar. Aber ich verstehe es, da der Aufwand gleich etwas steigt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment