How to write a WordPress Plugin part 3 – Creating the WordPress Plugin

Creating the WordPress Plugin

Ok, you just created the Javascript/HTML/CSS code you want to use for your WordPress site on an HTML page. How the heck you get this into your WordPress page? Well, with the magic of plugins.

What are plugins?

Plugins are a file and directory structure used by WordPress to add code to your WordPress site. Here is an example of the directory structure .

public_html/wp-content/plugins/
public_html/wp-content/plugins/movetotop/movetotop.php
public_html/wp-content/plugins/movetotop/assets
public_html/wp-content/plugins/movetotop/css
public_html/wp-content/plugins/movetotop/js
public_html/wp-content/plugins/movetotop/README.md

The main file that tells WordPress plugin how to run is movetotop.php

The other directories and files are:

README.md – Markdown readme file.
assets – image files and fonts used by plugin. Fontawesome is used here.
css – Cascading Stylesheet used by plugin. The stylesheet totop.css lives here
js – JavaScript files used by plugin. The JavaScript totop.js file code is here.

Creating the plugin.

Chop it up.

First things first, we will “chop” up the HTML and JavaScript code and place it where it needs to go.

To make the file naming consistant with the plugin name, I renamed the custom.css file to totop.css and custom.js to totop.js. Otherwise, there is no change to the content of the files or code enclosed.

I also separated the fontawesome files into assets/fontawesome-5 directory so to better organize these assets.

Movetotop.php and how it’s organized.

The real magic of WordPress plugin is found in the movetotop.php file.

There are three sections found in a plugin file:

Header. This is commented header enclosed is /* */

add_action/add_filter – this tells WordPress to execute scripts to add the CSS/HTML/JavaScript files into the HTML generated by WordPress sent to the web browser.

Function(s). This is the scripts that injects the CSS, HTML or JavaScript files when called by add_action/add_filter.

Well, let’s go into how this file is created and what is used.

Header File

Header file contains the following:

/*
Plugin Name:
Plugin URI:
Description:
Author:
Version:
Author URI:
*/

This is self explanatory. Without this information, the plugin will not show up in the WordPress plugin dashboard.

The hook function

The hook functions add_action/add_filter – This adds the CSS/HTML and JavaScript file into WordPress hooks. Generally, hooks are the location of the content.

Here are the five hooks used by this plugin:

add_action(‘wp_head’,’AddFontAwesomeCSS’);
add_action(‘wp_head’,’AddMoveToTopCSS’);
add_filter(‘the_content’,’AddMoveToTopButton’);
add_action(‘wp_footer’,’LoadJQuery’);
add_action(‘wp_footer’,’AddMoveToTopScript’);

Basically, the format is:

(Location of content(hook), Function content to be applied).

There are MORE hooks than what we’re using here (over 1,000+ hooks, see this link for a comprehensive list) which do a variety of things. For this example we’re just sticking to the basic ones.

Function

The function that injects the content inside the HTML.

Here is an example:

function AddMoveToTopCSS() {
$cssurl=plugins_url(‘/css/totop.css’,__FILE__);?>
<link rel=”stylesheet” href=”<?php echo $cssurl; ?>”>
<?php }

This function injects the css/totop.css file name and location and outputs it as a text HTML reference. This is then called by the hook function and injected in the location (hook) specified.

Wrap up.

That’s how you develop a basic WordPress plugin. We didn’t cover other stuff like an admin backend interface with configurable parameters. That we may do another time.

In the meantime, have a good day.

Roy

 

 

 

 

How to write a WordPress Plugin part 2 – Static HTML/CSS/JavaScript

When you write a WordPress plugin, you are essentially inserting HTML and JavaScript code into a webpage. The first thing you do is write a static HTML page with the effect you have in mind first.
While there are several kinds of plugins available for WordPress, we’ll only concern ourselves with adding a feature to the site as described above.

