Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
WordPress Plugin Development: Beginner's Guide
WordPress Plugin Development: Beginner's Guide

WordPress Plugin Development: Beginner's Guide: Build powerful, interactive plug-ins for your blog and to share online

eBook
$22.99 $25.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

WordPress Plugin Development: Beginner's Guide

Chapter 2. Social Bookmarking

I hope the first chapter got you warmed up and prepared for WordPress plugin development, and that you are as eager to start as I am.

In this chapter, we will create our first functional WordPress plugin and learn how to interact with the WordPress API (this is the WordPress interface to PHP) on the way. The knowledge you will gain in this chapter alone will allow you to write a lot of similar plugins.

Let's get moving! In this chapter, you will learn:

  • Creating a new plugin and having it displayed in the plugins admin panel

  • Checking the WordPress version and control activation of the plugin

  • Accessing API features—for example the title and permalink URL of each post

  • Using WordPress hooks to execute your plugin code when it's needed

  • Using conditional tags to control the flow of your plugins

You will learn these by:

  • Creating a 'social bookmarking' type of plugin that adds a Digg button to each post on your blog

As you probably know, Digg is a very popular service for promoting...

Plugging in your first plugin


Usually, the first step in plugin creation is coming up with a plugin name. We usually want to use a name that is associated with what the plugin does, so we will call this plugin, WP Digg This. WP is a common prefix used to name WordPress plugins.

To introduce the plugin to WordPress, we need to create a standard plugin header. This will always be the first piece of code in the plugin file and it is used to identify the plugin to WordPress.

Time for action — Create your first plugin

In this example, we're going to write the code to register the plugin with WordPress, describe what the plugin does for the user, check whether it works on the currently installed version of WordPress, and to activate it.

  1. Create a file called wp-digg-this.php in your favourite text editor. It is common practice to use the plugin name as the name for the plugin file, with dashes '-' instead of spaces.

  2. Next, add a plugin information header. The format of the header is always the same...

Displaying a Digg button


Now it's time to expand our plugin with concrete functionality and add a Digg link to every post on our blog.

In order to create a link we will need to extract post's permalink URL, title, and description. Luckily, WordPress provides us with a variety of ways to do this.

Time for action — Implement a Digg link

Let's create a function to display a Digg submit link using information from the post.

