Building a Disclaimer Notice Plugin for Multi-Author Blogs

A friend of mine operates a multi-author blog powered by WordPress.

To prevent any legal trouble, he often adds a ‘Disclaimer’ in every post made by guest authors which he did by editing and including the disclaimer text before publication.

I’m sure you will agree, that editing and adding disclaimers in every post made by guest authors seems a daunting task.

In this article, we will build a simple Disclaimer Notice plugin that will have an option page where a site administrator can add the disclaimer text which automatically gets appended immediately before or after the post content.

Plugin Development

To begin the plugin development, we need to include the plugin header in the plugin PHP file.

Without the header, WordPress will not recognize the plugin.

<?php
/*
Plugin Name: Disclaimer Manager
Plugin URI: http://sitepoint.com
Description: Easy Disclaimer Manager for Multi-author blogs.
Version: 1.0
Author: Agbonghama Collins
Author URI: http://w3guy.com
License: GPL2
*/

To get started building the settings page for the plugin; first, we will add the sub menu page to the ‘Settings’ menu using the function add_options_page placed in a function registered with the admin_menu.

// Add the admin options page
add_action( 'admin_menu', 'dm_settings_page' );

function dm_settings_page() {
	add_options_page( 'Disclaimer Manager', 'Disclaimer Manager', 'manage_options', 'disclaimer-manager', 'dm_options_page' );
}

The argument passed to add_options_page() are as follows:

  • Disclaimer Manager: The text to be displayed in the title tags of the page when the menu is selected.
  • Disclaimer Manager: The text to be used for the menu.
  • manage_options: The capability required for this menu to be displayed to the user.
  • disclaimer-manager: The slug name to refer to this menu.
  • dm_options_page: The function to be called to output the plugin settings page.

Below, is the code for the callback function dm_options_page that will display the settings page.

// Draw the options page
function dm_options_page() {
	?>
	<div class="wrap">
		<?php screen_icon(); ?>
		<h2> Disclaimer Manager for Authors </h2>

		<form action="options.php" method="post">
			<?php settings_fields( 'disclaimer_manager_options' ); ?>
			<?php do_settings_sections( 'disclaimer-manager' ); ?>
			<?php submit_button(); ?>
		</form>
	</div>
<?php
}

The WordPress Settings API is being used to build and manage the settings form.

The settings_fields function in dm_options_page() above output the nonce, action, and form fields for the settings page while the do_settings_sections() prints out all settings sections added to a particular settings page.

Below is the full Settings API code for the settings page.

// Register and define the settings
add_action( 'admin_init', 'dm_admin_init' );
function dm_admin_init() {
	register_setting( 'disclaimer_manager_options', 'disclaimer_manager_options',
		'' );

	add_settings_section( 'dm_main', 'Plugin Settings',
		'', 'disclaimer-manager' );

	add_settings_field( 'dm_textarea-id', 'Enter Disclaimer Text',
		'disclaimer_text_textarea', 'disclaimer-manager', 'dm_main' );

	add_settings_field( 'dm_select-id', 'Disclaimer Position',
		'disclaimer_text_position', 'disclaimer-manager', 'dm_main' );
}

	// Display and fill the form field
function disclaimer_text_textarea() {
	// get option 'disclaimer_text' value from the database
	$options         = get_option( 'disclaimer_manager_options' );
	$disclaimer_text = $options['disclaimer_text'];

	// echo the field
	echo "<textarea rows='8' cols='50' id='disclaimer_text' name='disclaimer_manager_options[disclaimer_text]' >$disclaimer_text</textarea>";
}

function disclaimer_text_position() {
	// get option 'disclaimer_position' value from the database
	$options             = get_option( 'disclaimer_manager_options' );
	$disclaimer_position = $options['disclaimer_position'];

	echo '<select name="disclaimer_manager_options[disclaimer_position]">';
	echo '<option value="top"' . selected( $disclaimer_position, 'top' ) . '>Top</option>';
	echo '<option value="bottom"' . selected( $disclaimer_position, 'bottom' ) . '>Bottom</option>';
	echo '</select>';
}

Take Note: The register setting() registers the setting.

The add_settings_section() creates settings sections – groups of settings you see on WordPress settings pages with a shared heading.

The add_settings_field() registers a settings field to a settings page and section.

The get_option() retrieves the values of the settings form from the database and the update_option() saves the form values to the database.

We are done building the settings page for the plugin.

Below is a screenshot of the plugin settings page.

Disclaimer Notice Plugin - Image 1

The function add_disclaimer_to_post as its name implies, appends the ‘Disclaimer’ text to the top or bottom of every post as defined in the plugin settings page.

function add_disclaimer_to_post( $content ) {
	
	$options = get_option( 'disclaimer_manager_options' );

	// get disclaimer text form DB
	$disclaimer_text = $options['disclaimer_text'];

	// get disclaimer position from DB
	$disclaimer_position = $options['disclaimer_position'];

	// ensure we are in a post and not a page
	if ( is_single() ) : // if disclaimer position is set to top
	{
		if ( $disclaimer_position == 'top' ) {
			$content = $disclaimer_text . $content;
		}

		// if disclaimer position is set to bottom
		if ( $disclaimer_position == 'bottom' ) {
			$content .= $disclaimer_text;
		}
	}

	endif;

	return $content;
}

Allow me to explain what the code above does.

The ‘Disclaimer Text’ and its position are retrieved from the database and saved to the variables $disclaimer_text and $disclaimer_position.

Next, we use the Boolean WordPress function is_single() to ensure we are dealing with a post and not an attachment or page.

The next two if conditional statements append the disclaimer to the top or bottom of the post content depending on the outcome of $disclaimer_position.

To put the function to work, we need to hook it to the content filter (used to filter the content of a post after it is retrieved from the database and before it is printed to the screen).

add_filter( 'the_content', 'add_disclaimer_to_post' );

Voila! We are done coding the ‘Disclaimer Plugin’.

Here is a screenshot of the plugin in action:

Disclaimer Notice Plugin Image 2

Conclusion

To further understand how the plugin was built and how you can implement it in your WordPress site, download the plugin.

If you are looking for an advanced disclaimer plugin with features such as:

  • Ability to choose the authors that will have a disclaimer or notification displayed in their post.
  • Built-in editor for adding CSS Styles for the ‘Disclaimer’ text / notification and lots more.

Grab the improved version from the WordPress Plugin Directory.

Let me know your thoughts in the comments.

Win an Annual Membership to Learnable,

SitePoint's Learning Platform

No Reader comments

Comments on this post are closed.