A story about how I made a little customization of WordPress
Hello.
I'm Mandai, in charge of Wild on the development team.
This Beyond Blog goes through a process of double-checking by someone other than the person who wrote it to ensure there are no technical or grammatical issues before it is published.
Up until now, we had exchanges like "I wrote this article so check it out" on Chatwork, but I thought it would be a pain to report on Chatwork after writing each article, so I'm waiting for reviews. I wrote a script that I would like people to check when the time comes.
what did you do?
After finishing writing a blog article, I added a function that changes the status from ``Draft'' to ``Waiting for Review'' and posts a review request via Chatwork API.
What I am doing programmatically is as follows.
- Checking the transition state of blog articles
- Collect article information and author information
- POST to Chatwork API
That's all.
And this is the completed sauce.
/** * Post something awaiting review to Chatwork */ function announce_blog_status_in_review($new_status, $old_status, $post){ if ($old_status != 'pending' && $new_status == 'pending'){ $cw_room_id = 'XXXXXXXX'; $cw_api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'; $cw_to = 'xxxxxxxx'; $cw_target_url = 'https://api.chatwork.com/v2/rooms/'. $cw_room_id. '/messages'; $blog_title = wp_specialchars_decode ($post->post_title, ENT_QUOTES); $blog_author = wp_specialchars_decode(get_the_author_meta('last_name', $post->post_author), ENT_QUOTES); $message = "[info][title]Review received[/title]\ n"; $message.= "[To:$cw_to] Blog Uncle\n"; $message.= $blog_author. "is waiting for your blog to be reviewed!\n"; $message.= "Title: ". $blog_title. "\n"; $message.= "URL : $post->guid[/info]"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $cw_target_url); curl_setopt($ch curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, array('body' = > $message)); curl_exec($ch); } } add_action('transition_post_status', 'announce_blog_status_in_review', 10, 3);
To accomplish this, I needed to know a little about the functions provided by wordpress, so I would like to touch on that.
get_the_author_meta() function
Get the author's information from this function.
Author_id is included in $post, which is the third argument of the function, but I don't know the important name.
Even if you look at the ID, you won't know who it is, so here is a function to get various information from author_id.
Set the key of the desired information to the first argument of the function.
There are a lot of them, but I found a site that lists all the keys, so I'll post a link.
How to retrieve various data of users registered in WordPress
add_action() function
Even if you create a function, it won't be executed unless you set a hook to know when it will be executed.
The add_action() function is the function that registers the function to that hook.
The function registered by the add_action() function will be executed when the do_action() function with the same value set for the first argument of the pair is executed.
However, the do_action() function is installed in various places, and it is possible to hook processing at various times.
The action we hooked this time, "transition_post_status", will be called when the status of the article changes.
However, there are actually many statuses for an article, and you can see the combination of actions before and after the change, so I only process it when it goes from draft to pending review.
summary
WordPress is getting bigger and bigger with new features being added all the time, so it can be a bit difficult to get used to, but if you look into it you'll quickly find some information, so I think you can customize it without worrying.
And it's surprisingly easy to customize!
It's because it's used by so many people.
I was able to do it in a good way and felt good, so I decided to write this article with great momentum.
That's it.