• Resolved jamie.gordon.tt

    (@jamiegordontt)


    Hi,

    I am trying to add a custom meta-box for author name on my wordpress, however when I select an existing user, it outputs as the user ID, and not the author’s name.

    Is there an easy solution to my problem?

    My code is as follows:

    add_filter( 'the_author', 'replace_author_name' );
    add_filter( 'get_the_author_display_name', 'replace_author_name' );
    
    function replace_author_name( $name ) {
    global $post;
    
    $author = get_post_meta( $post->ID, 'replace-author', true );
    
    if ( $author )
    
    $name = $author;
    
    return $name;
    
    }
    add_filter( 'rwmb_meta_boxes', 'your_prefix_meta_boxes' );
    function your_prefix_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array(
            'title'      => __( 'Select an author', 'textdomain' ),
            'post_types' => 'post',
            'fields'     => array(
               array(
                    'id'   => 'replace-author',
                    'name' => __( 'Select an existing author (if not yourself)'),
    				'desc' => __( 'Use this if you are uploading the news story on behalf of another author', 'textdomain' ),
                    'type' => 'user',
                    'field_type' => 'select_advanced',
    				'placeholder' => 'click here to change the author;',
                ),
    			array(
                    'id'   => 'guest-author',
                    'name' => __( 'Or type a guest Author', 'textdomain' ),
                    'type' => 'text',
                ),
    			array(
                    'id'   => 'copyright',
                    'name' => __( 'Who owns the copyright?', 'textdomain' ),
    				'desc' => __( 'No copyright will display if this box is left blank', 'textdomain' ),
                    'type' => 'text',
                ),
    
            ),
        );
        return $meta_boxes;
    }

    https://wordpress.org/plugins/meta-box/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Anh Tran

    (@rilwis)

    Hi Jamie,

    The “user” field stores user’s ID in the meta value, so when you get the meta value, you’ll receive user ID. That’s correct.

    In order to get user display name, please use get_userdata() function.

    Best regards

    Plugin Contributor Manny Fleurmond

    (@funkatronic)

    To get the user’s name:

    add_filter( ‘the_author’, ‘replace_author_name’ );
    add_filter( ‘get_the_author_display_name’, ‘replace_author_name’ );

    function replace_author_name( $name ) {
        global $post;
    
        $author = get_post_meta( $post->ID, 'replace-author', true );
    
        if ( $author ){
            $author_data = get_userdata($author);
            $name = $author_data['display_name'];
        }
    
        return $name;
    }
    Thread Starter jamie.gordon.tt

    (@jamiegordontt)

    Hi Manny,

    The above code doesn’t seem to work. It prevents anything on the page loading after the_author()

    I also forgot to mention that I have a guest-author function as follows:

    add_filter( ‘the_author’, ‘guest_author_name’ );
    add_filter( ‘get_the_author_display_name’, ‘guest_author_name’ );

    function guest_author_name( $name ) {
    global $post;
    $author = get_post_meta( $post->ID, 'guest-author', true );
    if ( $author )
    $name = $author;
    return $name;
    }

    Do you know where I am going wrong?

    Plugin Contributor Manny Fleurmond

    (@funkatronic)

    The code I posted was meant to replace your code. Basically, you use get_userdata to get the user name

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom metabox for author not author ID’ is closed to new replies.