Aug 04, 2023 4:49 am
I want to post the reply content to Chatwork, but I can't get the new content with this code.
New topics can be sent to Chatwork.
I would like your advice.
function reply_message( $topic_or_post_id, $args ) { if(!get_option('post_cw_api_token') || !get_option('post_cw_roomid')) return; // Get post information from wpForo if(current_filter() === 'wpforo_after_add_topic'){ $post = WPF()->topic->get_topic($topic_or_post_id); }else{ $post = WPF()->post->get_post($topic_or_post_id); } $author_id = $post['userid']; // adjust this if 'userid' is not the correct key $author_name = get_the_author_meta('display_name', $author_id); $topic = wpforo_topic($post['topicid']); $topic_url = $topic['url']; $expert_num = get_option('post_send_cwr_expert'); $send_title = esc_html( $post['title'] ); $send_content = isset($post['topicid']) ? wp_strip_all_tags(wpforo_topic($post['topicid'], 'body')) : ''; $send_content = mb_substr($send_content, 0, $expert_num); if (mb_strlen($send_content) >= $expert_num) { $send_content = mb_substr($send_content, 0, $expert_num - 3) . '...'; } $body = '[info][title]' . get_option('post_send_cwr_messege') . '[/title]投稿者:' . $author_name . "\n[hr]\n" . $send_title ."\n". $send_content . "\n" . $topic_url . '[/info]'; $roomid = get_option('post_cw_roomid'); $key = get_option('post_cw_api_token'); $url = 'https://api.chatwork.com/v2/rooms/'.$roomid.'/messages'; $data = array( 'body' => $body ); $headers = array( 'Content-Type: application/x-www-form-urlencoded', 'X-ChatWorkToken: '.$key ); $options = array('http' => array( 'method' => 'POST', 'content' => http_build_query($data), 'header' => implode("\r\n", $headers), )); $contents = file_get_contents($url, false, stream_context_create($options)); }
3 Replies
Aug 08, 2023 2:16 pm
Hi @gocchimama,
I think you should connect your function to an action hook not to a filter hook. The best hook to intercept new topic and post data is the wpforo_after_add_post hook. It sends three arguments:
do_action( 'wpforo_after_add_post', $post, $topic, $forum );
So you can get the $post['body'] as the topic (first post) or reply content.
Aug 15, 2023 5:14 am
Thanks!
I use this hook,
add_action( 'wpforo_after_add_post', 'reply_message', 10, 2 );
But,
wpdb::prepare was called incorrectly.The query expected only one placeholder, but sent an array of multiple placeholders.
I get an error message.