How to create a WordPress Widget

I think it’s time to learn how to create a WordPress Widget. WordPress (WP from now on) is the most used blogging platform, and I want to help everybody to code their favorite widgets.

From the official site (where you can find a lot of widgets): WordPress Widgets (WPW) is like a plugin, but designed to provide a simple way to arrange the various elements of your sidebar content (known as “widgets”) without having to change any code.

From a geeky point of view, a widget is a php file. Fullstop. Being a php file, you can make whatever you want. You “just” need to master php and follow some guidelines given by WP guys.

To make the simplest widget ever, you have to know only two WP functions:

The first is register_sidebar_widget($name, $function): adds your widget in the widget admin panel. You simply have to change $name with the name of your widget and $function with the function that your widget will execute.

The second one is add_action($event, $function): $event tells what event your $function should be associated with.

Ok, now let’s write some php

Triqui widgetI love Emanuele Feronato";
}

function init_triqui(){
	register_sidebar_widget("Triqui", "triqui_widget");     
}

add_action("plugins_loaded", "init_triqui");

?>

Lines 2-9: This commented code is needed to show WP users who made the plugin, what does the plugin do, where to download updates, and so on. Look at this picture:

WordPress widget

It represents your plugin management page, and you can see how your information is displayed according to what you write in lines 2-9

Line 11: Beginning of the widget itself

Line 12: The widget… just writing the title (between h2 tags) and a link to my blog

Line 15: Function to initialize the widget

Line 16: As explained before, I am registering the sidebar widget calling it Triqui, and want it to execute the triqui_widget function, the one at lines 11-13

Line 19: When the plugins are loaded, I want init_triqui to be executed. Doing this way, once plugins are loaded, I register the sidebar widget that will display the link at my blog.

And that’s all.

Now you have to save your file with a php extension (for example triqui.php) and upload it in your wp-content/plugins directory you will find in your WP installation.

Then you will find your Triqui plugin in your plugin management page and once you activate it you will find the Triqui widget in your Sidebar Arrangement page (Presentation -> Widgets in your admin panel)

WordPress widget

… and… that’s it… now drag your widget into your sidebar and you will display my link in your blog.

This was the very first widget… but soon I will teach you how to develop more complex and interactive widgets, and, why not, widgets that could you earn some money.

Meanwhile, why don’t you display my widget for a day or two? If you do it…

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers