Shortcodes are incredibly versatile tools that make it easy to display various types of content anywhere on your website. They offer a simple way to inject dynamic elements into your pages and posts without needing to dive deep into code every time. In this example, I’ll walk you through the process of creating a custom shortcode that retrieves and displays an author’s email and website address.
This shortcode can be particularly useful on an Author archive page or within a single post template, allowing you to automatically showcase the author’s contact information without manually adding it to each page. With just a few lines of code, you can enhance the functionality of your site and improve the user experience by providing relevant author details wherever needed.
<?php
add_shortcode('author-info', function($atts){
switch($atts['type']){
case "email":
return get_the_author_meta('user_email');
break;
case "website":
return get_the_author_meta('url');
break;
}
});