我想使用一个从高级URL Shortener脚本的API生成短URL的短代码。
我将API中的代码添加到模板函数中:
// Main Function
function shorten_url($url){
$siteurl="http://go.mysite.com"; // FOR Example http://gempixel.com/short
$apikey="8jkyA3e7xs1J"; // You can get it from the user account
if($apikey && $siteurl){
$short=@file_get_contents("$siteurl/api?api=$apikey&url=".strip_tags(trim($url)));
$short=json_decode($short,TRUE);
if(!$short["error"]){
return $short["short"];
}
}
}
// Shortcode function
function shortcode_shorten_url($atts,$content){
return shorten_url($content);
}
// Register Shortcode
add_shortcode("shorten", "shortcode_shorten_url");
接下来,我想检查它是否有效,所以我在模板中添加了单个代码:<?php echo do_shortcode(\'[shorten]http://mylink.com[/shorten]\'); ?>
它起作用了,我看到了一个简短的链接。如何在其他有外部链接的地方使用由模板生成的短链接(functions.php
), 像下面这样?
<?php $numerado = 1; { while( have_rows(\'voo\') ): the_row(); ?>
<li class="elemento">
<a href="**I WANT TO PUT SHORT LINK HERE INSTEAD NORMAL LINK FROM op1**<?php echo get_sub_field(\'op1\'); ?>" rel=\'nofollow\' target="_blank" class="prawyklik">
<span class="a"><b class="icon-controller-play play"></b> <?php _e(\'Option\',\'mundothemes\'); ?> <?php echo $numerado; ?></span>
<span class="b">
<img src="http://www.google.com/s2/favicons?domain=<?php echo get_sub_field(\'op2\'); ?>" alt="<?php echo get_sub_field(\'op2\'); ?>">
<?php echo get_sub_field(\'op2\'); ?>
</span>
<span class="c"><?php echo get_sub_field(\'op3\'); ?></span>
<span class="d"><?php echo get_sub_field(\'op4\'); ?></span>
</a>
</li>
<?php $numerado++; ?>
<?php endwhile; } ?>