I want to use a shortcode that generates a short URL from API of Premium URL Shortener script.
I added code from API to my template functions:
// 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");
Next I wanted to check if it works, so I added in template single code:
<?php echo do_shortcode('[shorten]http://mylink.com[/shorten]'); ?>
It works, I see a short link.
How can I use that short link in other places where I have external links, generated by template (functions.php
), like below?
<?php $numerado = 1; { while( have_rows('voo') ): the_row(); ?>
<li class="elemento">
<a href="https://wordpress.stackexchange.com/questions/217725/**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="https://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; } ?>