WordPress Plugin

Start WP Plugin Example

This example can be used as a template for building more complex plugins for actual production and distribution. All it does is send an email to a named recipient when a page or post is loaded by a user (including the title of the page or post).

First, create a new folder in the WordPress Plugins folder. The new folder should be the slug name of your plugin, in this case page-post-load-notifier.

Second, upload an initial PHP file to your new plugin folder. The name of the PHP file should be the same as the new plugin folder (not including the .PHP extension). In this case: page-post-load-notifier.php. Other files can be added to this folder later, depending on plugin scope and design.

The PHP file in this case contains the following code:


<?php
/**
* Plugin Name: Page Post Notifier
* Plugin URI: http://downrecs.com
* Description: Sends an email with a page or post name when that page or post is loaded.
* Version: 1.0.0
* Author: Nels Johnson
* Author URI: http://downrecs.com
* License: GPL2
*/

add_action( 'template_redirect', 'post_or_page_loaded_email_notification' );
function post_or_page_loaded_email_notification( $post ) {
  $loaded_title = get_the_title($post->ID);
  if ( !is_admin() ) {
    $email = 'webmaster@yoursite';
    $subject = 'Your Site Page or Post Viewed: ' . $loaded_title;
    $message = 'Someone just viewed the page or post named: ' . $loaded_title;
    wp_mail( $email, $subject, $message );
  }
}


Third, go to WordPress Admin and activate the plugin.

End WP Plugin Example