Automattic / Co-Authors-Plus

Multiple bylines and Guest Authors for WordPress
https://wordpress.org/plugins/co-authors-plus/
GNU General Public License v2.0
293 stars 205 forks source link

Feature request: add ability to add shortcode (with example) #1015

Closed bensontrent closed 1 year ago

bensontrent commented 1 year ago

I have to implement this plugin as a shortcode for my client's website that uses elementor. I created a plugin to accomplish this but it may be better to roll this into the base code. You can ignore my styling--it was a quick and dirty job to get something done quickly for the client but this shows how to implement Coauthors plus as a shortcode.

function coauthors_plus_shortcode() {

    // Check if Co-Authors Plus is active
    if ( function_exists( 'coauthors_posts_links' ) ) {

        // Get co-authors for the current post
        $coauthors = get_coauthors();

         // Prepare output
         $output = '';
         $output .= '<style> .coauthor img { border-radius:100%} .coauthor-heading {color: rgba(12,12,12,0.5); font-family: Rubik, Sans-serif;font-weight: 300;} </style>';
         $output .= count($coauthors) > 1 ? '<h3 class="coauthor-heading">About the authors</h3>' : '<h3 class="coauthor-heading">About the author</h3>';
         foreach ( $coauthors as $coauthor ) {
             $author_posts_url = get_author_posts_url( $coauthor->ID, $coauthor->user_nicename );

             $output .= '<div class="coauthor" style="display: flex; align-items: start; margin-bottom:30px; color: rgba(12,12,12,0.5); font-family: Rubik, Sans-serif;font-size: 15px;font-weight: 300;">';
             $output .= get_avatar( $coauthor->user_email, 96 );
             $output .= '<div style="margin-left: 30px;">';
             $output .= '<h3 style="color:black; margin-top:-7px; font-size: 22px;font-weight: 300; text-transform: capitalize;" ><a href="' . esc_url( $author_posts_url ) . '">' . $coauthor->display_name . '</a></h3>';
             $output .= '<div class="bio" >' . $coauthor->description . '</div>';

             $output .= '</div>';
             $output .= '</div>';
         }

         return $output;
        return $output;

    } else {
        return '<p>Co-Authors Plus plugin is not active.</p>';
    }

}

add_shortcode( 'coauthors_plus', 'coauthors_plus_shortcode' );
GaryJones commented 1 year ago

Thanks @bensontrent!

As we move towards having block support (#997) then it's highly unlikely that a shortcode would be added to the plugin, but I appreciate you sharing the code so that others may find and use it for inspiration should they need it!