How to Create Google +1 WordPress Plugin

If you are a web developer with basic PHP skills just like me than writing a WordPress plugin is not difficult for you. All you need, PHP skills, a right direction, some resources, a little information on how WordPress expects your plugin to behave, and most importantly a great idea.

How to create a Google + Plugin for WordPress. Although there are plugins available but wouldn’t it be nice to learn how to create one from scratch? And that’s the topic of my post today — I’ll show you how to create your very own WordPress Plugin to display your Google plus feed in your post.

This tutorial ” How to Create own Google+1 WordPress Plugin” is only for the learning purpose, and is the clone of Easy Google +1 Plugin.

Names, Files, and Locations

Plugin Name

The plugin name must be unique, Check out Plugins directory and the other repositories it refers to, to verify that your name is unique; you might also do a Google search on your proposed name. I choose WPArena Google +1 for this plugin.

Plugin Files

The file name of PHP should be unique because People who install your Plugin will be putting this PHP file into the WordPress Plugin directory in their installation, wp-content/plugins/, so no two Plugins they are using can have the same PHP file name.

Another option is to split your Plugin into multiple files. Your WordPress Plugin must have at least one PHP file; it could also contain JavaScript files, CSS files, image files, language files, etc. If there are multiple files, pick a unique name for a file directory and for the main PHP file, such as create a directory name “wparena-google-plusone” and two files as easy-google-plusone.php and easy-google-plusone-admin.php

Readme File

If you want to host your Plugin on http://wordpress.org/extend/plugins/, you also need to create a readme.txt file in a standard format and include it with your Plugin. See http://wordpress.org/extend/plugins/about/readme.txt for a description of the format.

Right now we are not adding this plugin to WordPress Plugin Directory, so we are not creating the one.

Home Page

It is also very useful to create a web page to act as the home page for your WordPress Plugin. This page should describe how to install the Plugin, what it does, what versions of WordPress it is compatible with, what has changed from version to version of your Plugin, and how to use the Plugin.

File Headers

The first thing is to add some information to your main Plugin PHP file, as we would add in a file named easy-google-plusone.php file.

Standard Plugin Information

Without the header, the plugin will never be activated, so it’s very important to Plugin’s main PHP file must contain a standard Plugin information header. As we will add the following information about our wpArena Google +1 plugin:

The most important is the Plugin Name line and the rest of the information will be used to create the table of Plugins on the Plugin management screen. The order of the lines is not important.

GPL2 is the License slug that should be a short common identifier.

Important: file must be in UTF-8 encoding.

To indicate a GPL2 license, include the following lines in your Plugin:

<?php
/* Copyright YEAR PLUGIN_AUTHOR_NAME (email : PLUGIN AUTHOR EMAIL)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
?>

Programming Your Plugin

If you have created above files in a directory “wparena-google-plusone” including with these two files “easy-google-plusone.php” and “easy-google-plusone-admin.php” and add this plugin directory in a wp-content/plugins directory, you can see this plugin in the administration panel so WordPress is aware of it. However, it doesn’t do anything as it contains nothing except the information header. We are going to change this now.

WordPress Plugin Hooks

Hooks are the backbone of WordPress. They enable plugin developers to “hook” into the WordPress workflow to change how it works without directly modifying the core code. This enables users to easily upgrade to newer versions of WordPress without losing modifications.

WordPress has two primary types of hooks: action hooks and filter hooks.

Action Hooks: Enables you to execute a function at a certain point.

Filter Hooks: Enables you to manipulate the output passed through the hook. Hooks aren’t just for plugins. WordPress uses hooks internally. If you browse through the core source code, you can see many examples of how WordPress uses its own system to hook into itself. Read detail about Hooks: Professional WordPress Plugin Development.

For WPArena Google +1 Plugin, by calling the “wp-head” function, we can execute and register a function before WordPress adds the head contents, That can be done using the “wp_head” action hook which we will use to add the Google +1 button script to the<head> tag. After this now we need to add Google+ button before or after the posts, that can be done registering a function to be executed before displaying your posts’ content using the “the_content” filter hook. So, Plugin will use the following two hooks:

Override the Post Content

The add_filter WordPress function takes the two parameters the filter hook name and plugin function name, which will register a function to filter the content of the posts.

The following code registers the wpgpo_content function to filter the_content hook and creates the wpgpo_content function which has one parameter the $content variable. The $content parameter contains post content.

The following code will add the +1 button tag to your posts’ content by PHP dot concatenation and then return the new content. And will add the default settings +1 tag to the beginning of your posts. You can easily concatenate it to the end of your posts by starting first by the $content variable.

At the end give style to Google+ button and then to add your posts’ permalink as the href attribute of the +1 button. Get the post permalink using the get_permalink() function that takes the post ID as parameter. The $post variable is one of WordPress global variables that will use to get the current post ID.

Reader Interactions

Thank you for choosing to leave a comment on WPArena. Please bear in mind that all comments are moderated according to our comment policy. Your email address will never be published but is required. Do NOT use keywords in the name field. First name is fine with us. Lets have a meaningful conversation and respect others opinions.

Name *

Email *

Website

Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Our Services

Footer

About

WPArena is a premium online resource site of WordPress and is focused on providing excellent WordPress Tutorials, Guides, Tips, and Collections. The site design is updated at regular intervals with more features. Look here. You can read more about WordPress Arena here.