Skip to content

Instantly share code, notes, and snippets.

@barryhughes
Created January 14, 2017 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barryhughes/97b010b540169fa74370e136398c6fa3 to your computer and use it in GitHub Desktop.
Save barryhughes/97b010b540169fa74370e136398c6fa3 to your computer and use it in GitHub Desktop.
Rudimentary example of taking form data and using it to create a (bbPress) forum topic
<?php
function post_to_bbp_shortcode() {
if ( 'success' === post_to_bbp_status() ) {
return post_to_bbp_success();
}
else {
return post_to_bbp_form();
}
}
function post_to_bbp_form() {
$check = wp_create_nonce( 'post_to_bbp' );
return "
<form method='post'>
<input name='post_to_bbp_msg' />
<input type='submit' value='Send!' />
<input type='hidden' name='post_to_bbp_check' value='$check' />
</form>
";
}
function post_to_bbp_success() {
return 'Thank you for your submission.';
}
function post_to_bbp_listener() {
if ( ! isset( $_POST['post_to_bbp_check'] ) || ! isset( $_POST['post_to_bbp_msg'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['post_to_bbp_check'], 'post_to_bbp' ) ) {
return;
}
bbp_insert_topic( array(
'post_parent' => 4, // This needs to be a valid forum ID!
'post_title' => 'Result of submission dated ' . date_i18n( 'Y-m-d H:i:s' ),
'post_content' => filter_var( $_POST['post_to_bbp_msg'], FILTER_SANITIZE_STRING )
) );
post_to_bbp_status( 'success' );
}
function post_to_bbp_status( $value = null ) {
static $status;
if ( null === $value ) {
return $status;
}
else {
$status = $value;
}
}
add_action( 'init', 'post_to_bbp_listener' );
add_shortcode( 'post_to_bbp', 'post_to_bbp_shortcode' );
@barryhughes
Copy link
Author

barryhughes commented Jan 15, 2017

This is a purely illustrative example! Notes:

  • The shortcode [post_to_bbp] should be used wherever the form needs to be generated
  • It doesn't take care of sanitizing the input or guard against repeat submissions

It really just shows a basic approach to creating a custom form that generates forum topics; in general the [bbp-topic-form] shortcode that ships with bbPress may well be a better bet/starting point for your own customizations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment