How it works...

Many WordPress themes only show the name of the author under the title. with a link to the author archive page. Some of the modern themes provide an in-built profile to display custom-designed profiles with additional details. In this scenario, we wanted to display the author's profile using a plugin. Let's identify how the code works using the following steps:

  • First, we used a the_content filter with a callback function to add additional content, as we did several times in previous chapters. Inside the callback function, we checked if the post author is set before adding the details.
  • In step 3, we retrieved the author ID by calling $post->post_author on the global $post object. This object is available in post screens, with details about the post.
  • Then, we used the get_userdata function to get the data of the author from the wp_users table.
  • In the next few lines, we retrieved the author's details from the database. The first name, last name, and description were stored as meta values in the wp_usermeta table. The website was stored in the wp_users table as a built-in column.
  • Then, we used the get_avatar function to grab the author's image from gravatar.com.
  • In step 4, we created HTML containers for the profile and assigned the data using the variables from the previous section. The author's profile is now added at the end of the post content.
  • Then, we created a new folder for CSS files and created a style.css file. In step 6, we added the necessary styles to highlight the profile using background color and border. We also added some paddings and margins to make the profile look better.
  • In step 8, we used the wp_enqueue_scripts action to call the wpccp_chapter6_register_plugin_styles function for adding CSS files. 
This action is used by WordPress to include CSS and Script files for the frontend of the site. Directly adding files using <link> or <script> tags is not recommended in WordPress. 
  • Inside the callback function, we called the wp_register_style function to register a stylesheet file for the site. We can't use stylesheets without registering them first. In this function, we have passed a unique slug and path to the CSS file. The path was retrieved by using the plugin_dir_url function and providing a relative path. At this stage, the stylesheet will be only registered and not included in the frontend of the site.
  • Then, we used a wp_enqueue_style function to include the stylesheet on the frontend. We have to use the slug we used to register the stylesheet as the parameter to this function. 

At this stage, the code is ready for an author profile. Before we started checking the profile, we went to the backend profile and added the first name, last name, website, and description for the admin profile. Now, we can view the post from the frontend with the profile data retrieved from the custom code.

Before moving into the next recipe, remove the code added for this recipe inside the WPCookbook Chapter 6 plugin.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset