Category: Tech

Plugins and Shortcodes

Plugins and shortcodes can extend WordPress’s power markedly. Tons of free, pre-written ones are available at https://wordpress.org/plugins/. The header there says “Browse 60,453 free plugins” which means most every topic is covered, probably many times over.

What if you want to do your own plugins? It turns out that isn’t very difficult and there are many resources to help both getting started and with on-going development activities. Here are a few resources I started with. Note that many of these give code examples which I find very useful for learning anything new.

Some Examples

I followed the first reference to generate a shortcode for the current year, which is 2025 and then branched off from there to generate another for the current date and time: 10 Dec 2025 3:48 UTC. The highlighted items are the results of the shortcodes. Neither of these took any input, but merely returned the appropriate values when invoked.

Current Year:

<?php

/*
Plugin Name:  Salcodes
Version: 1.0
Description: Output the current year in your WordPress site.
Author: Salman Ravoof
Author URI: https://www.salmanravoof.com/
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: salcodes
*/

/**
 * [current_year] returns the Current Year as a 4-digit string.
 * @return string Current Year
*/

add_shortcode( 'current_year', 'salcodes_year' );
function salcodes_init($atts){
 function salcodes_year() {
 return getdate()['year'];
 //return "ollie";
 }
}
add_action('init', 'salcodes_init');

/** Always end your PHP files with this closing tag */
?>

Date-Time Display

<?php
/*
Plugin Name: date-time-display
Description: Displays the date and time of day.
Version: 1.0.1
Author: owa
License: GPL2
 */

/*
 * Shortcode is [date_time_display]
 * returns date string formatted like 17 Feb 2023 14:26 UTC
 * Prefix is dtd
 */

function date_time_display($atts) {
  // format date as 17 Feb 2023 14:26 UTC
  $current_date_time = date('d M Y G:i e');
  //then output it
  return $current_date_time;
}

function dtd_shortcodes_init(){
 add_shortcode('date_time_display', 'date_time_display');
}
add_action('init', 'dtd_shortcodes_init');
?>

User IP and Location

On a closing note, here is a plugin available at the WordPress site here that generates information about the current user. See an example of some of its features in the footer, below.

Implementing https on site

Implementing SSL security on a website is now quite easy — and free, using the Let’s Encrypt free and automated certificate authority.

The process is well described here which takes you through the setup of the certificate and implementing it on an apache website. It includes setting up auto-renewal, to guarantee fresh certificates at all times.

A final step, forcing all web traffic to the secure (https) server using apache’s redirect (not rewrite) rule is described here.

All in all, the process took no more than a couple of hours, including a few redos along the way with typical first-time mistakes.

 

Some apt-get commands

Ubuntu uses the apt package management system which makes updates and upgrades pretty simple.

When logging on, ubuntu summarizes available updates. Use the following to do the updates:

sudo apt-get upgrade

Note, though:

However, if you want to upgrade, unconcerned of whether software packages will be added or removed to fulfill dependencies, use the ‘dist-upgrade‘ sub command.

Many more apt-get commands are summarized in 25 Useful Basic Commands of APT-GET and APT-CACHE for Package Management.

‘man apt-get‘ or ‘man apt-cache’ are also useful.

NameVirtualHosts

Apache’s NameVirtualHost mechanism allows (among other things) a single numeric ip address to handle multiple domains’ websites. For example, the DNS entries for both this site (home.acheson.org) and another site point to the same numeric ip address. Apache delivers the appropriate content to incoming requests based upon the domain name in the request record.

Setting this up is quite straightforward and is well described in Apache’s documentation: http://httpd.apache.org/docs/2.2/vhosts/examples.html

DNS Works

When you create an EC2 instance, Amazon assigns a so-called public DNS to it that gets you to your new instance via browser or ssh client or whatever. It is not exactly memorable, looking something like ec2-54-123-45-67.compute-1.amazonaws.com. With just a few minutes of effort, though, you can set your own domain name to point to the new instance.

The first step is to create an “elastic ip address” in the EC2 management console (look for the link in the left sidebar). This gives you a unique numeric ip address to use.

Next, once you have that new ip address, you need to link it to the appropriate EC2 instance. Once that is done, the ip address is “live” which you can test in your browser.

Finally, use this newly created numeric ip address to set up a new “A” record in your DNS system. It will take a short time to propogate, though this seems to be measured in seconds now as opposed to the hours of only a few years ago. Your new instance should now be accessible via your own domain name.

Hello World

Today I set up a new D7 instance on Amazon Web Services.

Some basics about it:

  • The EC2 instance is based on an Ubuntu 12.10.
  • apt-get install added apache2, php5, then mysql, xml, mcrypt, mbstring and gd (sometimes the exact names were php5-name, e.g., php5-mcrypt.
  • Article at http://www.techrepublic.com/blog/datacenter/build-a-drupal-installation-on-amazon-ec2/5231 was a helphful checklist.
  • After the installation, df -h showed 6.3 Gb available out of 7.9 Gb total. Lots of room.