Created
January 14, 2017 23:58
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a purely illustrative example! Notes:
[post_to_bbp]
should be used wherever the form needs to be generatedIt 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.