PHP

PHP is a scripting language, just like BASH or JavaScript. It is interpreted by the computer instead of being compiled and executed. It is often used in I am using two primary sources for this page. The first is the 4th Edition of Learning PHP, MySQL, & JavaScript from the O’Reilly press. The second is the official PHP website. I like  a combination of reading and doing to best relearn or learn a language or concept. When I worked for Hewlett-Packard, I wrote evaluation webpages for beta software products our division was producing. The work was all done in HTML and PHP. Right now I am only using the PHP website as a reference. When I downloaded and tried to run their 7.0.11 version of PHP, Windows complained that the executable was not safe. When I find out more about this problem, I will hopefully be able to correct it and run their PHP environment. For now, I will be just using the book and doing experiments on this webpage.

PHP can be incorporated into your webpages or can be a stand-alone file on your web-server. The stand-alone files usually end in “.php“. However, you can also force “.htm” and “.html” files to be interpreted as “.php” files. Some programmers prefer this latter method to prevent people from easily seeing what is the files interpreted by PHP.

 <?php echo "This is a PHP print statement" \?>

PHP and WordPress

WordPress is written using PHP. For security reasons, PHP executable snippets are not allowed to run in a WordPress post or page. However, PHP can be added through plugins. WordPress has a great article about how to add PHP code via plugins at Getting Started with WordPress Plugin Development: The Ultimate Guide by Daniel Pataki.

Plugin Location

Plugins are located under ./wp_content/plugins. A directory is needed to hold the plugin code, where the directory is placed directly under the plugins directory. The name of directory and file to  initialize the plugin must be the same. For example:

./wp_content/plugins/cha-file-name/cha-file-name.php

Be careful of standard name conventions. CamelCase names are not allowed. All letters must be lower case, with no space, hyphens (“-“) or underscores (“_”) connecting the parts of the name. Actual PHP functions prefer underscores instead of hyphens.

Code

The header of the PHP file used as a plugin is critical, each line that contains a keyword in the header is used by WordPress to describe the Plugin to the user. With this format, when you open your plugins list, you should see the Plugin Name, Description, Author, Version, and Plugin URI. The Author URI is used as a link for the Author’s name.

<?php
/**
 * Plugin Name: My Facebook Tags
 * Plugin URI: http://www.cookhealthalliance.com
 * Description: This plugin adds some Facebook Open Graph tags to our single posts.
 * Version: 1.0.0
 * Author: Wayne Cook from work by Daniel Pataki
 * Author URI: http://www.cookhealthalliance.com
 * License: WWC1
 * 
 * This work is initially copied from the WordPress Page
 * "Getting Started with WordPress Plugin Development: The Ultimate Guide"
 * https://premium.wpmudev.org/blog/wordpress-plugin-development-guide/?utm_expid=3606929-91.15T0nlf8TFCqo1W_BlZjGg.0&utm_referrer=https%3A%2F%2Fpremium.wpmudev.org%2Fblog%2Fusing-ajax-with-wordpress%2F
 *
 * To add this to the header of each post, 'wp_head' must be specified. The add_action() adds 
 *  the following meta definitions to the headers of single blog entries.
 * Notice that the PHP commands come in groups surrounding the code that needs to be placed in the header.
 **/
add_action( 'wp_head', 'my_facebook_tags' );

function my_facebook_tags() {
  if( is_single() ) {
  ?>  
    <meta property="og:title" content="<?php the_title() ?>" />
    <meta property="og:site_name" content="<?php bloginfo( 'name' ) ?>" />
    <meta property="og:url" content="<?php the_permalink() ?>" />
    <meta property="og:description" content="<?php the_excerpt() ?>" />
    <meta property="og:type" content="article" />
    <?php 
      if ( has_post_thumbnail() ) :
        $image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' ); 
    ?>"/>  
    <!--?php endif; ?-->
  <?php
  }
}

The meta properties are a construct set up by WordPress to allow other entities to specify properties that they understand. Google set up Open Graph (og) standards for defining properties that it understands. Social Media sites, like Facebook, have adopted these standards, so that you can add information about your site or blog, using <b>php</b> constructs, that give it better visibility. This helps with your Search Engine Optimization (SEO) as well as making your shared link look better. The one thing I find interesting is that there is not closing ?–> after the last “}”.

Blog Page Before Plugin

This is a screen shot of my blog page before I added my plugin.

Blog Page before Plugin Activation

Blog Page before Plugin Activation

WordPress Activation

You need to copy your plugin to your wp-content/plugins directory. You will need to create a subdirectory with the identical name to your plugin. For example, my plugin name is MyFacebookTags.php. Therefore my directory name must be MyFacebookTags.

Plugin loaded, before Activation

Plugin loaded, before Activation

Blog Page After Activation

This is a screen shot of my blog page after activation. You can see the standard set of tags that have been added to the blog page to make sharing the post a bit easier.

Activated Plugin and Web Blog Page

Activated Plugin and Web Blog Page

Notice the Sharing icons in the upper left corner.

Creating Your Own Contact Form

Creating my own contact form taught me quite a bit about the WordPress, HTML, and PHP interaction. The form I created is:

This currenlty works on Edge, Chrome, Firefox, running test on Safari
First Name (required):

Last Name (required):

E-mail Address (required):

My area of interest is (not required):
Health and Nutrition
Computer Science
Education
Funding
Subject (required):

Comment: (not required)


A good free contact form to install is contact-form-7 by Takayuki Miyoshi. Remember that WordPress stores pages in well defined locations that are then linked to the expected URL for each page. A good plugin will reject absolute path page references and will allow WordPress to resolve the proper location using its own rules. When someone tries to access a page using the absolute address, the variable ‘ABSPATH’ will be set. I do check for ‘ABSPATH’ being set and exit the plugin if it is set. In this form plugin, I have two files.

  1. ./cha-contact-form/cha-contact-form.php – base – directory and file names the same.
  2. ./cha-contact-form/contact-php/ – directory storing my work file.
  3. ./cha-contact-form/contact-php/contact-shortcode-fn.php – creates form and handles POST.

WordPress has some excellent tutorials on how to set up your form and actions, especially in Plugin API/Action Reference/admin post (action). A good place to see how to build a basic form is HTML forms – the basics. To allow a line to access your plugin, you need to use the add_shortcode() PHP function. The add_shortcode() function needs some setup to access the function being called. The first name is the name to be used in the square bracket definition in the code. The second is the name of the function to be called.

Code Below the Header Comments:
// Do not execute if accessed directly
if(!defined('ABSPATH'))
	exit;
	
/** Constants ***************************************************/
global $wpdb;

// Path and URL
if ( !defined( 'CF_PLUGIN_DIR' ) )
	define( 'CF_PLUGIN_DIR', WP_PLUGIN_DIR . '/cha-contact-form' );
	
if ( !defined( 'CF_PLUGIN_NAME' ) )
	define( 'CF_PLUGIN_NAME', 'cha-contact-form' );	
require_once( CF_PLUGIN_DIR . '/contact-phps/contact-shortcode-fn.php');
add_shortcode( 'cha-contact-form', 'contact_shortcode_fn' );
?>
Place this in your Code:

This currenlty works on Edge, Chrome, Firefox, running test on Safari
First Name (required):

Last Name (required):

E-mail Address (required):

My area of interest is (not required):
Health and Nutrition
Computer Science
Education
Funding
Subject (required):

Comment: (not required)


As a side note, to print the “[” and “]”, I looked up the escape codes in Looking for a List of Valid HTML Escape Characters?