每天更新一次插件形式

Update a form of a plugin once a day

我需要每天在 wordpress 后端保存一次选项页面(经典的“保存更改”)。 有什么办法吗? 我拿了一个插件并对其进行了修改以从外部站点获取黄金价格。现在的问题是我必须每天保存一次新价格,我想安排“提交”操作。

这里是代码:

    function woocommerce_gold_price_fields() {

        $karats          = get_option( 'woocommerce_gold_price_options' );
        $currency_pos    = get_option( 'woocommerce_currency_pos' );
        $currency_symbol = get_woocommerce_currency_symbol();

        ?>

            <table class="form-table widefat">
                <thead>
                    <tr valign="top">
                        <th scope="col" style="padding-left: 1em;"><?php esc_html_e( 'Karats', 'woocommerce-gold-price' )?></th>
                        <th scope="col"><?php esc_html_e( 'Price', 'woocommerce-gold-price' ) ?></td>
                        <th scope="col"><?php esc_html_e( 'Weight Unit', 'woocommerce-gold-price' ) ?></td>
                    </tr>
                </thead>




                    <?php 
$json = file_get_contents('https://data-asg.goldprice.org/dbXRates/EUR');
$decoded = json_decode($json);
$item = $decoded->items;
$date = $decoded->date;
$karats['24k'] = ($item[0]->xauPrice * 32.1507466);
        //echo $karats['24k'];
        
        $karats['22k'] = ($karats['24k'] * 0.9166);
        $karats['20k'] = ($karats['24k'] * 0.833);
        $karats['18k'] = ($karats['24k'] * 0.75);
        $karats['14k'] = ($karats['24k'] * 0.583);/**/
        
                    ?>

                <tr valign="top">
                    <th scope="row" style="padding-left: 1em;"><label for="woocommerce_gold_price_options_24">24k</label></th>
                    <td><?php

                    $input = ' <input style="vertical-align: baseline; text-align: right;" id="woocommerce_gold_price_options_24" name="woocommerce_gold_price_options[24k]" size="10" type="text" value="' . $karats['24k'] . '" /> ';

                    switch ( $currency_pos ) {
                        case 'left' :
                            echo $currency_symbol . $input;
                        break;
                        case 'right' :
                            echo $input . $currency_symbol;
                        break;
                        case 'left_space' :
                            echo $currency_symbol . '&nbsp;' . $input;
                        break;
                        case 'right_space' :
                            echo $input . '&nbsp;' . $currency_symbol;
                        break;
                    }

                ?>
                    </td>
                    <td> / <?php echo woocommerce_gold_price_weight_description(); ?></td>
                </tr>

                <tr valign="top" class="alternate">
                    <th scope="row" style="padding-left: 1em;"><label for="woocommerce_gold_price_options_22">22k</label></th>
                    <td>

                <?php

                $input = '<input style="vertical-align: baseline; text-align: right;" id="woocommerce_gold_price_options_22" name="woocommerce_gold_price_options[22k]" size="10" type="text" value="' . $karats['22k'] . '" />';

                switch ($currency_pos) {
                    case 'left' :
                        echo $currency_symbol . $input;
                    break;
                    case 'right' :
                        echo $input . $currency_symbol;
                    break;
                    case 'left_space' :
                        echo $currency_symbol . '&nbsp;' . $input;
                    break;
                    case 'right_space' :
                        echo $input . '&nbsp;' . $currency_symbol;
                    break;
                }

                ?>
                    </td>
                    <td> / <?php echo woocommerce_gold_price_weight_description(); ?></td>
                </tr>
                <tr valign="top">
                    <th scope="row" style="padding-left: 1em;"><label for="woocommerce_gold_price_options_18">18k</label></th>
                    <td>
                <?php

                $input = '<input style="vertical-align: baseline; text-align: right;" id="woocommerce_gold_price_options_18" name="woocommerce_gold_price_options[18k]" size="10" type="text" value="' . $karats['18k'] . '" />';

                switch ($currency_pos) {
                    case 'left' :
                        echo $currency_symbol . $input;
                    break;
                    case 'right' :
                        echo $input . $currency_symbol;
                    break;
                    case 'left_space' :
                        echo $currency_symbol . '&nbsp;' . $input;
                    break;
                    case 'right_space' :
                        echo $input . '&nbsp;' . $currency_symbol;
                    break;
                }
                ?>
                    </td>
                    <td> / <?php echo woocommerce_gold_price_weight_description(); ?></td>
                </tr>
                <tr valign="top" class="alternate">
                    <th scope="row" style="padding-left: 1em;"><label for="woocommerce_gold_price_options_14">14k</label></th>
                    <td>
                <?php

                $input = '<input style="vertical-align: baseline; text-align: right;" id="woocommerce_gold_price_options_14" name="woocommerce_gold_price_options[14k]" size="10" type="text" value="' . $karats['14k'] . '" />';

                switch ($currency_pos) {
                    case 'left' :
                        echo $currency_symbol . $input;
                    break;
                    case 'right' :
                        echo $input . $currency_symbol;
                    break;
                    case 'left_space' :
                        echo $currency_symbol . '&nbsp;' . $input;
                    break;
                    case 'right_space' :
                        echo $input . '&nbsp;' . $currency_symbol;
                    break;
                }
                ?>
                    </td>
                    <td> / <?php echo woocommerce_gold_price_weight_description(); ?></td>
                </tr>
            </table>
        <?php
    }

您可以使用时间表每天自动执行一次任务

// Schedule Cron Job Event
if ( ! wp_next_scheduled( 'register_price_gold_hook' ) ) {
    wp_schedule_event( time(), 'twicedaily', 'register_price_gold_hook' );
}

add_action('register_price_gold_hook', 'register_price_gold');

function register_price_gold() {

    // Update your Option Value with Gold Price

};