I am trying to make some ACF fields data available to Javascript.
I have my localize_script() set up in my add_action() filter in Setup.php:
add_action('wp_enqueue_scripts', function () {
wp_enqueue_style('sage/main.css', asset_path('styles/main.css'), false, null);
wp_enqueue_script('sage/main.js', asset_path('scripts/main.js'), ['jquery'], null, true);
if (is_single() && comments_open() && get_option('thread_comments')) {
wp_enqueue_script('comment-reply');
}
wp_localize_script('sage/main.js', 'testimonialQuote', \App\Controllers\Testimonials::testimonialQuote());
}, 100);
I want to this field to be accessible to my testimonials.blade.php view. Note: this this view is in a blocks folder if that matters.
Here is my controller:
<?php
namespace App\Controllers;
use Sober\Controller\Controller;
class Testimonials extends Controller {
public static function testimonialQuote() {
return get_field('testimonial_quote');
}
}
Iâve tested this out by replacing âget_field(âtestimonial_quoteâ)â with a string and the javascript in my common.js is able to retrieve it.
To further debug, I tried accessing this in my testimonials.blade.php file like so:
@php
$testimonial_quote = Testimonials::testimonialQuote();
@endphp
<div class="testimonials-wrapper">
@if($testimonial_quote)
<p>{!!$testimonial_quote!!}</p>
@else <p>nope!</p>
@endif
</div>
It also returns null here. But when I attempt to render it directly in the above blade file using the get_field method, it works.
Therefore, there is something wrong with my controller, but the answers that have worked for others in this Discourse have not resolved the issue for me. Any idea what I am missing?