Creating a Simple Lorem Ipsum Generator Plugin

You know already that WordPress is the most ever popular CMS (content Management system), It offers lots of plugins to extend it is functionality. In this article we are going to create our own plugin that might seem simple but it is very useful: a lorem Ipsum generator. There are lots plugins there but we have to create our own plugin. If you have found yourself repeatedly copying and pasting Lorem Ipsum text to WordPress posts and pages, You know, it takes lots of time. So lets create our own A Simple Lorem Ipsum Generator plugin. 

Introduction

Welcome back to web-spidy! In this article we will walk through the process of creating a custom and very useful WordPress plugin that generates Lorem Ipsum text for learning purpopse. If you have tired to copying and pasting Lorem Ipsum text from Lorem Ipsum generator then this article is very useful for you. 

Why Lorem Ipsum?

Before we get into the code and details, lets know first why Lorem Ipsum is so useful and important in web development world. Lorem Ipsum is a dummy text that used in the printing and typesetting industry. It appearance of real text but doesn’t convey any meaningful content. This texts make perfect for temporarily filling in content spaces while developing a website. You can use it without any copyright content. Let’s create a Lorem Ipsum text generator plugin for WordPress, You need to follow the following steps.

Step 1: Create a Plugin Directory

First step to create our plugin, You need to go your WordPress installation directory. And Find the Folder ‘wp-content’ and inside the ‘wp-content’ you will find ‘plugins’ folder. In the plugins folder You need to create a new folder and name it what you want. But I named it “LoremIpsumGenerator.

Step 2: Create the Main PHP File

Now create a main PHP file inside the “LoremIpsumGenerator” folder which is heart of Any WordPress plugin. We will name this file like: “lorem-ipsum-generator.php.” This file contains the code of the WordPress plugin. 

Step 3: Plugin Header

Add this part of code in the “lorem-ipsum-generator.php” file. This is our plugin header information. This includes name of plugins, Plugin Description, Version and Author’s name and Author URI: 

				
					<?php
/*
Plugin Name: Lorem Ipsum Generator
Description: A simple Lorem Ipsum text generator for WordPress.
Version: 1.0
Author: Your Name
Author URI: https://web-spidy.com/
*/

				
			

Step 4: Define the Lorem Ipsum Function

Now, let’s create the main function of our plugin, which generates Lorem Ipsum text. We will call this function “generate_lorem_ipsum”. It accepts optional parameters and you to specify the length of the generated text using a shortcode. 

				
					// Define a shortcode to generate Lorem Ipsum text.
function lorem_ipsum_shortcode($atts) {
    $atts = shortcode_atts(array(
        'length' => '100', // Default length of Lorem Ipsum text.
    ), $atts);

    // Generate Lorem Ipsum text.
    $lorem_ipsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent tincidunt nec elit a tempor. Nullam et rutrum leo, eu pellentesque leo. Mauris eget tristique justo. Mauris congue pulvinar diam. In sit amet ultricies velit. Nam posuere vitae sem ac imperdiet. In hac habitasse platea dictumst. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Aliquam erat volutpat. Nulla eget sapien sed ipsum suscipit tempus ut sed ligula. Quisque mollis tellus ut dui sollicitudin, id ornare velit ultrices. Etiam pretium hendrerit eros id bibendum. Phasellus sagittis quam quis augue rutrum, vel posuere neque commodo. Curabitur consectetur elit in dui vulputate, eget faucibus nisl lobortis. Phasellus pretium eleifend pretium. Suspendisse a convallis mi.

In hac habitasse platea dictumst. Nam bibendum orci nisi, fermentum imperdiet neque pellentesque non. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse fermentum pretium risus, ut consectetur quam sagittis vel. Etiam non ante a magna porta iaculis non nec ligula. Donec sit amet facilisis lacus. Duis congue auctor mi non aliquet. In volutpat ex vel egestas malesuada. Morbi auctor finibus massa, in faucibus sem rutrum feugiat. Curabitur convallis arcu eget dolor vulputate, sit amet facilisis augue tincidunt. Sed lobortis elit urna, et varius urna aliquet vitae. Integer porta rhoncus ante non consequat. Phasellus commodo dolor hendrerit odio scelerisque ullamcorper.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam blandit, nibh ac fermentum lacinia, ligula metus rhoncus neque, a pharetra eros velit eget ex. Donec ultricies lacinia odio vel sollicitudin. Maecenas lobortis posuere libero, et viverra lorem. Aenean lectus augue, maximus vitae dignissim ac, dignissim et leo. Sed non ipsum lorem. Curabitur ullamcorper, magna tempor interdum pharetra, felis enim porta libero, sit amet ultrices ex leo vitae libero. Vivamus eget efficitur ante. Vestibulum efficitur, quam id pellentesque pharetra, nisl tortor euismod massa, sed euismod lacus leo id felis. Duis maximus quis dolor ac vestibulum. Fusce id suscipit mi. Sed rutrum finibus mauris. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae;

Donec ut vulputate velit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Etiam vel est lacinia, egestas est sed, convallis dolor. Aenean luctus felis in lobortis venenatis. Donec vitae pellentesque nulla, sed tempor nisl. In ultricies risus nec faucibus scelerisque. Pellentesque suscipit in arcu vel viverra. Donec mattis blandit magna ac fringilla. Vestibulum suscipit, elit ut rutrum ornare, turpis ex feugiat ligula, et consectetur nibh orci id eros. Cras eleifend, ipsum ut consectetur blandit, risus augue faucibus est, eget malesuada dolor nibh vitae mauris. Curabitur mattis convallis malesuada. Etiam tincidunt lorem ut velit rhoncus gravida. Donec ac libero a sapien elementum vestibulum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.

Mauris posuere quam at purus scelerisque maximus. Maecenas ut tellus ultricies, accumsan justo eget, finibus sem. Donec pharetra imperdiet urna id fermentum. Suspendisse dictum massa leo, ullamcorper cursus mi feugiat a. Mauris malesuada ac nibh id dictum. Maecenas vulputate aliquam purus, vitae pellentesque risus lacinia at. Pellentesque vitae sapien maximus, ultricies ipsum a, fermentum ipsum. Quisque commodo mauris id ligula consectetur tristique. Phasellus aliquet feugiat malesuada. Morbi vel placerat elit.';

    // Trim the text to the specified length.
    $lorem_ipsum = substr($lorem_ipsum, 0, intval($atts['length']));

    // Return the Lorem Ipsum text as HTML.
    return '<p>' . esc_html($lorem_ipsum) . '</p>';
}
add_shortcode('lorem-ipsum', 'lorem_ipsum_shortcode');
?>

				
			

Step 5: Activate the Plugin

Now you need to copy the complete code with header and paste it in the “lorem-ipsum-generator.php” file. Now You need to activate the plugin. Goto your “Plugins” section, and activate the “Lorem Ipsum Generator” plugin. 

Step 6: Usage

Now you can see our plugin in action. You can use this short code [lorem_ipsum] in your posts, pages and widgets to generate the Lorem Ipsum text. and you can define your text length as well like this: 

[lorem_ipsum length="50"]

This will generate Lorem Ipsum text that containing 50 words.

Conclusion

And This is simple and incredibly practical Lorem Ipsum generator plugin for Your WordPress website. You can enhance this plugin by adding some customization options, styles etc. There are others plugins are available in WordPress plugin section which generate the Lorem Ipsum text. But We have develop our own plugin for learning purpose. 

If you found this article helpful please show your support with me by liking and sharing this article and feel free to leave any question or suggestions for more tutorials in the comments section below. 

Thank You for reading my article, and happy coding with your new Lorem Ipsum generator plugin!

Video tutorial here:



Leave a Comment