WordPress.org

Ready to get started?Download WordPress

Forums

custom password protected page text editable by client (9 posts)

  1. coding4fun
    Member
    Posted 2 years ago #

    So I have put this into my functions.php

    
    <?php
    add_filter( 'the_password_form', 'custom_password_form' );
    function custom_password_form() {
    	global $post;
    	$label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
    	$o = '<form class="protected-post-form" action="' . get_option('siteurl') . '/wp-pass.php" method="post">
    	' . __( "This post is password protected. To view it please enter your password below:" ) . '
    	<label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" /><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" />
    	</form>
    	';
    	return $o;
    }
    ?>
    

    The issue is the client has no way of editing the text without going to functions.php. Is there any way around this? For example, can I pull text from another document into functions.php? I am open to all suggestions.

    Thank you

  2. widelegger
    Member
    Posted 2 years ago #

    I use the native WordPress function get_template_part to pull in different php files I've added to my theme:

    http://codex.wordpress.org/Function_Reference/get_template_part

  3. coding4fun
    Member
    Posted 2 years ago #

    That helped! Thanks! Here's what I did. First I had to create a page called contact-password.php in my theme. If you don't have ftp access to a site to just make that file in your theme, you can plug this code into line one of the header.

    <?php touch('wp-content/themes/your-themes-name/contact-password.php');?>

    save the file, load the main page once, and then remove this code. The file is automatically added to your editor.

    Here's the functions.php code:

    
    <?php
    add_filter( 'the_password_form', 'custom_password_form' );
    function custom_password_form() {
    	global $post;
    	$label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
    	$o = '<form class="protected-post-form" action="' . get_option('siteurl') . '/wp-pass.php" method="post">
    	' . get_template_part( 'contact', 'password' ) . '
    	<label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" /><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" />
    	</form>
    	';
    	return $o;
    }
    ?>
    

    It pulls in whatever you place on contact-password.php to the password protected pages.

  4. sramos14
    Member
    Posted 2 years ago #

    Hi, I used this great trick but now my password box and submit buttons aren't appearing properly. The submit button is before the password input box. See it here: http://wtsglobal.com/wp/services-agreement/

    Would love any suggestions. I'm not so much a coder....

  5. coding4fun
    Member
    Posted 2 years ago #

    It looks like a styling issue to me. on style.css I took off
    float: left;
    under input[type="submit"], input[type="reset"]
    and it looked fine.

    You may have to give this one button on the private page a class of its own to make it look right.

  6. sramos14
    Member
    Posted 2 years ago #

    okay, I managed to get a line break in so it looks ok, but Firefox is prepopulating the password field with something... Looks fine in chrome and safari...

  7. coding4fun
    Member
    Posted 2 years ago #

    Under Firefox > Preferences > Security, i have the box checked to remember passwords for sites. You probably have this too. Just uncheck that and clear your browser's cache. The pre-fill you see in the password input should be gone.

  8. sramos14
    Member
    Posted 2 years ago #

    Thanks the style thing works better than my line break. Any thoughts on the password being ghost prepopulated in Firefox?

  9. sramos14
    Member
    Posted 2 years ago #

    I figured it out. I added autocomplete="off" to the password input field in the code. Thanks for the quick responses! Client happy.

Topic Closed

This topic has been closed to new replies.

About this Topic