我有一个使用bitly API创建短链接的插件,但有一个问题!
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 );
它为每个帖子创建2个二进制链接,其中一个是
/?p=12345
另一个是
/post-name
我安排了所有的帖子,所以我注意到
?p=12345
在自动保存、预览和计划时创建
/post-name
发布帖子后创建。。。不幸的是
?p=12345
设置为默认短url;
?p=12345
=
bit.ly/sdfssdfd
另一个被忽略了。。
我应该如何强制它只创建/post-name
并将其设置为默认值,而不是另一个?!
我试过这个:
$mypost = get_page( $id );
if ( !in_array($mypost->post_status, array(\'future\', \'publish\')) ) {
return "Post must be published to get a shortlink";
}
但正如所指出的,必须发布帖子才能获得短链接(它会创建1个链接,但
/?p=12345
在bitly网站上,但在wordpress上看不到)。。。我想我需要的不是发布,而是在计划完成后创建url。。
所以我试着改变
$mypost = get_page( $id );
if ( !in_array($mypost->post_status, array(\'schedule\', \'future\')) ) {
return "Post must be published to get a shortlink";
}
但正如你所想象的那样,它不起作用。。。
你知道怎么解决吗?
谢谢