24 Feb 2009

Customize WordPress Default Protected Post Password Form

Add the following lines to your WordPress Theme’s template functions.php file (For example, functions.php file for a Theme named “default” would probably reside in the directory wp-content/themes/default/functions.php) or create it as a Plugin by filling the additional Standard Plugin Information:

<?php
function custom_password_form($form) {
  $subs = array(
    '#<p>This post is password protected. To view it please enter your password below:</p>#' => '<p>Your own custom message.</p>',
    '#<form(.*?)>#' => '<form$1 class="passwordform">',
    '#<input(.*?)type="password"(.*?) />#' => '<input$1type="password"$2 class="text" />',
    '#<input(.*?)type="submit"(.*?) />#' => '<input$1type="submit"$2 class="button" />'
  );

  echo preg_replace(array_keys($subs), array_values($subs), $form);
}

add_filter('the_password_form', 'custom_password_form');
?>

From the snippet above, here are the changes that have been made to the original post password form:

  1. Replace “This post is password protected. To view it please enter your password below:” with “Your own custom message.”.
  2. Add class name passwordform to form element.
  3. Add class name text to input element with type attribute that has the value password.
  4. Add class name button to input element with type attribute that has the value submit.

Customize the snippet to suit your needs and preferences.