Please or Register to create posts and topics.

Create Posts thru API Integration

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.

Pino Strano has reacted to this post.
Pino Strano

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');

 

Jakerome has reacted to this post.
Jakerome
Need professional help with Asgaros Forum? Book an appointment with us at domra Web Solutions for setup and customization services. Learn more about our Toolbox for Asgaros Forum plugin to enhance your forum experience.

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.

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'] = '';

 

Jakerome and Pino Strano have reacted to this post.
JakeromePino Strano
Need professional help with Asgaros Forum? Book an appointment with us at domra Web Solutions for setup and customization services. Learn more about our Toolbox for Asgaros Forum plugin to enhance your forum experience.

Thank you @qualmy91! You’ve been really helpful! I’ll check this out.

Asgaros and qualmy91 have reacted to this post.
Asgarosqualmy91

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?

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.

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?