I am trying to extract data from the feed when publishing a post based on the url_address_web field and then fill in the other fields according to the available feed. All indications are that it has succeeded, however the fields remain blank after the post is updated. I would rather achieve updating posts in post-type "web" on a daily basis, however, at the moment, populating the ACF fields is not working for me either.
**functions.php **
// Funkce pro získání dat ze feedu
function get_data_from_feed($feed_url) {
$feed_data = simplexml_load_file($feed_url);
$data = array();
if ($feed_data) {
$data['wordpress_version'] = (string) $feed_data->wordpress_version;
$data['php_version'] = (string) $feed_data->php_version;
$data['storage_usage'] = (string) $feed_data->storage_usage;
// Aktivní pluginy
$active_plugins = array();
foreach ($feed_data->active_plugins->plugin as $plugin) {
$active_plugins[] = (string) $plugin;
}
$data['active_plugins'] = $active_plugins;
// Pluginy k aktualizaci
$update_plugins = array();
foreach ($feed_data->update_plugins->plugin as $update_plugin) {
$update_plugin_data = array(
'name' => (string) $update_plugin->name,
'current_version' => (string) $update_plugin->current_version,
'new_version' => (string) $update_plugin->new_version,
);
$update_plugins[] = $update_plugin_data;
}
$data['update_plugins'] = $update_plugins;
// Uživatelé
$users = array();
foreach ($feed_data->users->user as $user) {
$users[] = (string) $user;
}
$data['users'] = $users;
}
// Logování do souboru
file_put_contents(__DIR__ . '/feed.txt', 'Fetching data from feed URL: ' . $feed_url . PHP_EOL, FILE_APPEND);
file_put_contents(__DIR__ . '/feed.txt', 'Feed data: ' . print_r($data, true) . PHP_EOL, FILE_APPEND);
return $data;
}
// Funkce pro aktualizaci ACF polí
function update_acf_fields($post_id, $data_from_feed) {
// Aktualizace jednotlivých polí dle potřeby
update_field('verze_wordpress', $data_from_feed['wordpress_version'], $post_id);
update_field('php_verze', $data_from_feed['php_version'], $post_id);
update_field('storage_usage', $data_from_feed['storage_usage'], $post_id);
update_field('aktivni_pluginy', $data_from_feed['active_plugins'], $post_id);
// Aktualizace pole "Pluginy k aktualizaci"
$update_plugins = array();
foreach ($data_from_feed['update_plugins'] as $plugin) {
$update_plugins[] = array(
'nazev_pluginu' => $plugin['name'],
'aktualni_verze' => $plugin['current_version'],
'nova_verze' => $plugin['new_version'],
);
}
update_field('pluginy_k_aktualizaci', $update_plugins, $post_id);
// Aktualizace pole "Aktivní uživatelé"
update_field('aktivni_uzivatele', $data_from_feed['users'], $post_id);
// Logování do souboru
file_put_contents(__DIR__ . '/feed.txt', 'Updating ACF fields for post ID: ' . $post_id . PHP_EOL, FILE_APPEND);
file_put_contents(__DIR__ . '/feed.txt', 'ACF fields updated successfully for post ID: ' . $post_id . PHP_EOL, FILE_APPEND);
}
// Funkce pro aktualizaci ACF polí pro opakující se bloky
function update_acf_repeater_field($field_key, $sub_field_keys, $data, $post_id) {
$repeater_data = array();
foreach ($data as $item) {
$row_data = array();
foreach ($sub_field_keys as $sub_field_key) {
if (isset($item[$sub_field_key])) {
$row_data[$sub_field_key] = $item[$sub_field_key];
}
}
$repeater_data[] = $row_data;
}
update_field($field_key, $repeater_data, $post_id);
}
// Hook pro spuštění akce po publikaci postu
add_action('publish_web', 'update_acf_fields_on_publish', 10, 2);
// Funkce spuštěná po publikaci postu
function update_acf_fields_on_publish($ID, $post) {
// Zkontroluj, zda jde o post typu "web"
if ($post->post_type === 'web') {
// Získání URL feedu z ACF pole
$web_url = get_field('url_adresa_webu', $ID);
// Pokud je URL adresa webu k dispozici, pokračujeme
if ($web_url) {
// Automatická generace URL feedu
$feed_url = $web_url . '/wp-content/plugins/webfusion-connector/feeds/stav-webu.xml';
// Získání dat z feedu
$data_from_feed = get_data_from_feed($feed_url);
// Aktualizace ACF polí
update_acf_fields($ID, $data_from_feed);
// Zkontrolujme hodnotu ACF pole po aktualizaci
$verze_wordpress = get_field('verze_wordpress', $ID);
error_log('Value of ACF field verze_wordpress after update: ' . print_r($verze_wordpress, true));
// Aktualizace pole "Speciální pole" (příklad pro opakující se bloky)
$special_field_key = 'specielni_pole';
$special_sub_field_keys = array('nazev', 'popis'); // Přizpůsobte svým potřebám
if (isset($data_from_feed['special_data'])) {
update_acf_repeater_field($special_field_key, $special_sub_field_keys, $data_from_feed['special_data'], $ID);
}
// Výpis pro ladění
error_log('ACF update completed for post ID: ' . $ID);
} else {
// Výpis pro ladění, pokud URL adresa webu není k dispozici
error_log('ACF update skipped. Web URL is not available for post ID: ' . $ID);
}
}
}
// Hook pro plánovanou úlohu
add_action('wpse_daily_cron', 'update_acf_fields_daily');
// Funkce spuštěná každý den
function update_acf_fields_daily() {
// Získání všech postů typu "web"
$web_posts = get_posts(array('post_type' => 'web', 'posts_per_page' => -1));
// Pro každý post provedeme aktualizaci ACF polí
foreach ($web_posts as $web_post) {
// Získání URL feedu z ACF pole
$web_url = get_field('url_adresa_webu', $web_post->ID);
// Automatická generace URL feedu
$feed_url = $web_url . '/wp-content/plugins/webfusion-connector/feeds/stav-webu.xml';
// Výpis pro ladění
error_log('Starting ACF update for post ID: ' . $web_post->ID);
// Získání dat z feedu
$data_from_feed = get_data_from_feed($feed_url);
// Aktualizace ACF polí
update_acf_fields($web_post->ID, $data_from_feed);
// Výpis pro ladění
error_log('ACF update completed for post ID: ' . $web_post->ID);
}
}
// Nastavení plánované úlohy při aktivaci pluginu
register_activation_hook(__FILE__, 'wpse_schedule_cron');
function wpse_schedule_cron() {
if (!wp_next_scheduled('wpse_daily_cron')) {
wp_schedule_event(time(), 'daily', 'wpse_daily_cron');
}
}
// Zrušení plánované úlohy při deaktivaci pluginu
register_deactivation_hook(__FILE__, 'wpse_unschedule_cron');
function wpse_unschedule_cron() {
wp_clear_scheduled_hook('wpse_daily_cron');
}
ACF PHP export:
acf_add_local_field_group( array(
'key' => 'group_65e4c721c4b74',
'title' => 'Pole k webům',
'fields' => array(
array(
'key' => 'field_65e4c722f9f66',
'label' => 'URL adresa webu',
'name' => 'url_adresa_webu',
'aria-label' => '',
'type' => 'url',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'hide_admin' => 0,
'default_value' => '',
'placeholder' => '',
'acfe_field_group_condition' => 0,
),
array(
'key' => 'field_65e4c73ef9f67',
'label' => 'Verze wordpress',
'name' => 'verze_wordpress',
'aria-label' => '',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'hide_admin' => 0,
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'acfe_field_group_condition' => 0,
),
array(
'key' => 'field_65e4dc05c8fad',
'label' => 'storage usage',
'name' => 'storage_usage',
'aria-label' => '',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'hide_admin' => 0,
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'acfe_field_group_condition' => 0,
),
array(
'key' => 'field_65e4c752f9f68',
'label' => 'PHP verze',
'name' => 'php_verze',
'aria-label' => '',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'hide_admin' => 0,
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'acfe_field_group_condition' => 0,
),
array(
'key' => 'field_65e4c768f9f69',
'label' => 'Aktivní pluginy',
'name' => 'aktivni_pluginy',
'aria-label' => '',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'hide_admin' => 0,
'acfe_repeater_stylised_button' => 0,
'layout' => 'table',
'pagination' => 0,
'min' => 0,
'max' => 0,
'collapsed' => '',
'button_label' => 'Přidat řádek',
'acfe_field_group_condition' => 0,
'rows_per_page' => 20,
'sub_fields' => array(
array(
'key' => 'field_65e4c774f9f6a',
'label' => 'Název pluginu',
'name' => 'nazev_pluginu',
'aria-label' => '',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'hide_admin' => 0,
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'acfe_field_group_condition' => 0,
'parent_repeater' => 'field_65e4c768f9f69',
),
),
),
array(
'key' => 'field_65e4c786f9f6b',
'label' => 'Pluginy k aktualizaci',
'name' => 'pluginy_k_aktualizaci',
'aria-label' => '',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'hide_admin' => 0,
'acfe_repeater_stylised_button' => 0,
'layout' => 'table',
'pagination' => 0,
'min' => 0,
'max' => 0,
'collapsed' => '',
'button_label' => 'Přidat řádek',
'acfe_field_group_condition' => 0,
'rows_per_page' => 20,
'sub_fields' => array(
array(
'key' => 'field_65e4c7a3f9f6c',
'label' => 'Název pluginu',
'name' => 'nazev_pluginu',
'aria-label' => '',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'hide_admin' => 0,
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'acfe_field_group_condition' => 0,
'parent_repeater' => 'field_65e4c786f9f6b',
),
array(
'key' => 'field_65e4c7aff9f6d',
'label' => 'Aktuální verze',
'name' => 'aktualni_verze',
'aria-label' => '',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'hide_admin' => 0,
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'acfe_field_group_condition' => 0,
'parent_repeater' => 'field_65e4c786f9f6b',
),
array(
'key' => 'field_65e4c7b5f9f6e',
'label' => 'Nová verze',
'name' => 'nova_verze',
'aria-label' => '',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'hide_admin' => 0,
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'acfe_field_group_condition' => 0,
'parent_repeater' => 'field_65e4c786f9f6b',
),
),
),
array(
'key' => 'field_65e4c7c2f9f6f',
'label' => 'Aktivní uživatelé',
'name' => 'aktivni_uzivatele',
'aria-label' => '',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'hide_admin' => 0,
'acfe_repeater_stylised_button' => 0,
'layout' => 'table',
'pagination' => 0,
'min' => 0,
'max' => 0,
'collapsed' => '',
'button_label' => 'Přidat řádek',
'acfe_field_group_condition' => 0,
'rows_per_page' => 20,
'sub_fields' => array(
array(
'key' => 'field_65e4c7d2f9f70',
'label' => 'Uživatel',
'name' => 'uzivatel',
'aria-label' => '',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'hide_admin' => 0,
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'acfe_field_group_condition' => 0,
'parent_repeater' => 'field_65e4c7c2f9f6f',
),
),
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'web',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'left',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
'show_in_rest' => 1,
'acfe_autosync' => '',
'acfe_form' => 0,
'acfe_display_title' => '',
'acfe_meta' => '',
'acfe_note' => '',
) );
**Report from feed.txt log **
Fetching data from feed URL: https://vas-webmaster.cz/wp-content/plugins/webfusion-connector/feeds/stav-webu.xml
Feed data: Array
(
[wordpress_version] => 6.4.3
[php_version] => 8.1.27
[storage_usage] => 247.31 GB
[active_plugins] => Array
(
[0] => All in One SEO
[1] => Assets4Breakdance
[2] => Breakdance
[3] => Destiny Elements
[4] => Site Kit by Google
[5] => SR Toolkit update
[6] => SR Automatic Upgrade
[7] => Webfusion connector - site status
[8] => WooCommerce
[9] => WP Rocket
[10] => WPC AJAX Add to Cart for WooCommerce
[11] => WPS Hide Login
)
[update_plugins] => Array
(
[0] => Array
(
[name] => All in One SEO
[current_version] => 4.5.2.1
[new_version] => 4.5.7.3
)
[1] => Array
(
[name] => CDN Enabler
[current_version] => 2.0.6
[new_version] => 2.0.8
)
[2] => Array
(
[name] => Site Kit by Google
[current_version] => 1.115.0
[new_version] => 1.121.0
)
[3] => Array
(
[name] => WooCommerce
[current_version] => 8.4.0
[new_version] => 8.6.1
)
[4] => Array
(
[name] => WPC AJAX Add to Cart for WooCommerce
[current_version] => 1.6.6
[new_version] => 2.0.0
)
[5] => Array
(
[name] => WPS Hide Login
[current_version] => 1.9.10
[new_version] => 1.9.13.2
)
[6] => Array
(
[name] => Destiny Elements
[current_version] => 1.4.2
[new_version] => 1.8
)
[7] => Array
(
[name] => SR Automatic Upgrade
[current_version] => 4.5
[new_version] => 4.7
)
)
[users] => Array
(
[0] => vodys
)
)
Updating ACF fields for post ID: 844
ACF fields updated successfully for post ID: 844
Server monitor error:
2024-03-04 09:42:04 Error 78.136.162.96 AH01071: Got error 'PHP message: Value of ACF field verze_wordpress after update: 6.4.3PHP message: ACF update completed for post ID: 844', referer: https://wp-web.dev/wp-admin/post.php?post=844&action=edit Chyba Apache
Nemáte tušení, kde by mohl být problém? Thank you!
I tried replacing the acf field names with key.