- WordPress 3 Plugin Development Essentials
- Brian Bondari Everett Griffiths
- 400字
- 2021-04-09 21:20:00
Proof of concept
Before we get WordPress involved at all, let's make sure that Digg's button works on a simple static page. If we can't make it work outside of WordPress, it's unlikely that we can make it work inside of WordPress. Take a look at the guide at http://about.digg.com/downloads/button/smart and get familiar with the sample code and the options available. We're going to try to make a simple static HTML page that implements a "Medium Smart Digg Button". According to Digg's documentation, all we need to do is add a bit of JavaScript to the document head and then use a special anchor tag on our page. Taking Digg's lead, the following is the HTML we've saved in a file named proof_of_concept.html
:
<html> <head> <title>Proof of Concept</title> <script type="text/javascript"> (function() { var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0]; s.type = 'text/javascript'; s.async = true; s.src = 'http://widgets.digg.com/buttons.js'; s1.parentNode.insertBefore(s, s1); })(); </script> </head> <body> <p>My post content goes here...</p> <a class="DiggThisButton DiggMedium"></a> </body> </html>
Visiting this page in a browser should look something like the following screenshot:

Make sure you see something like the preceding screenshot before you go on. This gives you an opportunity to troubleshoot the basic functionality before WordPress or PHP are involved.
Avoiding conflicting function names
Remember from Chapter 1 that WordPress' architecture normally relies on globally scoped functions in the main namespace, so name collisions are a big concern. In layman's terms, each function must have a unique name to avoid PHP errors. For example, the following PHP script would die with a fatal error:
<?php function main() { // Do something } function main() { // Oops... "Fatal error: Cannot redeclare main()" } ?>
Even if the functions are declared in different files, the names must be unique. As we write plugins, we must be careful to avoid using function names that are already in use by WordPress or by other plugins.
In this chapter, we will ensure unique function names by adding a prefix to each name, for example, my_plugin_main()
or my_plugin_some_function
. Usually this prefix is inspired by your plugin's name. We're naming our plugin Digg This, so we've used "diggthis" as a prefix to each function's name.
In later chapters, we will show you how to avoid conflicting function names by using PHP classes, objects, and static functions. Object-oriented coding represents a cleaner solution to the problem of namespace pollution, but it is a bit harder to follow, so we have dedicated this chapter to show you how we solved this problem using simple function names. We'll tackle the more advanced solutions later.
- PPT設(shè)計(jì)實(shí)用教程
- Joomla! Social Networking with JomSocial
- SOLIDWORKS Visualize 實(shí)例詳解(微視頻版)
- Photoshop+CorelDRAW平面設(shè)計(jì)實(shí)例教程(第4版)
- IT Inventory and Resource Management with OCS Inventory NG 1.02
- Jetpack Compose:Android全新UI編程
- 中文版Photoshop CC平面設(shè)計(jì)實(shí)用教程
- 有趣的Flutter:從0到1構(gòu)建跨平臺(tái)App
- ANSYS Workbench中文版超級(jí)學(xué)習(xí)手冊(cè)
- AutoCAD入門教程全掌握
- Photoshop CS6中文版基礎(chǔ)與實(shí)例教程(第6版)
- 抖音+剪映+Premiere短視頻制作從新手到高手(第2版)
- After Effects印象 影視高級(jí)特效光影篇
- 跨境電商:速賣通搜索排名規(guī)則解析與SEO技術(shù)
- 中文版Photoshop平面設(shè)計(jì)入門教程