简体   繁体   English

每周在Wordpress single.php模板中执行一次PHP代码

[英]Execute PHP code once per week in a Wordpress single.php template

I have the below code in my single.php template. 我的single.php模板中包含以下代码。 It retrieves a price from an external website, then, if it's different than the existing price custom field, it updates the meta value. 它从外部网站检索价格,然后,如果它与现有的价格自定义字段不同,它将更新元值。

That part works as intended. 该部分按预期工作。 What I'd like to do, though, is only check this and update it once a week rather than with each page load. 不过,我想做的只是每周检查一次并更新一次,而不是每次加载页面时都要更新一次。

At first, I thought I could do it based on the post modified date, but apparently that doesn't change when updating post meta. 起初,我以为我可以根据修改后的日期执行此操作,但是显然更新更新后的元数据并不会改变。

If I could somehow incorporate this into functions.php to update all posts weekly, that would be even better. 如果我可以将其合并到functions.php中以每周更新所有帖子,那会更好。 But, it's fine if it only triggers on the post's load too. 但是,如果它也仅触发帖子的负载,那就很好。 I'm sure there's a way to schedule a cron for it, but I'm unfamiliar with programming crons. 我敢肯定有一种计划它的cron的方法,但是我不熟悉编程的cron。

<!-- Check external price -->
<?php 
    if(get_field('xpath_price')) { 
        libxml_use_internal_errors(true);
        $doc = new DomDocument();
        $url = get_field('_scrape_original_url');
        $doc->loadHTML(file_get_contents($url));
        $xpath = new DOMXPath($doc);
        $query = get_field('xpath_price');
        $metas = $xpath->query($query);
        foreach ($metas as $meta) {
            $priceexternal1 = preg_replace("/(.*?)(\.)(.*)/", "$1", $meta->nodeValue);
            $priceexternal = preg_replace("/[^0-9]/", "", $priceexternal1);
        }
        echo '<h3>External Price</h3>';
        echo $priceexternal;
    } 
?>

<!-- Update post_meta if different -->
<?php 
    if ($priceexternal && ($priceexternal) <> (get_field('price'))) {
        global $post;
        update_post_meta( $post->ID, 'price', $priceexternal ); 
        $priceout = $priceexternal;
    } elseif(get_field('price')) {
        $priceout = preg_replace("/[^0-9]/", "", get_field('price'));
    }
?>  

The whole wp-cron system can be a bit confusing for the uninitiated, although it definitely is the correct way of doing what you want. 整个wp-cron系统对于初学者来说可能会有些混乱,尽管它绝对是您想要做的正确方法。 However, if you're not happy getting to grips with it, you could use a simple transient set to expire after a set period (see Codex ) 但是,如果您不满意此方法,则可以使用一个简单的瞬态设置在设置的时间段后过期(请参阅Codex

For example... 例如...

if ( !get_transient( 'my-price-timer' ) ) { 
    // no transient exists, so process price check
    if(get_field('xpath_price')) {
        // etc
    }
    // now create the transient to say that we've done it
    set_transient( 'my-price-timer', 'done', WEEK_IN_SECONDS );
}

https://codex.wordpress.org/Function_Reference/wp_cron https://codex.wordpress.org/Function_Reference/wp_cron

 add_filter( 'cron_schedules', 'cron_add_weekly' );

 function cron_add_weekly( $schedules ) {
    // Adds once weekly to the existing schedules.
    $schedules['weekly'] = array(
        'interval' => 604800,
        'display' => __( 'Once Weekly' )
    );
    return $schedules;
 }

then 然后

if ( ! wp_next_scheduled( 'my_task_hook' ) ) {
  wp_schedule_event( time(), 'weekly', 'my_task_hook' );
}

add_action( 'my_task_hook', 'get_prices_function' );

function get_price_function() {
  // Your function
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM