- WordPress Plugin Development Beginner's Guide
- Vladimir Prelovac
- 678字
- 2021-05-21 20:12:19
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.
- 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='text/javascript'> digg_url = '$link'; digg_title = '$title'; digg_bodytext = '$text'; </script> <script src='http://digg.com/tools/diggthis.js' type='text/javascript'></script>" return ($button); }
- Modify our filter function to include the Digg button for single posts and pages, and a Digg link for all the other pages:
/* Add Digg This to the post */ function WPDiggThis_ContentFilter($content) { // if on single post or page display the button if (is_single() || is_page()) return WPDiggThis_Button().$content; else return $content.WPDiggThis_Link(); }
- Digg button now shows at the beginning of the single post page.
What just happened?
WordPress will parse our content filter function according to the conditional statement we have added:
function WPDiggThis_ContentFilter($content) { // if on single post or page display the button if (is_single() || is_page()) return WPDiggThis_Button().$content;
This means that if the current viewed page is a single post or page, we will append our Digg button at the beginning of that post.
If we are viewing all the other pages on the blog (like for example the home page or archives) we will show the Digg This link instead.
if (is_single() || is_page()) return WPDiggThis_Button().$content; else return $content.WPDiggThis_Link(); }
The reason for doing so is that we do not want to clutter the home page of the blog with a lot of big yellow Digg buttons. So we just place a subtle link below the post instead. On single pages, we show the normal button using our new WPDiggThis_Button()
function.
The first part is similar to our previous WPDiggThis_Link()
function, and it acquires the necessary post information.
/* 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));
However in this case, we are treating all the information through the js_escape()
WordPress function, which handles formatting of content for usage in JavaScript code. This includes handling of quotes, double quotes and line endings, and is necessary to make sure that our JavaScript code will work properly.
We then create a code using Digg API documentation for a JavaScript button:
// 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' type='text/javascript'></script>";
Conditional Tags
We have used two functions in our example, is_single()
and is_page()
. These are WordPress conditional tags and are useful for determining the currently viewed page on the blog. We used them to determine if we want to display a button or just a link.
WordPress provides a number of conditional tags that can be used to control execution of your code depending on what the user is currently viewing.
Here is the reference table for some of the most popular conditional tags.

Conditional tags are used in a variety of ways. For example, is_single('15')
checks whether the current page is a single post with ID 15. You can also check by title. is_page('About')
checks if we are on the page with the title 'About'.
Note
Quick reference
is_single(),
is_page()
: These are conditional tags to determine the nature of the currently viewed content
js_escape()
: A WordPress function to properly escape the strings to be used in JavaScript code
WordPress Conditional Tags
: http://codex.wordpress.org/Conditional_Tags
- Photoshop+Camera Raw風(fēng)光、人文、城市、星空攝影后期技法(全彩)
- UG NX10.0從新手到高手
- AutoCAD 2017實(shí)用教程
- Painter繪畫(huà)實(shí)例教程
- Moldflow 2010完全自學(xué)與速查手冊(cè)(模流分析·成本控制)
- PPT設(shè)計(jì)實(shí)用教程
- Autodesk Ecotect Analysis 2011綠色建筑分析應(yīng)用
- SPSS進(jìn)階分析與實(shí)務(wù)
- 中文版Maya 2022完全自學(xué)教程
- Flash CC動(dòng)畫(huà)制作案例教程
- Photoshop CS6標(biāo)準(zhǔn)教程(全視頻微課版)
- 詳解AutoCAD 2022室內(nèi)設(shè)計(jì)(第6版)
- 跨境電商:速賣(mài)通搜索排名規(guī)則解析與SEO技術(shù)
- Adobe創(chuàng)意大學(xué)Premiere Pro影視剪輯師標(biāo)準(zhǔn)實(shí)訓(xùn)教材(CS6修訂版)
- Oracle Application Express Forms Converter