Create A Wordpress Widget - Part 1

Traversy Media · Intermediate ·🌐 Frontend Engineering ·11y ago
In this series we will build a Facebook likebox widget with multiple parameters -- Exercise Files - https://github.com/codeskillet/likebox -- For more web development videos, visit http://codeskillet.com

What You'll Learn

This video demonstrates how to create a WordPress widget, specifically a Facebook likebox widget, using PHP and JavaScript. It covers the basics of WordPress plugin development, including creating a plugin folder, adding a comment block, and registering the widget.

Full Transcript

hey guys in this minseries we're going to be building a WordPress widget all right and a WordPress widget is basically a plug-in uh it's just a certain kind of plugin you can log in into your WordPress admin area and go to widgets and then you can pretty much drag them to uh whatever position your template allows all right and what we're going to be building is basically a a Facebook likebox widget um so you can get a like box if you go to this URL all right so normally the user would need to uh enter their page URL uh set up some parameters like the width the height the color what they want to display and then they would have to get the code and they would have to get this JavaScript SDK and include that in their site and then they would have to find somewhere to to embed this HTML um and for someone that's not a developer and doesn't know much about um HTML or JavaScript uh can be kind of a pain so uh this widget will just will be able to install it in a WordPress site very easily um basically just upload it and then the user can go to the back end and go to the widget parameters and they'll be able to edit this stuff there okay instead of having to come here and then generate the code and all that all right so uh that's what we'll be building and if you're serious about learning how to build WordPress plugins and widgets um I suggest going to this URL okay this is the widgets API and this actually has a lot of the code that we're going to use um and there's a bunch of other information as well um I'd also suggest going to the plug-in API page as well all right so this is our site um actually let me reload that's not supposed to be there so it's just a basic Al a default WordPress installation uh this is version 3.91 but I do have the I think it's the 2012 template enabled um just because it's easier to see the sidebar and all that all right so with that said what we're going to do is open up I'm using notepad++ all right and I'm in my let me go to the root directory okay so this is the root directory of my WordPress site okay so what we're going to do is we need to go to the wp content folder and then the plugins folder and then we want to create a new folder and I'm going to call this likebox okay and then in that folder we're going to create a couple files all right so the first one is going to be just called likebox PHP and then we're going to create another file I'm going to call this one class. likebox PHP okay so this will be our main file um and then we'll include this which will be the like bux it'll be the widget class um and that's where most of our coding will be okay uh and I'm also going to create a new folder called JS because um we need to include our JavaScript SDK so inside there I will create another file and I'm just going to call this actually I'll call it um like box. JS okay so these are our three main files all right and we'll start with the the main file which is basically the entry point for the widget so the first thing you need to do when building a plugin is you need to include a comment block which basically um shows your WordPress that you have a plugin and it displays the name and also um you need to have it so that it'll pop up in the back end all right so we want it to show if we go to plugins all right we want it to show here so we can actually activate it okay and let me just get that code for you okay plug-in API if we scroll down where is it maybe this isn't the right okay writing a plugin this link here here we go all right so here we have the I guess called file headers okay so it's just this comment Block in PHP so I'm going to copy that and paste it and we're just going to change some some things here the plug-in name is going to be likebox plug-in URI um I'm just going to say Cod skillet.com description we'll just say uh simple plugin to show Facebook like box I believe the only field that's required here is the plug-in name I don't think you need any all this uh it's just good to have though um just for metadata version will be 1.0 author feel free to put your own name okay URI we'll say Cod skillet.com and then license we'll say gpl2 that's fine all right so this is actually all we need for it to show up in WordPress so if you save that and then go ahead to the back end and and reload this plug-in area now you can see we have our likebox plugin okay it has the description the version the author it goes to the to the author's site so very easy to actually get it to show up here all right so um let's see I'm not going to activate it yet what I want to do now is I want to get it so we can include our Javascript file so we're going to create a function okay and this is going to be called addore scripts all right and then in this function we're going to call a JavaScript function called WQ uh I'm sorry WP um what is it n qu e u e scripts all right and we can actually look this up all right so basically um it has a few parameters okay uh that actually this one here is for CSS same thing but except style all right and here's ours wpnq script all right and the parameters uh let's see the parameters the first one here my J this can basically be anything it just uh basically is a a context reference um the next one is going to be the actual URL to the Javascript file and the false I can't remember exactly what that is um doesn't look like it gives us a description of the parameters example all right well basically we're just going to use false for that third parameter uh so the first parameter I'm just going to put you want to make sure you have quotes and we'll say likebox scripts okay the next one is going to be a URL or uh a link to our Javascript file and in WordPress we can actually use this plugins uncore URL function and that will get us to our actual plugins folder all right so from the plugins folder we want to concatenate on slash likebox and then sljs slash likebox DJs and that'll bring us to our JavaScript file and um let's just put in false for the third parameter all right so that is our ad scripts function right now it's not going to do anything it's just a declaration so we have to tell WordPress when to run it so we're going to use this add action hook all right so what we're going to say is the first parameter is going to be this WP NQ script I'm sorry this first one should be script okay singular and then this here is scripts all right this is the actual hook so um oops the next parameter we just want to put in the name of the function that we created which is ADD scripts okay so that should add that now what I want to do is activate the plugin so activate and then in the front end uh let's go to the Javascript file and just give an alert we'll say alert one okay so now you can see that that JavaScript is now included okay we'll delete that so that's all set the next thing we want to do is include our class file okay so we'll say include class you should you should have your code um well commented so this will add add our JavaScript okay include class we're going to use include and class do it's in the same directory so just do class. likebox do PHP okay that'll include our class and then the last thing we want to do in this file is uh register our widget and then all the rest of the code will be pretty much in the class all right so to do this we want to say function register like box okay and then we're going to call register uncore widget and let's see we're going to pass in likebox underscore widget and then we need to use the add action hook again so add action okay we're going to use uh widgets underscore and nit and then the name of our function which is register like box all right so that's basically all the code we need for this main file all right the rest of the stuff is going to go into our class and we will get into that in the next part
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Traversy Media · Traversy Media · 41 of 60

