Notifications
Clear all

wpForo 1.x.x [Solved] Is there any way to add new posts to the forum programatically?

5 Posts
4 Users
4 Reactions
1,999 Views
WPForoNoob
Posts: 32
Topic starter
(@wpforonoob)
Trusted Member
Joined: 7 years ago

Hi,

I was wondering if WPForo has some functions similar to `wp_update_post()` and `wp_insert_post()` which can be used to add forum topics programatically.

Thanks.

4 Replies
Sofy
Posts: 4900
 Sofy
Admin
(@sofy)
Support Team
Joined: 7 years ago

Hi @wpforonoob,

Below is provided the codes that add a topics and posts in the forum. You just need to change red marked codes before using:

$topic = array(
   'userid' => USERID,
   'title' => TITLE,
   'body' => BODY,
   'forumid' => FORUMID,
   'views'   => VIEWS,
   'type'  =>  TYPE,
   'created' => CREATED
);
WPF()->topic->add($topic);

$reply = array(
   'userid' => USERID,
   'title' => TITLE,
   'body' => BODY,
   'topicid' => TOPICID,
   'created' => CREATED
);
WPF()->post->add($reply);

The arrays below contain only required arguments. If you need to insert additional arguments just get the fields name from the following tables:

  • wp_wpforo_posts
  • wp_wpforo_topics

the wp_ is just a prefix. In your case it can differ. 

If you open the topic and post classes you'll find all methods you want to use. 

1 Reply
WPForoNoob
(@wpforonoob)
Joined: 7 years ago

Trusted Member
Posts: 32

Thanks @sofy 🙂

WPForo is awesome.

 

Posts: 22
 msr
(@msr)
Eminent Member
Joined: 5 years ago

@wpforonoob @tward @sofy

Did anyone manage to do this from a cron job or similar code not already logged in? I tried adding the following code, and although it seems to login the user to wordpress, the topic is not made. I am assuming there is some login issue. How to properly login to wpForo?

require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
$user_login = 'admin'; 
$user = get_userdatabylogin($user_login);
$user_id = $user->ID; 
wp_set_current_user($user_id, $user_login);
wp_set_auth_cookie($user_id); 
do_action('wp_login', $user_login);

1 Reply
Robert
Admin
(@robert)
Joined: 9 years ago

Support Team
Posts: 10591

@msr

Don't search solution to login to wpForo, just search solution for login in WordPress. wpForo authorization is based on WordPress functions, there is no wpForo way to login, once you WordPress you'll be login in wpForo too. So just open a support topic in WordPress support forum or search in WordPress login functions:

https://cleverplugins.com/autologin-wordpress-php-script/