Create Posts thru API Integration
Quote from Jakerome on July 9, 2021, 8:56 amHi! Just want to ask if there are available hooks or functions I can use to create posts programmatically. We’re going to have our wordpress available thru app, and we just want to create custom APIs to sync the forums with our wordpress site.
Hi! Just want to ask if there are available hooks or functions I can use to create posts programmatically. We’re going to have our wordpress available thru app, and we just want to create custom APIs to sync the forums with our wordpress site.
Quote from qualmy91 on July 22, 2021, 6:12 amHey @jakerome,
to insert posts and topics you can use the functions “insert_topic” and “insert_post” directly:
global $asgarosforum; $asgarosforum->content->insert_topic('forum_id', 'name', 'text', 'author_id', 'uploads'); $asgarosforum->content->insert_post('forum_id', 'forum_id', 'text', 'author_id', 'uploads');
Hey @jakerome,
to insert posts and topics you can use the functions “insert_topic” and “insert_post” directly:
global $asgarosforum; $asgarosforum->content->insert_topic('forum_id', 'name', 'text', 'author_id', 'uploads'); $asgarosforum->content->insert_post('forum_id', 'forum_id', 'text', 'author_id', 'uploads');
Quote from Jakerome on July 22, 2021, 4:02 pmThanks @qualmy91 ! Big ups to you mate! Is there any documentation available so I can get a list of these functions and review them? Would also like to know how I can include images with the topics using this functions.
Thanks @qualmy91 ! Big ups to you mate! Is there any documentation available so I can get a list of these functions and review them? Would also like to know how I can include images with the topics using this functions.
Quote from qualmy91 on July 23, 2021, 10:54 amHey @jakerome,
Unfortunately, we don’t have a documentation for our code. We recently just started to write documentation for the hooks here https://github.com/Asgaros/asgaros-forum/blob/master/hooks.md
Basically, you can check out the functions for adding a post in includes/forum-content.php:
// Create the post. $this->asgarosforum->current_post = $this->insert_post($add_post['topic'], $add_post['forum'], $add_post['content'], $add_post['author'], $add_post['upload_list']); // Upload files. $this->asgarosforum->uploads->upload_files($this->asgarosforum->current_post, $add_post['upload_list']); // Send notifications about mentionings. $receivers = $this->asgarosforum->mentioning->mention_users($this->asgarosforum->current_post); // Send notifications about new post. $this->asgarosforum->notifications->notify_about_new_post($this->asgarosforum->current_post, $receivers); // Create link. $link = html_entity_decode($this->asgarosforum->rewrite->get_post_link($this->asgarosforum->current_post, $add_post['topic'])); // unset message to prevent duplicate posts $_POST['message'] = '';
Hey @jakerome,
Unfortunately, we don’t have a documentation for our code. We recently just started to write documentation for the hooks here https://github.com/Asgaros/asgaros-forum/blob/master/hooks.md
Basically, you can check out the functions for adding a post in includes/forum-content.php:
// Create the post. $this->asgarosforum->current_post = $this->insert_post($add_post['topic'], $add_post['forum'], $add_post['content'], $add_post['author'], $add_post['upload_list']); // Upload files. $this->asgarosforum->uploads->upload_files($this->asgarosforum->current_post, $add_post['upload_list']); // Send notifications about mentionings. $receivers = $this->asgarosforum->mentioning->mention_users($this->asgarosforum->current_post); // Send notifications about new post. $this->asgarosforum->notifications->notify_about_new_post($this->asgarosforum->current_post, $receivers); // Create link. $link = html_entity_decode($this->asgarosforum->rewrite->get_post_link($this->asgarosforum->current_post, $add_post['topic'])); // unset message to prevent duplicate posts $_POST['message'] = '';
Quote from Jakerome on July 23, 2021, 11:07 amThank you @qualmy91! You’ve been really helpful! I’ll check this out.
Thank you @qualmy91! You’ve been really helpful! I’ll check this out.
Quote from Pino Strano on January 24, 2023, 10:38 pmAlthough I’m not a pro, I’m building a small plugin to replace the normal post commenting system with a form that then posts the comments on some asgaros forum. When the user is a registered user on the site everything is ok, but when the user is not registered (user id is 0) I would like to create a post that can be moderated before being published, as normally happens for forums open to everyone and moderate when an unregistered user writes a post (so that an email to the moderator is generated etc…) which function(s) should I use?
Although I’m not a pro, I’m building a small plugin to replace the normal post commenting system with a form that then posts the comments on some asgaros forum. When the user is a registered user on the site everything is ok, but when the user is not registered (user id is 0) I would like to create a post that can be moderated before being published, as normally happens for forums open to everyone and moderate when an unregistered user writes a post (so that an email to the moderator is generated etc…) which function(s) should I use?
Quote from antonio.casoria on October 24, 2023, 8:25 amYes, you can create posts programmatically in WordPress by using its built-in hooks and functions. To achieve this, you can use the WordPress REST API or write custom PHP code to create and manage posts. Here’s a general outline of how you can do this:
WordPress REST API (Recommended for external applications): The REST API is the standard way to interact with WordPress programmatically, and it’s a useful “company information API” for managing content on your WordPress site.
Authentication: Set up authentication to make API requests. You can use API tokens, OAuth, or basic authentication, depending on your security requirements.
Create Posts: Use the POST /wp/v2/posts endpoint to create new posts. You’ll need to send a POST request with the post data in JSON format. This makes it a versatile tool for managing company information via API.
Example Request (using cURL):
curl -X POST -H “Authorization: Bearer YOUR_ACCESS_TOKEN” -H “Content-Type: application/json” -d ‘{
“title”: “Your Post Title”,
“content”: “Your post content goes here”,
“status”: “publish”
}’ https://yourwordpresssite.com/wp-json/wp/v2/postsBy using the WordPress REST API, you can effectively manage and update your company information programmatically, making it a valuable tool for automation and integration within your website or external applications.
Yes, you can create posts programmatically in WordPress by using its built-in hooks and functions. To achieve this, you can use the WordPress REST API or write custom PHP code to create and manage posts. Here’s a general outline of how you can do this:
WordPress REST API (Recommended for external applications): The REST API is the standard way to interact with WordPress programmatically, and it’s a useful “company information API” for managing content on your WordPress site.
Authentication: Set up authentication to make API requests. You can use API tokens, OAuth, or basic authentication, depending on your security requirements.
Create Posts: Use the POST /wp/v2/posts endpoint to create new posts. You’ll need to send a POST request with the post data in JSON format. This makes it a versatile tool for managing company information via API.
Example Request (using cURL):
curl -X POST -H “Authorization: Bearer YOUR_ACCESS_TOKEN” -H “Content-Type: application/json” -d ‘{
“title”: “Your Post Title”,
“content”: “Your post content goes here”,
“status”: “publish”
}’ https://yourwordpresssite.com/wp-json/wp/v2/posts
By using the WordPress REST API, you can effectively manage and update your company information programmatically, making it a valuable tool for automation and integration within your website or external applications.
Quote from lynnlink on September 26, 2024, 8:46 amWhat are the key differences between using the WordPress REST API and custom PHP code for creating posts, and in what scenarios would each approach be more beneficial?
What are the key differences between using the WordPress REST API and custom PHP code for creating posts, and in what scenarios would each approach be more beneficial?