Before you dive into coding with AI, take a moment to consider some valuable insights.
Our articles cover the pros and cons of using AI in development, the importance of having a development environment, and how AI empowers hobbyists and small businesses to create and maintain their own websites, without the need of hiring professional developers.
In this guide, we’ll walk through the process of creating a WordPress plugin that displays a simple author block. This block shows the author’s avatar, name, bio, and a link to view all posts by the author. We’ll also include an option to specify which author to display using a shortcode attribute.
At the end of this guide, you’ll have a fully functional WordPress plugin ready for installation.
Example Prompt:
Create a WordPress plugin that adds a shortcode to display an author’s block, including their name, bio, avatar, and a link to their posts. If no author is specified, it should default to the current post’s author. Include a CSS file for styling.
First, create a new folder for your plugin. You can name it easycoding-author-block (or whatever you prefer). Inside that folder, create a PHP file called easycoding-author-block.php. This file will contain all the necessary PHP code for the plugin.
The following PHP code registers a shortcode to display an author block. This code handles the logic for displaying the author’s information, including the option to specify a different author.
easycoding_author_block_styles()
enqueues the CSS file located in the assets/css/style.css
directory. This ensures the plugin’s styles are loaded on the front end of your WordPress site.easycoding_author_block_shortcode()
function handles the creation of the author block. It accepts an optional author
attribute (defaulting to the current post’s author). The shortcode displays the author’s avatar, name, bio, and a link to their posts.Now let’s create the style.css
file to style the author block.
Inside your plugin folder, create the following directory structure:
Here’s an example of how to style the author block:
.author-block
class styles the entire block, setting up a flex container to align the avatar and author information..author-avatar
class styles the avatar image, giving it rounded edges (via border-radius
) and a fixed size..author-info
class defines the styling for the author’s name, bio, and the link to view more posts.Now that you have created both the PHP and CSS files, it’s time to install the plugin.
wp-content/plugins/
folder.easycoding-author-block
(or use the folder name you’ve chosen).easycoding-author-block.php
and assets/css/style.css
files.After activating the plugin, you can use the shortcode in any post or page to display the author block.
By default, the shortcode will display the author of the current post:
To specify a different author, use the author attribute, as shown below:
In this guide, you’ve learned how to create a simple WordPress plugin that displays an author block with a shortcode. You also saw how to enqueue a custom CSS file to style the block. Following this tutorial, you can easily customize and expand the plugin to fit your needs.
Happy coding!
The coding tips and guides provided on this website are intended for informational and educational purposes only. While we strive to offer accurate and helpful content, these tips are meant as a starting point for your own coding projects and should not be considered professional advice.
We do not guarantee the effectiveness, security, or safety of any code or techniques discussed on this site. Implementing these tips is done at your own risk, and we encourage you to thoroughly test and evaluate any code before deploying it on your own website or application.
By using this site, you acknowledge that we are not responsible for any issues, damages, or losses that may arise from your use of the information provided herein.