ACF Field value in wordpress login message filter

I am trying to add a custom message after the WordPress logo on the WordPress login form, I am using the ACF field on the home page to echo the value on the WordPress login page, I have mentioned the code, any help?

function alltomhobby_login_message( $message ) {
    if ( empty($message) ){
        $logintext = the_field('login_text');
        return $logintext;
    } else {
        return $message;
    }
}

add_filter( 'login_message', 'alltomhobby_login_message' );

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

the_field() echoes out the result. You want get_field() instead.

However these won’t automatically default to reading fields from the home page. You’re going to have to give get_field the post ID of the home page to look up so it knows where to get the field from. Something like:

function alltomhobby_login_message( $message ) {
    if ( empty( $message ) ){
        $show_on_front = get_option( 'show_on_front' );
        $home_page_id = ( 'page' === $show_on_front )
                            ? (int) get_option( 'page_on_front' )
                            : ( ( 'post' === $show_on_front )
                                     ? (int) get_option( 'page_for_posts' ) : NULL );

        if ( isset( $home_page_id ) ) {
            $logintext = get_field( 'login_text', $home_page_id );
            return $logintext;
        }
    }

    return $message;
}

add_filter( 'login_message', 'alltomhobby_login_message' );

Or you could instead hard-code the page ID if you know it and it’s fixed.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x