2

I've this plugin to create shortlink using the bitly API, but there's a problem!

function yoast_bitly_shortlink($url, $id, $context, $allow_slugs) {
    if ( ( is_singular() && !is_preview() ) || $context == 'post' ) {
        $short = get_post_meta($id, '_yoast_bitlylink', true);
        if ( !$short || $short == '' ) {
            if ( !defined('BITLY_USERNAME') || !defined('BITLY_APIKEY') ) {
                $short = 'http://yoast.com/wordpress/bitly-shortlinks/configure-bitly/';
            } else {
                $url = get_permalink( $id );
                $req = 'http://api.bit.ly/v3/shorten?format=txt&longUrl='.$url.'&login='.BITLY_USERNAME.'&apiKey='.BITLY_APIKEY;
                if ( defined('BITLY_JMP') && BITLY_JMP )
                    $req .= '&domain=j.mp';
                $resp = wp_remote_get( $req );
                if ( !is_wp_error( $resp ) && is_array( $resp['response'] ) && 200 == $resp['response']['code'] ) {
                    $short = trim( $resp['body'] );
                    update_post_meta( $id, '_yoast_bitlylink', $short);
                }
            }
        }
        return $short;
    }
    return false;
}
add_filter( 'pre_get_shortlink', 'yoast_bitly_shortlink', 99, 4 );

function yoast_bitly_admin_bar_menu() {
    global $wp_admin_bar, $post;

    if ( !isset($post->ID) )
        return;

    $short = wp_get_shortlink( $post->ID, 'query' );

    if ( is_singular() && !is_preview() ) {
        if ( $short != 'http://yoast.com/wordpress/bitly-shortlinks/configure-bitly/' )
            $shortstats = $short.'+';

        // Remove the old shortlink menu, because it has some weird JS issues with admin bar when giving it submenu's.
        $wp_admin_bar->remove_menu('get-shortlink');
        $wp_admin_bar->add_menu( array( 'id' => 'shortlink', 'title' => __( 'Bit.ly' ), 'href' => 'javascript:prompt('Short Link:', ''.$short.''); return false;' ) );
        $wp_admin_bar->add_menu( array( 'parent' => 'shortlink', 'id' => 'yoast_bitly-link', 'title' => __( 'Bit.ly Link' ), 'href' => 'javascript:prompt('Short Link:', ''.$short.''); return false;' ) );
        $wp_admin_bar->add_menu( array( 'parent' => 'shortlink', 'id' => 'yoast_bitly-twitterlink', 'title' => __( 'Share on Twitter' ), 'href' => 'http://twitter.com/?status='.str_replace('+','%20', urlencode( $post->post_title.' - '.$short) ) ) );
        $wp_admin_bar->add_menu( array( 'parent' => 'shortlink', 'id' => 'yoast_bitly-stats', 'title' => __( 'Bit.ly Stats' ), 'href' => $shortstats, 'meta' => array('target' => '_blank') ) );
    }
}
add_action( 'admin_bar_menu', 'yoast_bitly_admin_bar_menu', 95 );

It creates 2 bitly links of every post one is the /?p=12345 and the other is /post-name I Schedule all my posts, so, I've noticed that ?p=12345 is created on auto saves, previews and when Scheduled and the /post-name is created once the post is published... And unfortunately ?p=12345 is set as default short url; ?p=12345 = bit.ly/sdfssdfd and the other one is ignored..

What should I do to force it to create only 1 link of /post-name and set it as default instead of the other one?!

I've tried this:

$mypost = get_page( $id );
  if ( !in_array($mypost->post_status, array('future', 'publish')) ) {
    return "Post must be published to get a shortlink";
  }

but as it's noted in return, post must be published to get a shortlink (it creates 1 link but the /?p=12345 in bitly website but it's not visible back on wordpress)... I guess I need, instead of published, the url to be created once Scheduled..

so I've tried to change

$mypost = get_page( $id );
  if ( !in_array($mypost->post_status, array('schedule', 'future')) ) {
    return "Post must be published to get a shortlink";
  }

but as you can imagine it isn't working...

do you have any idea how that can be fixed?

thanks!

1

I'm not sure if this is going to help or not, but it might help to know that some actions fire more than once. For example, save_post will actually fire three or four times on a given save. One is for the revision. One of the new post. Etc.

It could be, in your case, that you're experience something similar. Whatever it is is firing preform the post_name is assigned and then again after. Maybe?

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.