在Wordpress中以编程方式分享帖子

时间:2014-10-29 08:14:06

标签: wordpress facebook-graph-api

我想与Facebook网站中的每个新帖子分享wordpress。这个任务需要哪个钩子?

我写了这段代码,但它不起作用=(

function on_all_status_transitions $new_status, $old_status, $post) {
    if ($new_status == 'publish') {
        $url = 'http://facebook.com/sharer.php?u=' . get_permalink($post->ID);
        wp_redirect($url);
        // header('Location: '. $url); -> it doesn't work too =(
    }
}
add_action('transition_post_status', 'on_all_status_transitions', 10, 3);

1 个答案:

答案 0 :(得分:0)

使用publish_post挂钩,只要发布帖子就会触发

function on_all_status_transitions( $ID, $post) {     
        $url = 'http://facebook.com/sharer.php?u=' . get_permalink($ID);
        wp_redirect($url);
        exit;           
}

add_action( 'publish_post', 'on_all_status_transitions', 10, 3 );

check this