Tweet automatically from WordPress
Gianism has some useful functions. One of them is gianism_update_twitter_status()
. You can post tweet to twitter.
Fundamental
If you turn Gianism twitter integration on, connected account’s credentials are ready. This is different from other SNS. This means you can post tweet from connected account.
In short, let’s talk about Gianism.info. This site connected with twitter account @wpGianism
.
We release another plugin Chiramise! Try for it!https://t.co/Qth6pWfzLC pic.twitter.com/F4IjXabWpr
— Gianism (@wpGianism) November 6, 2016
So, WordPress can make tweet via twitter account, which represents your site’s identity.
Tweet when post published
At first, let’s assume that WordPress post a tweet when a post is published. In spite of your purpose, it’s important where to hook. In this case, transition_post_status
is that. It’s executed when post’s status changes. Code is like below:
<?php add_action( 'transition_post_status', function( $new_status, $old_status, $post ){ if ( ( 'publish' == $new_status ) && ( 'publish' != $old_status ) ) { $txt = 'New post is published! > ' . get_the_title( $post ) . ' ' . get_permalink( $post ); gianism_update_twitter_status( $txt ); } }, 10, 3 );
But Jetpack can do same thing without any code. Jetpack is better unless you need more customized tweet.
So, let’s challenge more generic example which goes over Jetpack’s simple static text. To celebrate author’s memorial publishing of 10 times, define function celebrate_author($post)
.
<?php function celebrate_author( $post = null ) { $post = get_post( $post ); $query = new WP_Query( [ 'post_type' => 'post', 'post_status' => 'publish', 'author' => $post->post_author, 'posts_per_page' => 1, ] ); if ( 10 == $query->found_posts ) { // this is 10th post return sprintf( 'Congrats! %1$s published memorial 10th post! %2$s', get_the_author_meta( 'display_name', $post->post_author ), get_permalink( $post ) ); } else { return ''; } }
Integrate this function to former hook example, you can go beyond Jetpack! More interactive tweet will please your users.
You should set more complicated condition
For brevity, we set condition as “New status IS publish and Former status IS NOT publish”. But on production environment, you should set more complicated conditions.
Let’s assume that you have made a site which anyone can post. If you set simple rule like that “if post status changes to publish, make a tweet”, some clever user notice that “Oh, this site make a tweet when post status changed!”. He/She might change same post’s status again and again.
To prevent such kind of hacks, you should set more complicated conditions. It’s wise enough to save the first published date and ignore second publishing.
Tweet automatically and periodically
WordPress has Cron. With this feature, periodical tweet is possible.
For example, cron which works every 1 hour is like below. You can refer more detailed Codex document.
<?php add_action( 'init', function() { // If event is not set, register it if ( ! wp_next_scheduled( 'my_daily_cron' ) ) { wp_schedule_event( current_time( 'timestamp', true ), 'hourly', 'my_daily_cron' ); } } ); // Register hook to the event above add_action( 'my_daily_cron', function() { // Do stuff } );
O.K. What should we tweet?
Do you like a twitter account which make a totally same tweet on every 1 hour, don’t you?
And because Twitter’s API restricts repetition of same tweet, your inactive twitter account may fail to post tweet.
Besides that, if you don’t require various tweet, 3rd party service satisfies.
Here we describe 2 examples.
Table Of Contents
- Fundamental
- Tweet when post published
- You should set more complicated condition
- Tweet automatically and periodically
- Case 1. tweet daily ranking via twitter
- Case 2. tweet with shifted interval
- Check your tweet's effect
- Integrate multiple SNS