A story about a little customization I did on WordPress

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

On this Beyond blog, articles undergo a double-check process by someone other than the author to ensure there are no technical or grammatical errors before publication.
Previously, we would exchange messages on ChatWork like, "I've written this article, please take a look," but I thought it was a hassle to report via ChatWork every time after writing, so I impulsively wrote a script to ask for checks when an article is awaiting review.

 

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 author_id is contained in the third argument of the function, $post, but the crucial name is not available.

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

The first argument of the function is set to the key of the information you want.
There are many keys, but I found a site that lists all of them, so I'll include a link.

How to extract various data of users registered in WordPress

 

add_action() function

Even if you create a function, it won't run unless you set up a hook to specify when it should execute.
The `add_action()` function is used to register 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 "transition_post_status" action that we hooked this time is called when the status of an article changes.
However, there are actually many article statuses, and we know the combination of actions before and after the change, so we only process it when the status changes from draft to awaiting review.

 

summary

WordPress is constantly adding new features and growing in scale, which can make it seem daunting at first, but if you search online, you can easily find some information, so I think you can customize it without feeling intimidated.
And surprisingly, customization is easy!

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 helpful,please give it a "Like"!
0
Loading...
0 votes, average: 0.00 / 10
909
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 thankfully I'm also given the opportunity to work on various other tasks, including marketing.
My image rights within Beyond are treated as CC0.