Then we will implement this function into our theme, to show the link just after the post content.

  1. Add a function to our plugin to display a Digg link:

    /* Show a Digg This link */
    function WPDiggThis_Link()
    {
    global $post;
    // get the URL to the post
    $link=urlencode(get_permalink($post->ID));
    // get the post title
    $title=urlencode($post->post_title);
    // get first 350 characters of post and strip it off // HTML tags
    $text=urlencode(substr(strip_tags($post->post_content), 0, 350));
    // create a Digg link and return it
    return '<a href="http://digg.com/submit?url=...

WordPress plugin hooks


Our plugin now works fine, but there is a problem. In order to use it, we also have to edit the theme. This can be a real pain for all sorts of reasons:

  • If you want to change to a different theme, the plugin will stop working until you edit the new theme.

  • If you want to distribute your plugin to other people, they can't just install it and activate it; they have to change their theme files too.

  • If you change the function name, you need to alter the theme files again

We need some way to make the plugin work on its own, without the users having to change their themes or anything else.

Hooks come to the rescue, making it possible to display our Digg This button in our posts—without ever modifying our theme.

Time for action — Use a filter hook

We will use the the_content filter hook to automatically add our Digg This link to the end of the post content. This will avoid the need for the users to edit their theme files if they want to use our plugin.

  1. Create a function that...

Adding a Digg button using JavaScript code


Our Digg link works fine for submitting the content, but isn't very pretty, and does not show the number of Diggs we received. That is why we need to use a standard Digg button.

This is accomplished by using a simple piece of JavaScript code provided by Digg, and passing it the necessary information.

Time for action — Implement a Digg button

Let us implement a Digg button, using information from the Digg API. We will use the newly created button on single posts, and keep the simple Digg link for all the other pages.

  1. Create a new function for displaying a nice Digg button using JavaScript code.

    /* Return a Digg button */
    function WPDiggThis_Button()
    {
    
    global $post;
    
    // get the URL to the post
    $link=js_escape(get_permalink($post->ID));
    
    // get the post title
    $title=js_escape($post->post_title);
    
    // get the content
    $text=js_escape(substr(strip_tags($post->post_content), 0, 350));
    
    // create a Digg button and return it
    $button="
    <script type...

Styling the output


Our Digg button looks like it could use a better positioning, as the default one spoils the look of the theme. So, we will use CSS to reposition the button.

Cascading Style Sheets or CSS for short (http://www.w3.org/Style/CSS/) are a simple but powerful tool that allows web developers to add different styles to web presentations. They allow full control over the layout, size and colour of elements on a given page.

Time for action — Use CSS to position the button

Using CSS styles, we will move the button to the right of the post.

  1. We will accomplish this by first encapsulating the button in a<div> element. Then we will add a CSS style to this element stating that the button should appear on the right, with a left margin towards the text of 10 pixels.

    // create a Digg button and return it
    $button="
    <script type='text/javascript'>
    digg_url = '$link';
    digg_title = '$title';
    digg_bodytext = '$text';
    </script>
    <script src='http://digg.com/tools/diggthis.js...

Summary


In this chapter, we created a working, useful, and attractive WordPress plugin from scratch. Our plugin now displays a fully functional Digg button.

We learned how to extract information using WordPress API and how to use CSS to improve the appearance of our plugin. We also investigated some more advanced WordPress functionalities such as hooks.

Specifically, we covered:

  • Creating a plugin: How to fill in the information header and create a simple plugin template

  • Checking WordPress version: How to check that our plugin is compatible with the user’s version of WordPress

  • Modifying theme files: How to safely add functions to the theme files when we need to

  • Accessing post information: Different ways of obtaining data from the post such as title, permalink and content

  • Using WordPress hooks: How to use actions and filters to get things done from within our plugin (and not modifying the theme for instance)

Now that we've learned about WordPress hooks, we are ready to expand our knowledge and...

Left arrow icon Right arrow icon

Key benefits

  • Everything you need to create and distribute your own plug-ins following WordPress coding standards
  • Walk through the development of six complete, feature-rich, real-world plug-ins that are being used by thousands of WP users
  • Written by Vladimir Prelovac, WordPress expert and developer of WordPress plug-ins such as Smart YouTube and Plugin Central
  • Part of Packt's Beginners Guide series: expect step-by-step instructions with an emphasis on experimentation and tweaking code

Description

If you can write WordPress plug-ins, you can make WordPress do just about anything. From making the site easier to administer, to adding the odd tweak or new feature, to completely changing the way your blog works, plug-ins are the method WordPress offers to customize and extend its functionality. This book will show you how to build all sorts of WordPress plug-ins: admin plug-ins, Widgets, plug-ins that alter your post output, present custom "views" of your blog, and more. WordPress Plug-in Development (Beginner's Guide) focuses on teaching you all aspects of modern WordPress development. The book uses real and published WordPress plug-ins and follows their creation from the idea to the finishing touches, in a series of carefully picked, easy-to-follow tutorials. You will discover how to use the WordPress API in all typical situations, from displaying output on the site in the beginning to turning WordPress into a CMS in the last chapter. In Chapters 2 to 7 you will develop six concrete plug-ins and conquer all aspects of WordPress development. Each new chapter and each new plug-in introduces different features of WordPress and how to put them to good use, allowing you to gradually advance your knowledge. This book is written as a guide to take your WordPress skills from the very beginning to the level where you are able to completely understand how WordPress works and how you can use it to your advantage.

Who is this book for?

This book is for programmers working with WordPress, who want to develop custom plug-ins and to hack the code base. You need to be familiar with the basics of WordPress and PHP programming and believe that code is poetry; this book will handle the rest.

What you will learn

  • Get to know the WordPress code base, WordPress s plug-in architecture, and the plug-in application programming interface (API) and learn how to hack it
  • Master the WordPress database and the API ñ access and manipulate data, handle user roles and permissions, posts, and so on
  • Hook into the rest of WordPress using actions and filters
  • Change the way your WordPress backend looks by customizing menus, submenus, and the plug-in admin panel
  • Integrate AJAX and jQuery into your plug-ins to dynamically generate content
  • Hook directly to WordPress edit pages and use AJAX to generate fast searches
  • Integrate your plug-in with WordPress panels and the tinyMCE editor
  • Access and work with third-party APIs like Flickr
  • Implement localization support for users of other languages
  • Maintain and manage your plug-in using SVN and publish it to the WordPress Plugin Repository
  • Handle security issues and make your plug-ins safer to use
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 16, 2009
Length: 296 pages
Edition : 1st
Language : English
ISBN-13 : 9781847193599
Vendor :
WordPress Foundation
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Feb 16, 2009
Length: 296 pages
Edition : 1st
Language : English
ISBN-13 : 9781847193599
Vendor :
WordPress Foundation
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 131.97
WordPress Plugin Development Cookbook
$43.99
WordPress 3 Plugin Development Essentials
$43.99
WordPress Plugin Development: Beginner's Guide
$43.99
Total $ 131.97 Stars icon

Table of Contents

8 Chapters
Preparing for WordPress Development Chevron down icon Chevron up icon
Social Bookmarking Chevron down icon Chevron up icon
Live Blogroll Chevron down icon Chevron up icon
The Wall Chevron down icon Chevron up icon
Snazzy Archives Chevron down icon Chevron up icon
Insights for WordPress Chevron down icon Chevron up icon
Post Types Chevron down icon Chevron up icon
Development Goodies Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2
(10 Ratings)
5 star 60%
4 star 20%
3 star 10%
2 star 0%
1 star 10%
Filter icon Filter
Top Reviews

Filter reviews by




David Patrick Oct 17, 2012
Full star icon Full star icon Full star icon Full star icon Full star icon 5
If you want to get started with wordpress plugin development, then this is what you need. I cant say anything more then that :)
Amazon Verified review Amazon
Honor Macdonald Aug 27, 2010
Full star icon Full star icon Full star icon Full star icon Full star icon 5
In this book the author walks you, step by step, through the creation of several viable WordPress plug-ins. Each new one builds on functionality and process learned in previous ones, to help you gaim more understanding of how the WordPress framework handles it's data, and what capabilities you have, as a programmer, in manipulating and presenting that data, and adding valuable function to the WordPress core.You don't really want to jump into this book (or this subject) if you don't know PHP from PCP, but I really don't think you'll need more than a basic familiarity with PHP to get rolling with it, either. The author does an excellent job of telling you what the code is doing and how it's doing it when each new bit is added.While it's definitely time for an update to this title (I'd suggest you search for a newer edition before you buy) the lessons you can learn here are still valid and easily applicable to 3.0.
Amazon Verified review Amazon
Joe Wilson Nov 04, 2009
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is written in a very easy to understand fashion. Each example builds on the the previous example. Its a great way for someone like me, who likes to learn by doing, to learn how to create plugins.There is a lot of information out on the web that will tell you how to create a plug-in, but this book allows you to follow the creation of a plugin from start to finish.Highly recommended
Amazon Verified review Amazon
Tyler Kareeson Apr 13, 2009
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book talks about the why and how of WordPress plugin development with heavy emphasis on the "how". The book's tagline "Learn by doing: less theory, more results" describes the book quite well. There is an introductory chapter that talks about the many benefits of learning WordPress plugin development. After that, the book immediately jumps right into showing you how to develop a series of 6 increasingly challenging WordPress plugins from scratch. It then finishes up by talking about plugin localization, promotion, and support tips.Even though this book is a "Beginner's Guide," there are a lot things in there that many advanced WordPress plugin developers can benefit from. I don't consider myself a beginner in WordPress plugin development (see my WordPress plugins), and I have definitely learned quite a bit of things after reading this book.[...]
Amazon Verified review Amazon
Kurt Pachinger Jan 30, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I learned many applicable techniques.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
Modal Close icon
Modal Close icon