How to create a shortcode in WordPress

Facebook
Twitter
LinkedIn

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;
	}
});
Matt Pramschufer is a seasoned technology expert and co-founder of E-Moxie, where he specializes in transforming visionary ideas and business goals into thriving online ventures. With over two decades of experience in website design, development, and custom application creation, Matt understands that success requires more than just a visually appealing website or a well-built app. He combines proven methodologies with the latest internet technologies to deliver digital solutions that not only look great but also drive measurable growth.

Related Posts