I've got a WP page containing a survey. The survey will be accessed by different people everyday (for simplicity say everyday for the whole year). I would like to password protect it and the password suppose to be changed everyday at midnight-ish. However to some extent the password should be formatted to be predictable for managers sharing the link to the survey (they can easily work-out what's today's password)- but not predictable to the end users :)

So for instance the password on the 2nd of Oct could look like

var day = currentDay
var month = currentMonth
var pass = "D!" + month + "K#" + day //formula passed to the managers

resulting in that day's password: D!10k#2

This solution doesn't have to be supa strong, it's just to protect the survey from overuse by "not expected" people. I've read this topic but it does not seem relevant that much.

Many thanks for reading and for any help :) Enjoy your Friday!


//EDIT It seems I would have to create a real CRON job triggering wp_cron and actually the code from the post I mentioned before would have to be modified to generate password according to the formula above. I know very little about php, so any help would be appreciated. No explanation regarding implementation is required though :)

//EDIT 2 Actually I find the post very useful, it just needs some tweaking, but I'm far from calling myself php delveloper so wanted ask for help.

I removed all scheduling code and left with a tiny bit of php code. Would the below snippet be enough to work if triggered by real CRON job? And what would be the formula to use marked below: FORMULA?

    add_action( 'wpse_change_pass_event', function()
        $slug = 'hello-world'; // Edit this post slug to your needs!

$formula = WHAT HERE? :)

       global $wpdb;
        array( 'post_password' => $formula ),
        array( 'post_name'     => $slug    ),
        array( '%s' ),
        array( '%s' )

It need not be very complicated. You can simply create a pattern like 2018-09-07-ABC (suffix of ABC) for the date 2018-09-07.

Set up the password creation/validation logic like password = md5('2018-09-07-ABC').

An administrator if they know the pattern, they can anytime goto any online md5 utility such as http://www.md5.cz and enter the pattern for current date.

Say for example today is December 25, 2018. Then they will enter 2018-12-25-ABC and the hash will be b21746c6db1c5323f28dd874fe60165f

TIP: You do not need to use entire 32 character password. Using first 8 characters (or any desired length) would be easier.

Sample PHP Code the generate password of length 8:

$md5hash  = md5(date('Ymd') . '-ABC');
$password = substr($md5hash, 0, 8);

Note: Take care of the timezone being used at the server when generating the password.

  • Thank you for the answer. I'm very weak in writing working php code :) Would snippet I added above work if triggered by real CRON job? And what code to use to for "formula" variable? Many thanks. – Rafał Sep 7 '18 at 12:33
  • I just added the sample php code to get the password for current date. – Izhar Aazmi Sep 7 '18 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.