I have, based on post category, added a password form (shortcode) whereof the purpose is to hide certain HTML code.
To do this, I have wrapped certain HTML-tags in a shortcode using the the_content
filter.
The password itself is added through an array, which would be much better suited as an global setting through the WP admin settings page - but that's a different story.
My main problem is this; after submitting the password - no matter if the password is right or wrong - the user get "scrolled" all the way to the top of the page.
That should not happen, which is why I added a ID to the form. Upon submitting the correct password, the user should be kept in the same place, which is where the form was which is where the HTML code now is.
Makes sense? Part of the problem is this; there's no message for "password successful" or "incorrect password".
As you can see in the code, I've already tried to add the ID to the path using this:
path=/#functioncode
without success.
This is the full code:
add_shortcode( 'protected', 'protected_html' );
function protected_html( $atts, $content=null ) {
$functionUserPassword = isset( $_REQUEST['password']) ? $_REQUEST['password'] : ( isset( $_COOKIE['functionUserPassword']) ? $_COOKIE['functionUserPassword'] : NULL );
if ( in_array( $functionUserPassword, array('password') ) ) {
$return_html_content = do_shortcode($content);
} else {
$return_html_content =
'<div id="functioncode" style="margin-top:20px;font-size:15px;">To view the content of this section, submit your password.</div>
<form method="post" onsubmit="functionCookie(this); return false;">
<input required style="display: block; width: 69%; height: 50px; margin-right: 1%; float: left; border: 2px solid #333;" type="text" placeholder=" Password Here" name="password" id="functionUserPassword">
<input style="display: block; margin: 0px; width: 30%; height: 50px; background-color: #333; color: #fff;" type="submit" value="Submit">
</form>
<script>
function functionCookie(form){
document.cookie = "functionUserPassword=" + escape(form.functionUserPassword.value) + "; path=/#functioncode";
</script>';
}
return $return_html_content;
}
Here's the code which I am using with the content filter:
add_filter( 'the_content', 'wrap_html_in_shortcode', 9 );
function wrap_html_in_shortcode( $content ) {
if ( ! in_category( 'premium' ) ) return $content;
$content = preg_replace('/(<span[^>]*>\s*<div[^>]*>)/',"[protected]$1", $content);
$content = preg_replace('/(<\/div>\s*<\/span>)/', "$1[/protected]", $content);
return $content;
}
return false
in theonsubmit
tag; however, your JS has a syntax error - the functionfunctioncode
is never closed, i.e. it's missing the}
.