The first thing I did was fire up my favorite editor.
I used Visual Studio Code https://code.visualstudio.com/. You can use any editor you wish such as Sublime, Brackets, Notepad++, Atom.
I like VS Code since it integrates EMMET https://emmet.io/, has built in IntelliSense https://en.wikipedia.org/wiki/Intelligent_code_completion#IntelliSense, and supports several plugins that make it very useful.

If you follow the link https://github.com/roadkillon101/HTML-example-of-MoveToTop, you can download the source files.

The first file I’d like you to look at is the index.htm file.
If you open it up in a web browser, you will see a bunch of lorem ipsum text. You scroll down the page, the “move-to-top” button will show up at the lower right hand corner of the web page. You press the button and the page will scroll to the top of the page.
Open up this index.htm file in the editor and I’d like you to notice the following:

<script type="text/javascript" src="./js/lib/jquery/jquery-1.10.2.js"></script>
<script type="text/javascript" src="./js/custom.js"></script>
<link rel="stylesheet" type="text/css" href="./css/fontawesome-all.css"/>
<link rel="stylesheet" type="text/css" href="./css/custom.css"/>

To make the button appear, we need the following:

JQuery – for the animation effects.

FontAwesome – for the up angles in the button.

custom.js – for the JavaScript code.

custom.css – for the styling of the button.

We also have the button itself with the FontAwesome fonts in the HTML.

<button onclick="topFunction()" id="toTop" title="Go to top"><i class="fas fa-angle-double-up fa-2x"></i></button> 
</button>

This code has to first work in a static HTML page before you make it work anywhere else.
I tweeked the code until it looked right and behaved as I expected it to here before making it work as a plugin in WordPress.

I won’t go into detail about how to write JavaScript or write CSS styling. You can examine the code and see how it works.

What I want you to notice is where the snippets of code are placed in the HTML. This is where the snippets need to be placed in the WordPress dynamically generated pages.

In this case, the code is in the header of the HTML and the body.

The next article will describe how to turn it into a WordPress plugin.

 

Writing a basic WordPress plugin (MoveToTop) part 1 – Basic overview

You may wonder how you go about modifying your WordPress site just adding a piece of JavaScript code.

Perhaps you are handy with writing some HTML, CSS, and JavaScript but you don’t know where to begin with converting it into a plugin for WordPress.

I will show you how you can do this with the MoveToTop example plugin that I have created and placed on GitHub.

You don’t have to write a whole blown out plugin if you just want to add a simple code snipped or tweak to your site, plus you can use it in ANY of your sites you need this snippet.

This plugin which I use on this site, creates a button on the lower right corner of the web page. When you click on it, it moves the web page to the top of the page (Get it…move to top…) You can customize it’s behavior and appearance by modifying the CSS and JavaScript code.

To get this magic to work, the first thing you need to do is to write a basic HTML page with the plugin in question. The script (totop.js) has to be working along with the HTML (found in movetotop.php), CSS (totop.css) appropriate libraries such as J QueryFont Awesome.

I wrote a static HTML page and made this page work first.

In the next article, I will show you how I developed the test code.

Obscure VOIP bug on Verizon/Motorola phones.

For the past week, I was trying to figure out why the ZOIPER VOIP application on android didn’t work
on WIFI to my VOIP PBX but worked when it was on the 4G Data network.
My own phone LG S3 worked fine with the same kind of SIP extension.
I made sure STUN worked (used for going through firewalls), and it worked fine with my device, just not his.
I thought it might be the Wireless hardware, the router, even the Xfinity cable internet service.
I had the device even tested on other wireless networks with other providers, still nothing.
After this, I had a laptop loaded up with ZOIPER and a new VOIP/SIP extension and it worked.
Then we did a google search on something like “verizon moto does not work with zoiper”
and turned up an obscure article on this…
Cannot connect to SIP Servers when on WiFi. When on LTE, there are no issues.
It turns out, one of the premium “features” (translation: BUG) in some of the newer Verizon
smartphones like Pixel, allows the phones to make a phone call through WIFI if you don’t have 4G/Cell service
available. This “feature” blocks 5060 (SIP) port from making a connection, needed for standard
SIP VOIP phones. This “feature” has been known about since November 2016 and yet, there is
no addressing of this issue by Verizon or Motorola.
You have to change the port SIP runs on to something else to make it work.
Luckily, ZOIPER supports IAX2 (port 4569), once we created an extension, it works.
It doesn’t even need STUN to traverse a NATTED firewall. It just works.

