Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在wordpress中自动更新数据?_Php_Wordpress - Fatal编程技术网

Php 如何在wordpress中自动更新数据?

Php 如何在wordpress中自动更新数据?,php,wordpress,Php,Wordpress,我创建帖子类型“member”。我有公司员工的API。我从这个API中获取员工“ID”、“日期”、“开始工作”和“完成工作”的信息。 每天都有新的数据,因为每个员工都没有标准的工作时间图表,他们想什么时候工作就什么时候工作。所以数据总是不同的。 在这个帖子类型中,我创建了3个元字段,其中我只添加了“ID”。 “开始工作”和“完成工作”将从API自动解析并添加到此字段。 这些数据每天都在变化,在我的元字段中也会发生变化,但它不会自动将这些数据保存到数据库中。 只有当我在管理面板中编辑页面时,它才会

我创建帖子类型“member”。我有公司员工的API。我从这个API中获取员工“ID”、“日期”、“开始工作”和“完成工作”的信息。 每天都有新的数据,因为每个员工都没有标准的工作时间图表,他们想什么时候工作就什么时候工作。所以数据总是不同的。 在这个帖子类型中,我创建了3个元字段,其中我只添加了“ID”。 “开始工作”和“完成工作”将从API自动解析并添加到此字段。 这些数据每天都在变化,在我的元字段中也会发生变化,但它不会自动将这些数据保存到数据库中。 只有当我在管理面板中编辑页面时,它才会更改

<?php
        function add_member_post_type1_meta_box() {
        add_meta_box(
            'member_post_type1_meta_box',
            'Today Working hours',
            'show_member_post_type1_meta_box',
            'member',
            'normal',
            'high'
        );

    }

    add_action('add_meta_boxes', 'add_member_post_type1_meta_box');

    function user_worker($user_id) {
        global $user_today, $work_today, $break_today, $member_post_type1_meta_fields;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "API_DOMAIN");
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);

    //convert json to array
        $worker_shifts = json_decode($result, true);

    //set correct timezone for date functions
        date_default_timezone_set("Europe/Helsinki");

    //get current hour
        $current_hour = date("G");

        $ID = $user_id;
        $user_today = array();
        $work_today = false;
        foreach($worker_shifts as $worker) {
            if (in_array($ID, $worker)) {
                $user_today = $worker;
                $work_today = true;
            }
        }
        $break_today = sizeof($user_today["shifts"])-1;
        if ($break_today>0) {
            array_push($member_post_type1_meta_fields,
                array(
                    'label'=> 'Break',
                    'id'    => 'custom_break',
                    'type'  => 'break'
                )
            );
            for ($i=0;$i<$break_today;$i++) {
                array_push($member_post_type1_meta_fields,
                    array(
                        'label'=> 'Start work day at',
                        'id'    => 'custom_start_'.$i,
                        'type'  => 'start_'.$i
                    ),
                    array(
                        'label'=> 'Finish work day at',
                        'id'    => 'custom_finish_'.$i,
                        'type'  => 'finish_'.$i
                    )
                );
            }
        }
    }

    // Field Array
    $prefix = 'custom_';
    $member_post_type1_meta_fields = array(
        array(
            'label'=> 'Day',
            'id'    => $prefix.'day',
            'type'  => 'day'
        ),
        array(
            'label'=> 'Start work day at',
            'id'    => $prefix.'start',
            'type'  => 'start'
        ),
        array(
            'label'=> 'Finish work day at',
            'id'    => $prefix.'finish',
            'type'  => 'finish'
        ),
    );

    // The Callback
    function show_member_post_type1_meta_box() {
        global $member_post_type1_meta_fields, $user_id, $post, $user_today, $work_today, $break_today, $wpdb;
        user_worker($user_id);
    // Use nonce for verification
        echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

    // Begin the field table and loop
        echo '<table class="form-table">';
        foreach ($member_post_type1_meta_fields as $field) {
            // get value of this field if it exists for this post
            $meta = get_post_meta($post->ID, $field['id'], true);
            // begin a table row with
            echo '<tr>
            <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
            <td>';
            if ($work_today) {

                switch($field['type']) {
                    // case items will go here
                    case 'day':
                        echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$user_today["date"].'" style="width: 100%; height: 40px;" />
                <br />';
                        update_post_meta($post->ID, $field['id'], $user_today["date"]);
                        break;
                    case 'break':
                        echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$break_today.'" style="width: 100%; height: 40px;" />
                <br />';
                        update_post_meta($post->ID, $field['id'], $break_today);
                        break;
                    case 'start':
                        echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.intval($user_today["shifts"][0][0]).'" style="width: 100%; height: 40px;" />
                <br />';
                        update_post_meta($post->ID, $field['id'], intval($user_today["shifts"][0][0]));
                        break;
                    case 'finish':
                        echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.intval($user_today["shifts"][0][1]).'" style="width: 100%; height: 40px;" />
                <br />';
                        update_post_meta($post->ID, $field['id'], intval($user_today["shifts"][0][1]));
                        break;
                } //end switch
                $wpdb->update( 'r4j47_postmeta',
                    array( 'custom_day' => $user_today["date"], 'custom_break' => $break_today, 'custom_start' => intval($user_today["shifts"][0][0]), 'custom_finish' => intval($user_today["shifts"][0][1]) ),
                    array( 'post_id' => $post->ID ),
                    array( '%s', '%d', '%d', '%d' ),
                    array( '%d' )
                );
                if ($break_today>0) {
                    for ($i=0;$i<$break_today;$i++) {
                        if ($field['type']=='start_'.$i) {
                            echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.intval($user_today["shifts"][$i+1][0]).'" style="width: 100%; height: 40px;" />
                        <br />';
                            update_post_meta($post->ID, $field['id'], intval($user_today["shifts"][$i+1][0]));
                        }
                        if ($field['type']=='finish_'.$i) {
                            echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.intval($user_today["shifts"][$i+1][1]).'" style="width: 100%; height: 40px;" />
                        <br />';
                            update_post_meta($post->ID, $field['id'], intval($user_today["shifts"][$i+1][1]));
                        }
                        $wpdb->update( 'r4j47_postmeta',
                            array( 'custom_start_'.$i => intval($user_today["shifts"][$i+1][0]), 'custom_finish_'.$i => intval($user_today["shifts"][$i+1][1]) ),
                            array( 'post_id' => $post->ID ),
                            array( '%d', '%d' ),
                            array( '%d' )
                        );
                    }
                }
            } else {
                switch($field['type']) {
                    // case items will go here
                    case 'day':
                        echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="" style="width: 100%; height: 40px;" />
                <br />';
                        delete_post_meta($post->ID, $field['id']);
                        break;
                    case 'start':
                        echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="" style="width: 100%; height: 40px;" />
                <br />';
                        delete_post_meta($post->ID, $field['id']);
                        break;
                    case 'finish':
                        echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="" style="width: 100%; height: 40px;" />
                <br />';
                        delete_post_meta($post->ID, $field['id']);
                        break;
                    case 'break':
                        echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="" style="width: 100%; height: 40px;" />
                <br />';
                        delete_post_meta($post->ID, $field['id']);
                        break;
                } //end switch
                $wpdb->update( 'r4j47_postmeta',
                    array( 'custom_day' => '', 'custom_break' => '', 'custom_start' => '', 'custom_finish' => '', 'custom_start_0' => '', 'custom_finish_0' => ''),
                    array( 'post_id' => $post->ID ),
                    array( '%s', '%d', '%d', '%d', '%d', '%d' ),
                    array( '%d' )
                );
            }
            echo '</td></tr>';
        } // end foreach
        echo '</table>'; // end table
    }

    // Save the Data
    function save_member_post_type1_meta($post_id) {
        global $member_post_type1_meta_fields;

    // verify nonce
        if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
            return $post_id;
    // check autosave
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
            return $post_id;
    // check permissions
        if ('page' == $_POST['post_type']) {
            if (!current_user_can('edit_page', $post_id))
                return $post_id;
        } elseif (!current_user_can('edit_post', $post_id)) {
            return $post_id;
        }

    // loop through fields and save the data
        foreach ($member_post_type1_meta_fields as $field) {
            $old = get_post_meta($post_id, $field['id'], true);
            $new = $_POST[$field['id']];
            if ($new && $new != $old) {
                update_post_meta($post_id, $field['id'], $new);
            } elseif ('' == $new && $old) {
                delete_post_meta($post_id, $field['id'], $old);
            }
        } 
// end foreach
    }
    add_action('save_post', 'save_member_post_type1_meta');
?>

您可以运行wp cron,以便在指定的时间或定期间隔自动执行某些操作

 //If your code is in plugin, On plugin activation
 register_activation_hook(__FILE__, 'schedule_time');

 //Or use init action instead of register_activation_hook, Add function to   register event to WordPress init
 add_action( 'init', 'schedule_time');

 function schedule_time() {
 //Schedule on daily basis
     wp_schedule_event(time(), 'daily', 'your_main_function_call');
 }

 function your_main_function_call() {
     // this will execute daily
 }

我认为您必须创建一个cron作业,并根据服务器上的时间设置它。这是自动更新的最佳方式。wp_schedule_event()在一段时间内未按照我们的要求工作