1

I want to generate 12 digit unique code with wordpress rest api which refresh every six hours. I didn't find any code or method for my requirement.

Example URL: ....domainname.com/wp-json/astra-sites/v1/get-last-unique-code

Json code on above link:

{"last_unique_code":"4qN6Ixy0&2!H"}

This code refresh every six hours. Please give me code or details to generate same code which refresh every six hours.

I know how to do custom "register_rest_route" to create custom api path but I didn't find any method to refresh code automatically with php and wordress.

I am using below code

class My_Rest_APIs {
    
    public function __construct() {
        add_action( 'rest_api_init', array( $this, 'create_api_posts_meta_field' ) );
    }

    public function create_api_posts_meta_field() {
        register_rest_route('my-custom-pages/v1', 'get-last-unique-code', [
            'methods' => 'GET',
            'callback' => array( $this, 'last_unique_code' )
        ]);
    }

    public function last_unique_code() {
        $pass = substr(str_shuffle("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstvwxyz@#$%&"), 0, 12);
        $checksum = array('last_unique_code' => $pass);
        return $checksum;
    }

}

Above code generates unique code but I don't know how to store it in database which refresh every six hours. I tried update_option() method but it doesn't work.

Thank you.

MaxCode
  • 13
  • 3
  • 1
    how about a cron job which runs every 6 hours, generates the code and stores it in the database? Then the PHP code which displays it will just get the latest value from the database. – ADyson Mar 04 '21 at 09:23
  • 1
    Or on-the-fly creation, with a _seed_ value for the random generator, that stays constant for each six hour interval. – CBroe Mar 04 '21 at 09:43
  • 1
    Try to use Transient API of WordPress that will helpful to manage time. You cen set,get,delete transient for specific interval of time – Momin IqbalAhmed Mar 04 '21 at 10:30
  • Can you please give me sample code for on-the-fly or cron job so I can try? – MaxCode Mar 04 '21 at 11:04
  • Why, where exactly are you stuck? Is the problem generating the random code? Or inserting into the database? or how to set up a cron schedule? Or something else? Don't just ask us to provide generic code for you (especially when we don't know anything about your existing application or how it would fit in) - instead, break the task down into its constituent parts, research each part and try to understand and implement it. If you get stuck, ask us about your particular issue. We're not a free write-my-code or tutorial service, but we'll _help_ you with a specific, definable programming problem – ADyson Mar 04 '21 at 11:09
  • P.S. Please review the [tour](https://stackoverflow.com/tour) page and the [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) article to better understand how the site works and how you should approach your questions. – ADyson Mar 04 '21 at 11:10
  • Thank you @ADyson for your suggestion. This is my first post so I am learning post rules. I added the code. – MaxCode Mar 04 '21 at 11:36

1 Answers1

0

Executed below updated code snippet of yours. When you run SITE_URL/wp-json/my-custom-pages/v1/get-last-unique-code you will find the value of your checksum which will be updated every six hours

class My_Rest_APIs {
    
    public function __construct() {
        add_action( 'rest_api_init', array( $this, 'create_api_posts_meta_field' ) );
    }

    public function create_api_posts_meta_field() {
        register_rest_route('my-custom-pages/v1', 'get-last-unique-code', [
            'methods' => 'GET',
            'callback' => array( $this, 'last_unique_code' )
        ]);
    }

    public function last_unique_code() {
        
        // Get any existing copy of our transient data
        if ( false === ( $pass = get_transient( 'pass_key' ) ) ) {
            // It wasn't there, so regenerate the data and save the transient
            $pass = substr(str_shuffle("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstvwxyz@#$%&"), 0, 12);
            set_transient( 'pass_key', $pass, 6 * HOUR_IN_SECONDS );
        }

        $checksum = array('last_export_checksums' => $pass);
        return $checksum;
    }

}

new My_Rest_APIs();
Momin IqbalAhmed
  • 950
  • 6
  • 13
  • Thanks a million, Iqbal. Your code is working perfectly. I appreciate your taking the time to answer my query. It has helped me :) :) :) – MaxCode Mar 04 '21 at 13:34