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="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; } ?>
1 Answer
You never need do_shortcode()
, better said, almost never, there are a few cases where do_shortcode()
could be appropiate.
Note how you could do just:
echo shorten_url( 'http://mylink.com ' );
instead of:
echo do_shortcode('[shorten]http://mylink.com[/shorten]');
Think about shortcodes as PHP functions placeholders; they are intended to be use where you can not execute PHP directly, like the content of post. But if you can execute PHP directly, like in a PHP script, there is no reason to use shortcodes, which is the placeholder, you can use the PHP function directly. There are a lot of benefits, meanly a better performance because do_shortcode()
performs some preg regex that you don’t neet at all.
So, you can use the shortlink everywehere you need. For example:
<?php $numerado = 1; { while( have_rows('voo') ): the_row(); ?>
<li class="elemento">
<a href="https://wordpress.stackexchange.com/questions/217725/<?php echo shorten_url( 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; } ?>