Learn How to Create a Plugin in WordPress.

0
10975
wordpressplugins(1)-740X296

wordpressplugins(1)-740X296

Hi y’all, today we are going to learn how to create a plugin and will use the Eduonix YouTube video as an example. In this example, we are fetching a course video from YouTube in your WordPress website. Three things you will learn from this blog is: How to create a video post type, how to create dynamic embed code for videos, and how to use the Settings API.

Basically we are going to create the following two things:

1) We are going to create a Disable plugin for full screen.1

2) We will build a custom post type plugin for YouTube videos.2

So let’s start with our installation!

First, we need XAMPP (if you already have WAMPP, then you don’t need XAMPP) and second, we need to set up WordPress. Follow the steps below for installation:

FOR XAMPP:

1) Download XAMPP from the link “https://www.apachefriends.org/index.htmlfor your operating system. Once you complete downloading, open it you will get the “XAMPP Control Panel”. Click on the start button for Apache and MySQL, it should be like this.3

2) Once you are done with this, go to your browser and type, “localhost/phpmyadmin”. You will get the following screen.
4

3) Click on “Database”. After clicking on it, you will get the database window. Create your database with name “mywordpress”.
5

FOR WORDPRESS:

1) Go to your XAMPP folder and you will see the “htdocs” folder in it. Open that folder and in that, folder create a new folder with the name “mywordpress”.

2) Visit “wordpress.org” website and download the latest version. After downloading open that folder and copy all the content and paste it in your “mywordpress” folder.

3) Open the “mywordpress” folder using one of the editors. Rename the “wp-config-sample.php” file with “wp-config”. After that set your database name, username,password.

By default, username will be “root”, if you don’t set the password and then make it blank.
6

4) After that, you have to generate a key for it, so go to your browser and search for the WordPress key generator. Click on the secret key generator, you will get the key format like this:
7

Copy this code and paste it here:
8

5) Once you are done with this, save it and go to the “localhost/mywordpress”. You will get the following window:
9

Select English and click on continue, you will get the following screen. Fill the information and click on “Install WordPress” button.
10

6) Once you are done with this, you will get the success page and it will ask for you to login. Put the username and password and click on login, you will get the following screen:
11

7) You will be able to see the “mywordpress”. Once you click on it, it shows the option of “Visit Site”. Click on the “visit site” button, you will get the ‘your wordpress’ website like this:
12

So, let’s start with actual coding:

1) Click on your “myswordpress” folder you will be able to see the wp-content In that, click on plugins folder. Create a new folder with name youtube-vid-gallery. In this folder, create the following structure of the folders as follows:
13

The “css” folder, create the “style.css” and “style-admin.css” files.
The “js” folder create the “main.js” file.
The “includes” folder will contain the following files:
14

2) This is the main plugin code. Add it in your “youtube-vid-gallery.php” In this code, we have included all the files that we have created in the includes folder.

3) Now, the next step is to add the code in “youtube-vid-gallery-scripts.php”. In this code, we use the “wp_enqueue_style”. It enqueue a CSS stylesheet and Registers the style, if source provided (does NOT overwrite it) and enqueues.

Syntax:

Description:

$handle
(string) (Required) Name of the stylesheet.
$src
(string|bool) (Optional) Path to the stylesheet from the root directory of WordPress. Example: ‘/css/mystyle.css’.
Default value: false
$deps
(array) (Optional) An array of registered style handles thi stylesheet depends on.

$ver
(string|bool) (Optional) String specifying the stylesheet version number, if it has one. This parameter is used to ensure that the correct version is sent to the client regardless of caching, and so should be included if a version number is available and makes sense for the stylesheet.

The “wp_enqueue_script” is provided to Enqueue a script and Register the script if $src provided (does NOT overwrite), and enqueues it.

Syntax:

Description :

$handle
(string) (Required) Name of the script.
$src
(string|bool) (Optional) Path to the script from the root directory of WordPress. Example: ‘/js/myscript.js’.

$deps
(array) (Optional) An array of registered handles this script depends on.

$ver
(string|bool) (Optional) String specifying the script version number, if it has one. This parameter is used to ensure that the correct version is sent to the client regardless of caching, and so should be included if a version number is available and makes sense for the script.

$in_footer
(bool) (Optional) Whether to enqueue the script before </head> or before </body>. Default ‘false’. Accepts ‘false’ or ‘true’.

Add your code in “youtube-vid-gallery-scripts.php”

WordPress Training for Beginners From Scratch

4) After that we will create a custom post type in the “youtube-vid-gallery-ctp.php” We are using two types of filters: singular and plural. Basically, apply_filters() interacts with the global $wp_filters array. It just checks the array if the current filter (or hook) has an action(/callback function) attached and then calls it.

For example:
A string that changes when the number of items changes. In English, you have “One comment” and “Two comments”. In other languages, you can have multiple plural forms. To handle this in WordPress you can use the _n() function.

singular – the singular form of the string (note that it can be used for numbers other than one in some languages, so ‘%s item’ should be used instead of ‘One item’)

plural – the plural form of the string

count – the number of objects, which will determine whether the singular or the plural form should be returned (there are languages, which have far more than 2 forms)

text domain – the plugins text domain

Add this code:

5) After adding this code, run your website. Click on Plugins and you will get the following window. Just click on activate.
16

6) Next step, we will be creating a form for adding video information and saving it. Add the following code in your “youtube-vid-gallery-fields”:

Once you are done with this, you will get the following screen:
17

7) Go to “Youtube” and search for “Eduonix”. After that open one video and copy the video id and add it in your video form.
18

After that, add the description and you will get a final window like this
19

Click on Publish button.

8) Add this css code in your “Style-admin.css” file.

9) Add this code in your “youtube-vid-gallery-shortcodes.php” to list out the videos.

Following this, click on “Pages” and create a page called “Videos”.
20

Once you are done with this, click on “Appearance” and in that select “Menus”. You will get the following screen:
21

After that, check the “Videos” option and click on the “Add to Menu” button. Once the video gets added, you will get the following screen. It shows that the videos have been added in a menu structure. Click on the Save menu button to save the changes.
22

After that, click on “Manage Locations” tab. In that, select primary menu as “MENU” and save it .

Add the following code in your “style.css” file. In that we are giving a bottom to video.

Once you have done this, you will get following output:
23

10) The last part is the coding for “Disable screen”. Go to your “youtube-vid-gallery-settings.php” and add this code there:

Once you have finished adding this code, you will get the “disable button” functionality in “Settings”-> “Reading” submenu. You will be able to see the following button:
24

If you check the disable button, you will get the error i.e “Fullscreen is unavaibale”, and if it is unchecked, you will be able to view the video in Fullscreen.
25

“In this section we learn a few things including how to create a custom post type for video, how to create video posts using the Youtube video id, as well as the video details field. We then learnt how to create the settings API, and how to set the video custom size. Finally, we also covered how to create a Fullscreen disable option.

I hope this has been helpful for you guys in understanding how to create a plugin in WordPress.

LEAVE A REPLY

Please enter your comment!
Please enter your name here