Ability to navigate to the next/previous post after saving edits to the current post

I think it would be nice to be able to navigate to the next or previous post from the current post (i.e. after saving changes). This would be convenient if your editing posts in a particular category instead of having to click back on the “Posts” link in the admin sidebar, then having to filter the posts by category again. It would also eliminate having to hit the browser back arrow “twice” to get back to your post list.


Read-only archive: https://petitions.classicpress.net/posts/172/ability-to-navigate-to-the-next-previous-post-after-saving-edits-to-the-current-post

Author: Hamid Abugideiri

Vote count: 5

Status: Pull Request (849)

No comments posted.

There is a plugin that adds next post and previous post buttons: Admin Post Navigation – coffee2code.com It also provides various filters which enable things like the orderby parameter to be changed.

I think this is the right approach but, if we add this to core, we should probably be able to do this with PHP where the plugin has to use JavaScript.

I’d be happy to look into this further with a view to coming up with a PR.

3 Likes

It would be even better if this carried over to CC and enabled next and previous buttons for orders

1 Like

Actually, for any Post Type. With a Screen Option to enable/disable it per Post Type.

I now have it working like that. The plugin I mentioned uses the Screen Options to set filters to change the orderby parameter. Having tested it, I don’t like that much. It’s better, in my view, to have it follow the order of the posts that’s already set. If someone wants to change that, they can use the filters that already exist in CP to do so (either globally or just in the edit screen).

Great idea! Will work on that.

1 Like

As you can see from the screenshot, I have the Previous/Next buttons working (just the Previous here because there is no Next) and I have the control added to the Screen Options. I have also added the JavaScript to make it respond immediately when the checkbox is checked or unchecked.

But there’s precious little documentation about Screen Options, and I haven’t been able to find how to hook into the function that presumably already exists to send the Additional Settings to the database. Has anyone any ideas?

Here is the code I have currently in the relevant part of wp-admin/class-wp-screen.php:


		if ( 'post' === $this->base ) {
			$expand                 = '<fieldset class="editor-expand hidden"><legend>' . __( 'Additional settings' ) . '</legend><label for="editor-expand-toggle">';
			$expand                .= '<input type="checkbox" id="editor-expand-toggle"' . checked( get_user_setting( 'editor_expand', 'on' ), 'on', false ) . ' />';
			$expand                .= __( 'Enable full-height editor and distraction-free functionality.' ) . '</label>';

			/**
			 * Filter for Previous and Next buttons alongside Add New button.
			 * 
			 * @param bool Whether to show these buttons. Default true.
			 *
			 * @since CP-1.x.x
			 */			

			// Current code mimics above code block, but maybe the following might be better
			// $nav_checked = get_user_meta( get_current_user_id(), '_admin-post-nav-' . $this->post_type, true );
			// if ( '' === $nav_checked ) {
				// $nav_checked = '1';
			// }

			$expand                .= '<label for="admin-post-nav-hide">';
			$expand                .= '<input type="checkbox" id="admin-post-nav-hide"' . checked( get_user_setting( 'admin-post-nav', 'on' ), 'on', false ) . ' />';
			$expand                .= _x( 'Enable Previous and Next buttons', 'Admin Post Navigation panel' ) . '</label></fieldset>';
			?>

			<script>
			document.addEventListener('DOMContentLoaded', function() {
				const checkboxNav = document.getElementById('admin-post-nav-hide');
				const buttonsNav = document.getElementById('admin-post-nav');

				checkboxNav.addEventListener('change', function(event) {
					if (event.currentTarget.checked) {
						buttonsNav.removeAttribute('hidden');
					} else {
						buttonsNav.setAttribute('hidden', 'hidden');
					}
				});
			});
			</script>

			<?php
			$this->_screen_settings = $expand;
		}

Look at wp-admin/js/postbox.js which already has code for some screen options on the editor page. There is a function save_state which does an AJAX call. That action is also used by the nav-menu.js, but checked for in wp-admin/admin-ajax.php where there is a long list of core actions.

2 Likes

Thanks, Joy. I shall take a look.

OK, I have it working now. The code it’s hooking into doesn’t allow for differentiation by post type, but (a) it does allow each user to set their own preferences and (b) I’ve now included a filter for the whole thing that can be used either to turn off all this functionality altogether or to disable it conditionally (e.g. by post type).

I’ll do a bit more testing and then submit a PR.

2 Likes

PR now submitted: Add Previous and Next buttons alongside Add New button on single post screen in admin by KTS915 · Pull Request #849 · ClassicPress/ClassicPress · GitHub

3 Likes

Looks like we missed this one. Updated status to pull request.