Custom Password form

I'm using the following in my functions.php to replace the default password form with a custom one.

add_filter( 'the_password_form', 'custom_password_form' );
function custom_password_form() {
	global $post;
	$label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
	$output = '<form class=&quot;protected-post-form&quot; action=&quot;http://cy4or.co.uk/wp-login.php?action=postpass&quot; method=&quot;post&quot;>
	<p>' . __( &quot;Some CY4OR Resources are protected. Please enter your password or complete the form below to request access.&quot; ) . '</p>
	<label class=&quot;pass-label&quot; for=&quot;' . $label . '&quot;>' . __( &quot;Password&quot; ) . ' </label><input name=&quot;post_password&quot; id=&quot;' . $label . '&quot; type=&quot;password&quot; /><input type=&quot;submit&quot; name=&quot;Submit&quot; class=&quot;button&quot; value=&quot;' . esc_attr__( &quot;Submit&quot; ) . '&quot; /></form>
	';
	return $output;
}

It works as it should when an entire post/page is password protected but when using the Password Protect plugin to hide just some of the content within a post it uses the default WP password form.

How can I hook into my custom form for this plugin?

  • Jonny Wood

    As a temporary solution I've edited the plugin code to reflect my custom form, but this is not an ideal solution as every time I update the plugin I'll have to re-apply the code.

    //check cookie for password
    		if ( $_COOKIE['psc-postpass_' . COOKIEHASH] == sha1( $password ) ) {
       		return do_shortcode( $content );
    		} else {
    		  $label = 'pwbox-' . rand();
    			return '<form class="protect-content" action="' . admin_url('admin-ajax.php') . '" method="post"><input type="hidden" name="action" value="psc-set" />
    			<p>' . __('Some CY4OR Resources are protected. Please enter your password below or <a href="'.get_bloginfo('url').'/resources/access/">request access here</a>.', 'psc') . '</p>
    			<p><label for="' . $label . '">' . __("Password:", 'psc') . ' <input name="post_password" id="' . $label . '" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . esc_attr__("Submit", 'psc') . '" /></p>
    			</form>
    			';
    		}
  • aecnu

    Greetings JWC,

    Thank you for supplying the custom coding that indeed resolves your own issue.

    The only way I can see that this issue to be better handled is that the custom coding is added to the plugin permanently. Of course there will be somebody complaining and want it reversed.

    Therefore considering that it is indeed custom coding and that most members will possibly want to reverse this if we changed it permanently, please advise if you were in our shoes how to handle this?

    I am all ears.

    Cheers, Joe

  • Jonny Wood

    Hi Joe,

    I certainly wouldn't suggest using my code as this is specific to me only.

    My point was that it is possible to change the default post password form via the functions file using:
    add_filter( 'the_password_form', 'custom_password_form' );

    The solution I thought may be possible, is for the plugin to use a hook that could be altered in the same way. For example:

    add_filter( 'password_protect_form', 'custom_password_protect_form' );

    My knowledge of Wordpress hooks, filters and php is limited but I hope this makes sense.

  • aecnu

    Greetings 1pp1k10k4m1,

    Thank you for the question but i have done all I can do which is alert the lead developer to the code, but the code is all there so have at it because there is no indication or guarantee that the lead developer may ever incorporate it and if so he will do the right thing and give applicable credit to Jonny Wood for the code.

    Other then that there is nothing more that I can do and you can go ahead and use those coding snippets if you chose coding them in as you see fit.

    Thank you for being a WPMU DEV Community Member!

    Cheers, Joe