Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wordpress @image directive returns image ID instead of get_field function when using ACF #50

Closed
plumthedev opened this issue Nov 19, 2020 · 4 comments · Fixed by #82
Closed
Labels
question Further information is requested

Comments

@plumthedev
Copy link

plumthedev commented Nov 19, 2020

When I using @image directive with ACF key, Blade renders it as wp_get_attachment_image(<image id here>, ...) instead of wp_get_attachment_image(get_field('acf key here'), ...). It is problem because image ID by ACF is dynamic and after change it in dashboard, changes does not make effect until rendered views will not be deleted. It is persistent on application in production mode and very dynamic content.

@Log1x
Copy link
Owner

Log1x commented Nov 20, 2020

Are you passing it as a string?

@Log1x Log1x added the question Further information is requested label Nov 20, 2020
@plumthedev
Copy link
Author

plumthedev commented Nov 20, 2020

Of course - my code looks like:

<div class="homepage-header-image">  
    @image('header.section.photo' , 'full', [])  
</div>  

and rendered blade looks like

<div class="homepage-header-image">  
    <?php echo wp_get_attachment_image(  
        22,  
        'full',  
        false,  
        []  
    ); ?>  
</div>  

So if I change image in Wordpress this change does not make effect until I don't clear rendered views. 
I find solution by returning call to get_field function instead of returning assigned image ID for moment of render.

I create own directive which is fair enough for this moment - code for it looks like duplicate of @image but changing put of first index if is ACF field.

$expression = Util::parse($expression);
$image = Util::strip($expression->get(0));

if (
   is_string($image) &&
   !is_numeric($image) &&
   Util::field($image)
    ) {
    $fieldKey = $expression->get(0);
    $expression = $expression->put(0, "get_field({$fieldKey})");
}

If we implement this on Wordpress directive this can be violation of SRP because WordPress do not have to worry about ACF. What do you think?

@kukac7
Copy link

kukac7 commented Sep 25, 2022

yes, unfortunately i have the same issue.

workaround:
@image(get_field('acf_image'))

@RobDobsonNC
Copy link

Hello - was there ever a permanent solution to this issue? It appears to be similar to:

https://discourse.roots.io/t/blade-directives-for-sage/14301/32

And I'm having the same issue, though the solution above from @kukac7 doesn't work for me, for some reason

@Log1x Log1x mentioned this issue Jul 29, 2023
@Log1x Log1x closed this as completed in #82 Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants