A story about a little customization I did on WordPress

Hello,
I'm Mandai, the Wild Team member of the development team.

On Beyond Blog, articles are published after a process whereby they are double-checked by someone other than the author to ensure there are no technical or grammatical errors.
Until now, I'd communicate on Chatwork, saying things like, "I wrote this article, so please take a look," but I thought it was a hassle to report on Chatwork after each post, so I impulsively wrote a script that asks for a review when a post is waiting to be reviewed.

 

What did you do?

After you finish writing a blog post, we added a function that changes the status from "draft" to "waiting for review" and posts a request for review via the Chatwork API

Here's what I'm doing programmatically:

  • Checking the transition status of a blog post
  • Collect information about the article and the author
  • POST to Chatwork API

That's all

And here is the finished sauce

/** * Post 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 = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; $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 has been added[/title]\n"; $message.= "[To:$cw_to] Blog Uncle\n"; $message.= $blog_author. " is waiting for your blog review!\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, CURLOPT_HTTPHEADER, array('X-ChatWorkToken: '. $cw_api_key)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 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 achieve this, I needed to know a little about the functions provided by WordPress, so I would like to explain that here

 

get_the_author_meta() function

This function retrieves information about the author.
The third argument of the function, $post, contains the author_id, but the author's name is not known.

Just looking at the ID won't tell you who it is, so here's a function to get various information from author_id

Set the key of the information you want as the first argument of the function.
There are many, but I found a site that compiles all the keys, so I'll include a link here.

How to extract 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 determine when it will be executed.
The add_action() function is the function that registers a function to that hook.

The function registered by the add_action() function is executed when the do_action() function is executed with the same value set for the first argument of the pair

That said, the do_action() function is installed in various places, allowing you to hook processing at various times

The action we hooked this time, "transition_post_status", is called when the post status changes.
However, there are actually many post statuses, and we know the combination of actions before and after the change, so we set it to process only when the post status changes from draft to waiting for review.

 

summary

WordPress is constantly adding new features and getting bigger, so it can feel a bit intimidating, but if you do some research you'll quickly find some information, so I think you can customize it without worrying too much.
And it's surprisingly easy to customize!

It's no wonder that so many people use it

It turned out really well and I felt good about it, so I ended up writing this article on impulse

 
That's all

If you found this article useful, please click [Like]!
0
Loading...
0 votes, average: 0.00 / 10
883
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but I'm also grateful to be able to do a variety of other work, including marketing.
My portrait rights within Beyond are CC0.