2

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="**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 1

0

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="<?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; } ?>
4
  • Ok, but how can I get this short link instead of full link from ACF 'op1' in place of 'op1' ?
    – Mateo
    Feb 15, 2016 at 18:19
  • Your question is really surprisingly basic. Please, no offense, but it suprising for me that you have been able to code a shortcode but don't know how to replace one echo function_name() with echo other_function_name(). That is all what I've explained in the answer. I have updated it.
    – cybmeta
    Feb 15, 2016 at 18:21
  • Sorry, my English is very bad I misunderstood what you wrote, but it works just perfect, thank you so much, for help, you solved my problem :)
    – Mateo
    Feb 15, 2016 at 18:40
  • Don't worry, my english is also not very good, maybe I wrote something inaccurately.
    – cybmeta
    Feb 15, 2016 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.