Created: July 12th, 2010
How to make a simple demo/download wordpress shortcode
Use the power of wordpress shortcode and post thumbnail, with some CSS3’s magic, today we will create an kick-ass demo/download box (As you can see at VNWebtalk)
Post thumnail:
First you need to confirm that your theme support post thumbnail. If it doesn’t. Follow these simple steps:
Open functions.php in your themes folder (If it doesn’t have, create one).
PHP:
<?php add_theme_support( 'post-thumbnails' ); //This is my size, you can set it to whatever you want. set_post_thumbnail_size( 580, 193, true ); ?>
Now go to Admin -> Posts to confirm that wordpress added the post thumbnail feature.
Demo/Download shortcode:
If you don’t know what is WordPress shortcode and/or how to use it. You should check there awesome article:
- WordPress Shortcode API
- How to use, style and implement WordPress shortcodes — TutToaster
- Mastering WordPress Shortcodes — Smashing Magazine
Now, we want to create a shortcode like this
[demodownload demo="demo-link" download="download-link"]content[/demodownload]
Copy/paste in to your functions.php file:
PHP:
<?php
//Demo/Download Shortcode
function demodownload($atts, $content = '') {
global $post;
$id = $post->ID;
//Get our variable
extract(shortcode_atts(array(
"demo" => '',
"download" => ''
), $atts));
//If we have content, show it, otherwise display default text
if ($content != '') {
$content = $content;
}
else{
$content = "View it online";
}
?>
<div class="postthumbholder">
<div class="postthumb">
<?php echo get_the_post_thumbnail($id); ?>
<div class="downloadholder">
<a href="<?php echo $demo; ?>" class="demo">Demo<span><?php echo $content; ?></span></a>
<a href="<?php echo $download; ?>" class="download">Download<span>Source files</span></a>
</div>
</div>
</div>
<?php
}
add_shortcode('demodownload', 'demodownload');
?>
Some CSS’s magic
.postthumbholder {
border-top:1px solid #FFFFFF;
display:block;
margin-bottom:15px;
padding-top:15px;
position:relative;
width: 600px;
}
.downloadholder {
-moz-border-radius:5px 5px 5px 5px;
-moz-box-shadow:0 1px 5px rgba(0, 0, 0, 0.4) inset, 0 1px 0 rgba(255, 255, 255, 0.9), 0 -1px 0 rgba(0, 0, 0, 0.5);
background-color:rgba(0, 72, 110, 0.7);
bottom:20px;
left:20px;
padding:10px 0;
position:absolute;
width:560px;
}
.downloadholder a:link, .downloadholder a:visited {
color:#FFFFFF;
display:inline;
float:left;
font-size:20px;
margin-left:70px;
padding:10px 0;
text-decoration:none;
text-shadow:0 1px 2px #0F2F3F;
text-transform:uppercase;
width:208px;
}
.downloadholder a:hover, .downloadholder a:active {
text-shadow:0 -1px 1px #447eb1;
}
Done! Hope you like this.
Cái này cải tiến chút thì hay nhỉ, ví dụ như chính link này mà click vào demo là tiêu, vì ko có demo. Lẽ ra nên có lựa chọn là chỉ xuất hiện 1 nút thôi (ko có demo hoặc ko có download), hoặc là kèm thông báo: Demo Not Provided, Demo Not Yet Available, tức là tùy biến thêm thông báo (khác với thông báo mặc định) khi chưa có link
[demodownload demo=“demo-link or demo-announcement” download=“download-link or download-announcement”]content[/demodownload]
Cái link demo ghõ sai đường dẫn :”>
Bài này chủ yếu viết simple thôi. Chứ code hiện tại chạy trên trang có khác 1 chút, nhưng rườm rà quá nên sợ rối.
Thật ra cũng không khó lắm, chỉ cần check 2 biến $demo và $dowload, nếu rỗng thì không out gì ra hoặc out thông báo ra.
Để vài hôm sẽ viết luôn bài update, có 1 số thành phần khác cần thiết như hiện số lần download, etc…
ok, mà viết cái bài update thành bài riêng thì hay, cứ để bài này đơn giản thế này cho đỡ rối