1 Changing Your DNS/Nameservers
Changing Your DNS/Nameservers
Traversy Media
2 Create a MySQL database in cPanel
Create a MySQL database in cPanel
Traversy Media
3 Install & Uninstall Joomla Extensions
Install & Uninstall Joomla Extensions
Traversy Media
4 Adding and linking an article in Joomla
Adding and linking an article in Joomla
Traversy Media
5 Create a Joomla Blog
Create a Joomla Blog
Traversy Media
6 Import & Export A MySQL Database
Import & Export A MySQL Database
Traversy Media
7 Use A Custom Font On Your Website Using CSS
Use A Custom Font On Your Website Using CSS
Traversy Media
8 Connect Joomla Site With Dreamweaver
Connect Joomla Site With Dreamweaver
Traversy Media
9 Remove Phoca Gallery 3.2.3 Footer Text
Remove Phoca Gallery 3.2.3 Footer Text
Traversy Media
10 Drupal 7 Security Update 7.19 to 7.20
Drupal 7 Security Update 7.19 to 7.20
Traversy Media
11 Add An Addon Domain In Cpanel
Add An Addon Domain In Cpanel
Traversy Media
12 Pull A Heroku Rails App and Database
Pull A Heroku Rails App and Database
Traversy Media
13 Create a Custom Joomla 2.5 Module - Part 1
Create a Custom Joomla 2.5 Module - Part 1
Traversy Media
14 Create a Custom Joomla 2.5 Module - Part 2
Create a Custom Joomla 2.5 Module - Part 2
Traversy Media
15 Create a Custom Joomla 2.5 Module - Part 3
Create a Custom Joomla 2.5 Module - Part 3
Traversy Media
16 Joomla SEO Tutorial - sh404sef Configuration
Joomla SEO Tutorial - sh404sef Configuration
Traversy Media
17 Font Dragr
Font Dragr
Traversy Media
18 Convert an HTML Template to Joomla 2.5/3.0 - Part One
Convert an HTML Template to Joomla 2.5/3.0 - Part One
Traversy Media
19 Convert an HTML Template to Joomla 2.5/3.0 - Part Two
Convert an HTML Template to Joomla 2.5/3.0 - Part Two
Traversy Media
20 Rockettheme Rocketlauncher   Joomla Site in Under 10 Minutes
Rockettheme Rocketlauncher Joomla Site in Under 10 Minutes
Traversy Media
21 JQuery FAQ Slider Tutorial
JQuery FAQ Slider Tutorial
Traversy Media
22 301 Redirect With htaccess File
301 Redirect With htaccess File
Traversy Media
23 Convert HTML to Wordpress Theme - Part 1
Convert HTML to Wordpress Theme - Part 1
Traversy Media
24 Convert HTML to Wordpress Theme - Part 2
Convert HTML to Wordpress Theme - Part 2
Traversy Media
25 Easy JQuery Widgets
Easy JQuery Widgets
Traversy Media
26 Codeigniter App Part 1 - Creating the Database
Codeigniter App Part 1 - Creating the Database
Traversy Media
27 Codeigniter App Part 2 - Installation and Configuration
Codeigniter App Part 2 - Installation and Configuration
Traversy Media
28 Codeigniter App Part 6 - Login/Register System
Codeigniter App Part 6 - Login/Register System
Traversy Media
29 Codeigniter App Part 7 - Models List CRUD
Codeigniter App Part 7 - Models List CRUD
Traversy Media
30 Codeigniter App Part 8 - Models Task CRUD
Codeigniter App Part 8 - Models Task CRUD
Traversy Media
31 Node.js Part 1 - Install NodeJS on Windows
Node.js Part 1 - Install NodeJS on Windows
Traversy Media
32 Node.js Part 3 - Building a Static Page Server
Node.js Part 3 - Building a Static Page Server
Traversy Media
33 Node.js Part 4 - NPM
Node.js Part 4 - NPM
Traversy Media
34 Node.js Part 2 - Install MongoDB in Windows
Node.js Part 2 - Install MongoDB in Windows
Traversy Media
35 Create a Joomla Quickstart with Custom Sample Data
Create a Joomla Quickstart with Custom Sample Data
Traversy Media
36 Install MongoDB in Ubuntu
Install MongoDB in Ubuntu
Traversy Media
37 HTML5 Web Storage
HTML5 Web Storage
Traversy Media
38 Create a Joomla Bootstrap Template From Scratch
Create a Joomla Bootstrap Template From Scratch
Traversy Media
39 Ubuntu Server 14.04 Setup Part 1 - Installation
Ubuntu Server 14.04 Setup Part 1 - Installation
Traversy Media
40 Ubuntu Server 14.04 Setup Part 3 - Set Static IP
Ubuntu Server 14.04 Setup Part 3 - Set Static IP
Traversy Media
Create A Wordpress Widget - Part 1
Create A Wordpress Widget - Part 1
Traversy Media
42 Create A Wordpress Widget - Part 2
Create A Wordpress Widget - Part 2
Traversy Media
43 Create A Wordpress Widget - Part 3
Create A Wordpress Widget - Part 3
Traversy Media
44 Create A Wordpress Widget - Part 4
Create A Wordpress Widget - Part 4
Traversy Media
45 Get Started With Sass on Windows
Get Started With Sass on Windows
Traversy Media
46 Build An HTML5 Template With Bootstrap and SASS - Part 1
Build An HTML5 Template With Bootstrap and SASS - Part 1
Traversy Media
47 Build An HTML5 Template With Bootstrap and SASS - Part 6
Build An HTML5 Template With Bootstrap and SASS - Part 6
Traversy Media
48 Build An HTML5 Template With Bootstrap and SASS - Part 4
Build An HTML5 Template With Bootstrap and SASS - Part 4
Traversy Media
49 Build An HTML5 Template With Bootstrap and SASS - Part 5
Build An HTML5 Template With Bootstrap and SASS - Part 5
Traversy Media
50 Build An HTML5 Template With Bootstrap and SASS - Part 3
Build An HTML5 Template With Bootstrap and SASS - Part 3
Traversy Media
51 Build An HTML5 Template With Bootstrap and SASS - Part 2
Build An HTML5 Template With Bootstrap and SASS - Part 2
Traversy Media
52 Build An HTML5 Template With Bootstrap and SASS - Part 7
Build An HTML5 Template With Bootstrap and SASS - Part 7
Traversy Media
53 Build An HTML5 Template With Bootstrap and SASS - Part 10
Build An HTML5 Template With Bootstrap and SASS - Part 10
Traversy Media
54 Build An HTML5 Template With Bootstrap and SASS - Part 8
Build An HTML5 Template With Bootstrap and SASS - Part 8
Traversy Media
55 Build An HTML5 Template With Bootstrap and SASS - Part 11
Build An HTML5 Template With Bootstrap and SASS - Part 11
Traversy Media
56 Build An HTML5 Template With Bootstrap and SASS - Part 9
Build An HTML5 Template With Bootstrap and SASS - Part 9
Traversy Media
57 Build An Audio Player Using HTML5 & jQuery - Part 1
Build An Audio Player Using HTML5 & jQuery - Part 1
Traversy Media
58 Build An Audio Player Using HTML5 & jQuery - Part 2
Build An Audio Player Using HTML5 & jQuery - Part 2
Traversy Media
59 Youtube Data API v3 & jQuery To List Channel Videos
Youtube Data API v3 & jQuery To List Channel Videos
Traversy Media
60 Using Bootstrap With Ruby on Rails
Using Bootstrap With Ruby on Rails
Traversy Media

This video teaches how to create a WordPress widget, including setting up the plugin folder, adding a comment block, and registering the widget. It also covers including JavaScript files and using the WordPress API.

Key Takeaways
  1. Create a plugin folder
  2. Add a comment block
  3. Register the widget
  4. Include a JavaScript file
  5. Use the WordPress API
💡 To create a WordPress widget, you need to create a plugin folder, add a comment block, and register the widget using the WordPress API.

Related AI Lessons

Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →