admin管理员组文章数量:1122846
Objective
I am working on a WordPress site built using Timber/Twig. I have a plugin (built using standard PHP and saved in the standard plugins folder) that is supposed to grab ACF values from a Repeater field's subfields, and save them in an array. I then save that array in a global variable called $homepage_collage_images
which I can then access in my Timber Context File (I have confirmed that this global variable is accessible in my context file and that I can access its contents).
Notes Because of other functions within my plugin (related to caching and other things), the code in question needs to be able to run within the plugin and not be called within the context file: otherwise the rest of the plugin won't work as intended (but that's outside of the context of this message).
I have WP Super Cache activated on my site as well, as I am building my plugin based off of some of its functionality related to caching. The only special settings activated with the WP Super Cache plugin are dynamic caching
and late init
. Late init has the following description: "Display cached files after WordPress has loaded.". Dynamic caching is required for my plugin to work, so turning it off isn't really an option. I have disabled both of these settings in my troubleshooting phase and neither seemed to be affecting anything, as my code still wasn't working as intended.
The Problem In my custom plugin, my code does not grab the ACF field values. This is the code I am using to grab the values and save them in an array:
function get_images_and_update_context() {
// Homepage Image Array
global $homepage_collage_images;
$homepage_collage_images = array();
// Get the ID of the current post
$post_id = get_the_ID();
if (have_rows('collage_homepage_image_variations', $post_id)) {
// Loop through the rows of the repeater field
while (have_rows('collage_homepage_image_variations', $post_id)) {
the_row();
// Get values of subfields for the current row
$image_640 = get_sub_field('collage_image_640');
$image_1024 = get_sub_field('collage_image_1024');
$image_320_1 = get_sub_field('collage_image_320_1');
$image_320_2 = get_sub_field('collage_image_320_2');
// Create an array to store the subfield values for the current row
$row_images = array(
'image_640' => $image_640,
'image_1024' => $image_1024,
'image_320_1' => $image_320_1,
'image_320_2' => $image_320_2
);
// Add the row array to the main homepage_collage_images array
$homepage_collage_images[] = $row_images;
}
}
var_dump("PLUGIN Array Dump: ", $homepage_collage_images); // Debugging statement
}
add_action('acf/init', 'get_images_and_update_context');
I have the var_dump
there as a debugging message to ensure this function is actually running (it is!). But the output from the var_dump
is string(19) "PLUGIN Array Dump: " array(0) { }
, so it doesn't look like the array has anything in it. I have tried omitting the $post_id
stuff as well, as I don't really think it is necessary...but it doesn't work without it either, so I put it in there to see if it fixed anything.
Troubleshooting I Have Done
I made sure that my code was working as intended by copy/pasting and running the get_images_and_udpate_context
function within the Timber context file itself and confirmed that it DOES grab the data correctly, as the var_dump
shows the array has been populated with the correct data. So, functionally the code seems to be arranged and setup properly, but within my plugin it will not grab the ACF values. My guess is that my plugin is running before the ACF Fields are ready to grab, but I don't know how to confirm that. As you can see, I am using add_action('acf/init', 'get_images_and_update_context');
to get this function running within the plugin, and I figured that using acf/init
as the hook would fix that potential issue.
So, that is about where I am at with this problem and am left scratching my head. If anyone has dealt with something similar or has any input, I'd really appreciate it!
本文标签: phpCannot Access ACF Field Values via my Plugin
版权声明:本文标题:php - Cannot Access ACF Field Values via my Plugin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736311746a1934848.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论