Shortcode for ACF Fields on the Post Author
Adds an [author_field name="job_title"] shortcode that shows an ACF user field of the current post's author, for author boxes and bylines.
Opens WPIDE on
Code
// User fields that may be shown publicly.
$wpide_acf_public_user_fields = array( 'job_title', 'company', 'website', 'short_bio' );
add_shortcode( 'author_field', function ( $atts ) use ( $wpide_acf_public_user_fields ) {
$atts = shortcode_atts( array( 'name' => '' ), $atts, 'author_field' );
$name = sanitize_key( $atts['name'] );
$post = get_post();
if ( ! $post || ! in_array( $name, $wpide_acf_public_user_fields, true ) || ! function_exists( 'get_field' ) ) {
return '';
}
$value = get_field( $name, 'user_' . (int) $post->post_author );
return is_scalar( $value ) ? esc_html( (string) $value ) : '';
} );
Notes
Only list fields that are safe to make public.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.