admin管理员组文章数量:1122846
I'm trying to redirect users directly to the URL of the file located in a post using a File field from ACF. Something like:
<?php function redirect_to_acf_file_url() {
$file_url = get_field('file_url');
if (!empty($file_url)) {
wp_redirect($file_url, 301);
exit;
} }
do_action ('redirect_to_acf_file_url'); ?>
This would enable me to share the URL of the post and when users accesses the post URL, they'll go straight to the File url.
Right now, the above PHP just displays a blank page.
Would appreciate any help.
Thanks!
I'm trying to redirect users directly to the URL of the file located in a post using a File field from ACF. Something like:
<?php function redirect_to_acf_file_url() {
$file_url = get_field('file_url');
if (!empty($file_url)) {
wp_redirect($file_url, 301);
exit;
} }
do_action ('redirect_to_acf_file_url'); ?>
This would enable me to share the URL of the post and when users accesses the post URL, they'll go straight to the File url.
Right now, the above PHP just displays a blank page.
Would appreciate any help.
Thanks!
Share Improve this question asked Apr 26, 2024 at 1:23 a_ya_y 11 Answer
Reset to default 1The action redirect_to_acf_file_url
doesn't exist, at least not in stock WordPress. You'll need to supply a proper action hook, and it'll need to be one that happens before any output is sent to the browser. I'd recommend something like init
or wp
.
add_action()
takes a hook name and a function name.
<?php
function redirect_to_acf_file_url() {
$file_url = get_field('file_url');
if (!empty($file_url) && is_string( $file_url ) ) {
wp_redirect( esc_url( $file_url ), 301);
exit;
}
}
do_action ( 'wp', 'redirect_to_acf_file_url' );
?>
References
add_action()
wp
action hookesc_url()
本文标签: advanced custom fieldsRedirect to File attached to a Post using ACF
版权声明:本文标题:advanced custom fields - Redirect to File attached to a Post using ACF 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306068a1932819.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论