Articles

WordPress – Making a Basic Plugin


Hey guys what’s up? In this video I’m
going to be teaching you guys how to make your very first WordPress plugin.
Now this plugin is going to be very simple and it’s just going to be a page
on the WordPress admin dashboard and it’s really easy to do and you can do a
bunch of things, so let’s say you’re building a website for a client, but you
want to have a custom plugin for them to change options on their website, contact
you, white label your own WordPress installation, – that kind of thing. Well
that’s what these videos are going to be for. Now in order to actually make this
plug-in you are actually going to need to know how to code in
HTML and PHP and it’s good to know some JavaScript and CSS but it’s not
necessarily required. I already have a series on HTM – i’ll have a link for that in
the description below, and if you want to learn PHP I’ll have a link to a great
series on PHP. I decided that there’s already so many series on that already
online that I figured why bother doing one but
if you want to see me make it PHP series just let me know in the comments section
below. Alright so let’s go ahead and get
started. So the first thing you need to do is have an installation of WordPress –
which we do and since we have that all, done I’m going to go ahead and make a
new plugin. So how do you do that? Well the first thing you need to do is
to organize your file structure. So go into your WP content folder on your
server, go into your plugins folder, and then make a new folder and call it
whatever you want. It honestly doesn’t matter, just make sure the folder doesn’t
exist. So I’m just going to call this “example-plugin”. Alright now in that
folder, I’m going to make a new file and I’m just going to change the syntax real
quick to PHP, and now I’m going to save this in my “wp-content” folder, followed by by “plugins” and example plugin and I’m going to call this “index.php”. Now you
may be saying “okay now it’s going to show up here right? So just refresh?”
No – that’s because you need to fill in some basic information and I’ll have an
example template in the description for you guys so you don’t have to worry
about that and all you have to do is use this template and all we have to do is
fill this out. So for plugin name I’m just going to type in “Example Plugin” For plugin URL, i’m not going to worry about that – that’s like if you want
to publish it somewhere on a website, either there or if you actually want to
submit this plug-in to the WordPress gallery. Which I’m not going to do.
“Description:” “example plug-in for YouTube” author – MTT, author URL – (whatever you can
just put your youtube link I don’t know) your business brand. etc. So there we go.
Version – I’m just going to put 0.1 for now because this is just a beta. Alright
so now that you have that if I go ahead and hit save, now it should show up. If I
go over here and I refresh, you see it says “example plugin for YouTube by MTT
version 1.0” and I can go ahead and activate it. Alright cool so your plug-in
is active but let’s actually go ahead and create our first thing. So this
plug-in is just going to be like a hello world style drop down menu item. So
before we get into that so I’m just going to show you –
so all these menus – essentially we’re going to add another tab, and you can add
pages to it. So let’s get started alright? So the first thing you want to do is
tell WordPress that you want to add an action and that action is going to
add a menu to the admin dashboard. So you want to type “add_action(“admin_menu”, “addMenu”);” and if you want I will have the link to this project in the video
description if you’re watching this on YouTube. I”m just going to call this
function “addMenu” because that’s actually what I called it up here so
you’re saying add_action, admin menu and it’s the function addMenu() and in this
addMenu function, we’re just going to do the first one which is – I want to add a
menu page and it’s actually group of pages so and then the first thing you
will put in is the page title – so “example options” followed by the menu title, so the main menu title so I’m just going to call this the same thing
“example options” and then followed by the capability – so these are the people that
can actually access your website. So I will have a link to a bunch of different
roles but I want only editors and administrators to have access to it, so I am going
to put 4 or 5, it doesn’t matter for editors and then I’m going to put
the menu slug. So that’s actually in the URL so I’m just going to put “example-options” for now and then so we have all that. Now for the
function that contains all the page. So I’m just going to call this “exampleMenu”
and then followed by an icon URL. If you want to set one – I’m not going to set one.
Then the position but by default it’ll just be at the bottom which is
fine by me. All right so we have that so let’s just save that. Come back, refresh.
So here you can see we now have an option and it says – well it’s giving you
an error because you actually have not made that function yet – BUT the menu
appears. So let’s go ahead and make that function. So as you can see we have
“exampleMenu” as the name of the function So let’s go ahead and make that function.
So I’m going to just go down a few lines and call this function “exampleMenu” and
like I said this is for someone that obviously knows some PHP already. If you
want to just go ahead and mess with the code already whatever you can, but I
recommend finishing this video – and then followed by whatever you want to appear
on the page. So I can just put “echo ‘Hello World’ world; ” and then boom. If i go back and I
refresh the page, you can see it says “hello world” Now that’s cool and all but
let’s say you want to add another page. So I’m going to go back into my addMenu function and then I’m going to do add – this time submenu page (add_submenu) and now you need to say what you want it to be a submenu of. I called it example-options. and if you actually look over
here, you can actually make it one of the submenu under here so if you want
them all under settings you can do that. I’m not going to do that, but like I said
I’ll have more information for everything you need to know in the
description. All right now I just want to give it a page title, “Option 1”, and then again just repeat it I don’t know why they do that but whatever.
You have your page title your menu title capability of four, four is fine menu
slug, “example-option-1” and it’s just going to be my slug, and then menu..
– actually no I’m just going to call this “option1” as my function. So now, once
again I can just go down here and call option1, and then put all my code I
want in here.. and I’m just going to put out walrus. Then if I go
back here… and i refresh, error on line 18 that’s why it’s important that you make
sure you watch out for semicolons people. You need to do that otherwise you
might crash your WordPress plugin. Alright so now we have an option one
menu that says walrus and an example options one that says whatever. But let’s
say you just have a bunch of HTML and you don’t want to learn how to code
that’s fine I mean I use this a lot. You can use the
heredoc syntax just fine. I’m just going to copy and paste this and
boom. So I copied and pasted that it just is coming soon and you can put
whatever HTML you want within there and don’t have to worry about PHP at all. So
thank you guys so much for watching this video, if you’ve enjoyed it make sure to
give it a thumbs up on YouTube. Hit the subscribe button, and make sure to circle
me on Google+ the link for that will be in the description, as well as on
Facebook and Twitter I’m going to be using that a lot now. So it’s the best
way to find out when more tutorials are coming out and if you want to
communicate with me you can do so that way as well
and don’t feel afraid to shoot me an email at [email protected]
and I will get back to you shortly. Thank you guys so much for watching and I will
catch you guys in a future MicroTechTutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *