Prevent front and posts page being the same


I’m sure those of you familiar with WordPress will be acustom to the two settings provided under Admin -> Settings -> Reading, known as the Front Page and the Posts Page. Whilst some users find their usage easy, and others find it somewhat confusing, these two settings have never been intended to share a page, in short both should never be set to the same.

The following simple jQuery based plugin will prevent a user from applying the same page to these settings. This could be useful for those of you providing clients with WordPress sites that do not understand such features and attempt to configure these both to the same page.

<?php
/*
Plugin Name: Prevent front and posts page being the same.
Description: Prevents the posts page and front page settings be set to the same page.
*/
add_action( 'admin_head-options-reading.php' , 'postspage_frontpage_not_same' );
function postspage_frontpage_not_same() {
	?>
	<script type="text/javascript">
	//<![CDATA[
	jQuery(document).ready(function($) {		
		// Get the current selected values
		var wp_frontpage = $('#page_on_front option:selected').val();
		var wp_postspage = $('#page_for_posts option:selected').val();
		if( wp_postspage != '' ) {
			$('#page_on_front').find('option[value=' + wp_postspage + ']').attr("disabled","disabled");
		}
		if( wp_frontpage != '' ) {
			$('#page_for_posts').find('option[value=' + wp_frontpage + ']').attr("disabled","disabled");
		}
		// Attach a change function to the front page dropdown
		$('#page_on_front').change(function() {
				// Re-declare the variable for use inside the scope of the function
				var wp_frontpage = $('#page_on_front option:selected').val();
				if( wp_frontpage != '' ) {
					// Find a matching option in posts page dropdown
					$('#page_for_posts').find('option[value=' + wp_frontpage + ']').attr("disabled","disabled");
				}	
				$('#page_for_posts').find('option[value!=' + wp_frontpage + ']:disabled').removeAttr('disabled');
		});
		// Attach a change function to the posts page dropdown
		$('#page_for_posts').change(function() {
				// Re-declare the variable for use inside the scope of the function
				var wp_postspage = $('#page_for_posts option:selected').val();
				if( wp_postspage != '' ) {
					// Find a matching option in the front page dropdown
					$('#page_on_front').find('option[value=' + wp_postspage + ']').attr("disabled","disabled");
				}
				$('#page_on_front').find('option[value!=' + wp_postspage + ']:disabled').removeAttr('disabled');
		});
	});
	//]]>
	</script>
	<?php
}

As always feel free to post a comment, or if you have any suggestions regarding the code, please do share them.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s