Skip to content

Instantly share code, notes, and snippets.

@felixarntz
Created August 17, 2017 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixarntz/5b1ad2a72dc22053c07812340daf3274 to your computer and use it in GitHub Desktop.
Save felixarntz/5b1ad2a72dc22053c07812340daf3274 to your computer and use it in GitHub Desktop.
Fixed Edit Sidebar
<?php
/*
Plugin Name: Fixed Edit Sidebar
Plugin URI: https://gist.github.com/felixarntz/5b1ad2a72dc22053c07812340daf3274
Description: Allows the sidebar in the post editing screen to scroll along if the screen is large enough.
Version: 1.0.0
Author: Felix Arntz
Author URI: https://leaves-and-love.net
License: GNU General Public License v2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
// Note that jQuery must be loaded on the page.
function fixed_edit_sidebar_print_script_and_style() {
?>
<script>
( function( $ ) {
'use strict';
function checkRightContainerOffset() {
var offset = 0;
var $postbox = $( '#postbox-container-1' );
if ( $( 'body' ).hasClass( 'admin-bar' ) ) {
if ( document.documentElement.clientWidth <= 782 ) {
offset = 46;
} else {
offset = 32;
}
}
if ( $postbox.height() < $( window ).height() - offset ) {
if ( $( window ).scrollTop() + offset >= $( '#titlediv' ).offset().top - 10 ) {
$postbox.addClass( 'fixed' );
} else {
$postbox.removeClass( 'fixed' );
}
} else {
$postbox.removeClass( 'fixed' );
}
}
checkRightContainerOffset();
$( window ).on( 'scroll', checkRightContainerOffset );
}( window.jQuery ) );
</script>
<style>
@media screen and (min-width: 980px) {
#post-body.columns-2 #postbox-container-1.fixed {
position: fixed;
top: 42px;
right: 20px;
margin-right: 0;
float: none;
}
}
</style>
<?php
}
function fixed_edit_sidebar_add_hook() {
add_action( 'admin_footer', 'fixed_edit_sidebar_print_script_and_style' );
}
add_action( 'load-post.php', 'fixed_edit_sidebar_add_hook' );
add_action( 'load-post-new.php', 'fixed_edit_sidebar_add_hook' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment