password - Adding a Cookie Session to Shortcode Content

I have a shortcode which "wraps" <pre> and <code> content in a password array. This was created a

I have a shortcode which "wraps" <pre> and <code> content in a password array. This was created a while back and I'm struggling on how to add a cookie or some sort of session handler to this setup so that a user with a password doesn't have to re-enter the password each and every time during a certain time frame.

As of right now, the user has to enter the password on each post to view the content.

I've looked into the setcookie but I do not understand how to incorporate that with the code I'm using (the code was not written by me and the developer is gone). I'm just the CSS designer of the site, trying to make sense out of this.

I understand that it should be something like this?

$cookie_name = "mycookie";
$cookie_value = $result; 
setcookie($cookie_name, $cookie_value,time()+3600*24,COOKIEPATH, COOKIE_DOMAIN);

This is the full code that's currently in use:

add_filter( 'the_content', 'wrap_code_in_shortcode' , 9 );
function wrap_code_in_shortcode($content) {

    $content = preg_replace('/(<pre[^>]*>\s*<code[^>]*>)/',"[protected_content]$1", $content);
    
    $content =  preg_replace('/(<\/code>\s*<\/pre>)/', "$1[/protected_content]", $content);

    return $content;
}

add_shortcode( 'protected_content', 'protected_password_content' );
function protected_password_content( $atts, $content=null ) {

    if (in_array(@$_REQUEST['password'], array('password'))){
        $return = do_shortcode($content);

    } else {

        $return = '
        <span>To view this section, enter your access code.</span><br>
            <form action="" method="post">
            <input style="display:block; width: 69%; height: 50px; margin-right: 1%; padding: 0px; float: left; border:1px solid blue;border-radius:3px;" type="text" placeholder="&#32;&#32;&#32;Access Code Here" name="password">
            <input style="display:block; margin: 0px;  width: 30%; height: 50px; padding: 0px;" type="submit" value="Show Content">
            </form>';
        }
    return $return;
}

I have a shortcode which "wraps" <pre> and <code> content in a password array. This was created a while back and I'm struggling on how to add a cookie or some sort of session handler to this setup so that a user with a password doesn't have to re-enter the password each and every time during a certain time frame.

As of right now, the user has to enter the password on each post to view the content.

I've looked into the setcookie but I do not understand how to incorporate that with the code I'm using (the code was not written by me and the developer is gone). I'm just the CSS designer of the site, trying to make sense out of this.

I understand that it should be something like this?

$cookie_name = "mycookie";
$cookie_value = $result; 
setcookie($cookie_name, $cookie_value,time()+3600*24,COOKIEPATH, COOKIE_DOMAIN);

This is the full code that's currently in use:

add_filter( 'the_content', 'wrap_code_in_shortcode' , 9 );
function wrap_code_in_shortcode($content) {

    $content = preg_replace('/(<pre[^>]*>\s*<code[^>]*>)/',"[protected_content]$1", $content);
    
    $content =  preg_replace('/(<\/code>\s*<\/pre>)/', "$1[/protected_content]", $content);

    return $content;
}

add_shortcode( 'protected_content', 'protected_password_content' );
function protected_password_content( $atts, $content=null ) {

    if (in_array(@$_REQUEST['password'], array('password'))){
        $return = do_shortcode($content);

    } else {

        $return = '
        <span>To view this section, enter your access code.</span><br>
            <form action="" method="post">
            <input style="display:block; width: 69%; height: 50px; margin-right: 1%; padding: 0px; float: left; border:1px solid blue;border-radius:3px;" type="text" placeholder="&#32;&#32;&#32;Access Code Here" name="password">
            <input style="display:block; margin: 0px;  width: 30%; height: 50px; padding: 0px;" type="submit" value="Show Content">
            </form>';
        }
    return $return;
}
Share Improve this question asked Jul 5, 2020 at 5:10 user191091user191091
Add a comment  | 

1 Answer 1

Reset to default 0

Welcome to WordPress Development StackExchange site! In general you understand everything right, but there can be one important caveat: if some page content like page header, navigation menu, etc is already sent to the user, a try to set cookie from PHP script will give a following error: "Cannot modify header information – headers already sent by...". To avoid it you can add some javascript to your form and set a cookie from this javascript. Lets try the following code:

function protected_password_content( $atts, $content=null ) {

    // if password is sent via posting form, try to use it first
    // next try to get password from the cookie
    $userpass = isset( $_REQUEST['password'] ) ? $_REQUEST['password'] : ( isset( $_COOKIE['userpass'] ) ? $_COOKIE['userpass'] : NULL );

    if ( in_array( $userpass, array('password') ) ) {
        $return = do_shortcode($content);
    } else {
        $return = '
            <span>To view this section, enter your access code.</span><br>
            <form action="" method="post" onsubmit="putCookie(this)">
            <input style="display:block; width: 69%; height: 50px; margin-right: 1%; padding: 0px; float: left; border:1px solid blue;border-radius:3px;" type="text" placeholder="&#32;&#32;&#32;Access Code Here" name="password" id="userpass">
            <input style="display:block; margin: 0px;  width: 30%; height: 50px; padding: 0px;" type="submit" value="Show Content">
            </form>
            <script>
            function putCookie(form) {
                var today = new Date();
                var expiry = new Date(today.getTime() + 24 * 3600 * 1000); // plus 1 day
                document.cookie = "userpass=" + escape(form.userpass.value) + "; path=/; expires=" + expiry.toGMTString();
            }
            </script>';
    }
    return $return;
}

You do not required to specify the cookie expire time. If you omit that part, the cookie will be set as so-called session cookie, which would be valid only until user closes his browser:

function putCookie(form) {
    document.cookie = "userpass=" + escape(form.userpass.value) + "; path=/";
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742291964a4416362.html

相关推荐

  • password - Adding a Cookie Session to Shortcode Content

    I have a shortcode which "wraps" <pre> and <code> content in a password array. This was created a

    15小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信