Distinguishing between new tech and tech marketing…

“Build it and he will come…”

That is a reference to the movie “Field of Dreams” where Kevin Costner’s character hears a voice that tells him to build a baseball field in the middle of his farm.  While the idea sounds crazy, you have to start building a project with nothing more than a prompting to get started.

This blog will be an exercise in just writing what I am doing in the technology world and to learn to be consistent in writing, more specifically, I will be sharing ideas for writing code, some projects I’m working on, new technology ideas and see what sticks.

Primarily, this blog will focus on front end development of hybrid mobile applications and desktop applications based on the JavaScript framework packages.  I will also delve into my thoughts of what’s useful or not.

Now with that said, there seems to be many different technologies that are starting to crawl out of the woodwork.  Some of the tech has the potential to solve some real problems, however other “solutions” are a form of “vaporware” which will be marketed widely, but when the marketing doesn’t take, will vanish like Silverlight or HDDVD.

It takes some time to learn a new framework or language and even more time to develop an application, so how do you avoid investing your time in a language or a framework  that is going nowhere?

I don’t claim to have a crystal ball or know the future, however if you are reading this blog, you do your research and get a couple of different perspectives.

Here are some of my thoughts:

1 – Be contrarian: if you see a product/service/tech that is heavily promoted in popular media, then be weary of it.

2 – Find out the value of the tool: Does it solve a problem that another (open source) solution already solve? Are there lower cost or free alternatives?

3 – Is it being used?: Are their other companies or groups using the products/solutions?

4 – What is the mind share of the tool?: How many people are using the tool for their project?

1 – Be contrarian.  One of the sure ways you can tell if you are in a financial bubble (over-inflated prices of a market) is when the news media such as TV, Newspaper, High volume news sites start saying about real estate, dot com stocks, Bitcoin, etc. saying that you need to get in before you lose out.  If this is true about financial bubbles, this is likely also true about hyping up new technology.  In this case, it would take the form of several technical books written about a new framework, Tech “Evangelists” who promote a given framework,  Free lunch seminars given by a mega marketing software giant.  You get the picture.

2 – Find out the value of the tool.  Microsoft Silverlight for example does have some nice bells and whistles that Adobe Flash doesn’t have, however with the new open source standards  that HTML5/CSS3/JavaScript was coming out with that you don’t have to pay licensing fees for and is ported to every platform, why use EITHER Flash or Silverlight when HTML/CSS/JS has the capabilities of both without the cost or other problems associated with proprietary solutions.

3 – Is it being used?: Do research on who is using a given platform or technology and what  solutions integrate into this.  AngularJS is used in many SPA (Single Page Application) websites and in addition is the core framework which Ionic (Mobile Hybrid Framework) uses for developing mobile applications.

4 – What is the market share of the tool.  Initially, when I was looking at a mobile cross platform framework, I was looking at Xamarin which appealed to me since I knew how to write code in C# and would not have to relearn a framework.  There was TONS of marketing in the search engines and books, speakers, etc.  This made me weary (see 1 – Be contrarian) so I looked up info related to the mind share Xamarin has and it represents about 2% of the market versus over 50% of cordova/phonegap.  This led me down the path of researching cordova, then Ionic.  Hybrid mobile apps are used everywhere both internally at companies and externally on Google Play and iTunes.

In either case, this is my though process on distinguishing between marketing hype of tech and what is actually will take hold and solve problems for you and your client.

If you have any ideas to share, please feel free to post or send me an email.

roy@customizedcode.us