I have a buddy press site with cube points installed. This is a two part question/request.
1. I need to add points to a user's account when they submit a form.
2. I have 5 different forms and each one needs to give the user a different amount of points.
I am using contact 7 forms. I don't have much php experience, but have been able to customize quite a bit of the site on my own.
Thanks in advance for the help!
Gabriel Merovingi answers:
Hi!
I have made you a custom Cubepoints module that will add points to users when they successfully submit one of your forms.
<strong>Installation</strong>:
1. Copy and Paste the code bellow into a new file and save it as "points_for_cf7form_submissions.php".
2. Upload the file to wp-content/plugins/cubepoints/modules/
3. In WordPress find you way to CubePoints Menu > Modules.
4. Locate the module "Points for Submitting Forms" and click "Activate".
5. Go to CubePoints > Configure.
6. Set how many points you want to award for each successful form submission.
7. Save.
<strong>Minimum Requirements</strong>:
- CubePoints version 3
- Contact Form 7 version 3.2
- Both plugins are activated
<strong>Demo</strong>:
http://temp.merovingi.com/
Username: guest
Password: guest
There are two forms on the front page that you can fill out and try. The top form gives 3000 points and the second form gives 1000.
<strong>Code</strong>:
<?php
/*
Custom CubePoints Module
Points for Publishing of Multiple Custom Post Type Module
Version: 1.0
*/
define( 'CP_MOD_CF7_POST_TYPE', 'wpcf7_contact_form' );
// Register our Module
cp_module_register(
__( 'Points for Submitting Forms', 'cp' ),
'formsubmissionpoints',
'1.0',
'dbm',
'http://www.merovingi.com',
'http://www.merovingi.com',
__( 'This module awards points when Contact Form 7 forms are submitted successfully.', 'cp' ), 1 );
// If our module is activated AND if Contact Form 7 is installed and active
if ( cp_module_activated( 'formsubmissionpoints' ) && function_exists( 'wpcf7' ) ) {
/** Module Configuration */
add_action( 'cp_config_form', 'cp_form_submissions_admin_setting' );
function cp_form_submissions_admin_setting() {
// Look for all forms
$cforms7_search_args = array(
'post_type' => CP_MOD_CF7_POST_TYPE,
'post_status' => 'any',
'posts_per_page' => -1,
'offset' => 0,
'orderby' => 'ID',
'order' => 'ASC' );
// Get points settings
$cf7_form_points = array();
$cf7_form_points = get_option( 'cb_form_submission_points' );
// Header
echo '
<br />
<h3>Contact Form 7 Submissions</h3>
<table class="form-table">
<tr valign="top">
<th scope="row"><label>' . __( 'Form ID' ) . '</label></th>
<td valign="middle">' . __( 'Points for successful form submissions' ) . '</td>
</tr>';
// Our Contact Forms 7 Query
$cform7_query = new WP_Query( $cforms7_search_args );
// Found forms
if ( $cform7_query->have_posts() ) :
while ( $cform7_query->have_posts() ) : $cform7_query->the_post();
$cform7_id = get_the_ID();
echo '
<tr valign="top">
<th scope="row">
<label for="cform7-ID">' . get_the_title() . '</label>
<input type="hidden" name="cform7[]" value="' . $cform7_id . '" />
</th>
<td valign="middle">
<input type="text" name="cform7-' . $cform7_id . '-points" id="cf7p-' . $cform7_id . '" value="' . $cf7_form_points[$cform7_id] . '" />
</td>
</tr>';
endwhile;
// If there are no forms
else :
echo '
<tr valign="top">
<th scope="row"><label>' . __( 'Error' ) . '</label></th>
<td valign="middle"><strong>' . __( 'No Contact Form 7 forms found' ) . '</strong></td>
</tr>';
endif;
// Reset our custom query
wp_reset_postdata();
// Footer
echo '
</table>';
}
/** Save Module Congif */
add_action( 'cp_config_process', 'cp_form_submissions_save' );
function cp_form_submissions_save() {
$cform7_settings = array();
// Run though all forms and collect the points value
foreach ( $_POST['cform7'] as $form_id ) {
$cform7_settings[$form_id] = $_POST['cform7-' . $form_id . '-points'];
}
// Remove old points
delete_option( 'cb_form_submission_points' );
// Save new points
update_option( 'cb_form_submission_points', $cform7_settings );
}
/** Hook into Contact Form 7 action hook for successful submissions */
add_action( 'wpcf7_mail_sent', 'cb_form_submission_hook', 5, 1 );
function cb_form_submission_hook( $cf7_form ) {
// Grab our points settings
$cf7_points_settings = array();
$cf7_points_settings = get_option( 'cb_form_submission_points' );
// Prep current user
$cf7_cp_current_user = wp_get_current_user();
// Run though all settings
foreach ( $cf7_points_settings as $cf7_form_id => $cf7_form_points ) {
// If we find the form add points accordingly
if ( $cf7_form->id == $cf7_form_id ) cp_points( 'formsubmit', $cf7_cp_current_user->ID, $cf7_form_points, $cf7_form->id );
}
return $cf7_form;
}
/** Adjust the Log. Overwrite custom post type entries. */
add_action( 'cp_logs_description', 'cp_admin_logs_desc_form7s', 10, 4 );
function cp_admin_logs_desc_form7s( $type, $uid, $points, $data ) {
if ( $type != 'formsubmit' ) return;
// Grab the form details
$the_cf7_form = get_post( $data );
_e( 'Points for "' . $the_cf7_form->post_title . '" form submission.' );
}
}
?>
tfullen comments:
Thanks so much Gabriel. I'll test this out and get back to you.
Gabriel Merovingi comments:
Edit: Adjusted demo address.
tfullen comments:
Gabriel, thank you so much! I implemented the module and it worked flawlessly!
Please let me know how I get you paid and if I can "vote" for you or do anything else to help your status here.
Thanks again,
Tony
Gabriel Merovingi comments:
Glad it worked. You can just vote my answer as the winning one and WP Questions will pay me :) You can always contact me by sending me a message here or by email.
Arnav Joy answers:
can you show the form
tfullen comments:
[[LINK href="http://theglobalgamechangers.com/share-your-story-2/"]][[/LINK]]
The site's not live yet, but will be by tonight.
tfullen comments:
http://theglobalgamechangers.com/share-your-story-2/
Sorry, it appears the link didn't get included...
Arnav Joy comments:
the form can be filled without login/registration , so how to add points
can you explain full flow as how to include points
tfullen comments:
That's correct. Is there an easy fix to this? I would like to make them only accessible when logged in, but haven't found a solution for that. Is there a plug in or a bit of code that I can put somewhere? Please forgive my lack of knowledge on the programming...
Ultimately, the flow should be... A user signs up for an account and then has access to all the forms.
Thanks for the quick response! I'm under the gun and have a ton of design revisions to make. If you can help me with some of the back end work, that would be fantastic!
tfullen comments:
I found a plug in to restrict certain pages, so that's taken care of.
Can you help with the original question?
Arnav Joy comments:
for this work , I have to work in your files..
so can you please provide me access to the site.
my email is: [email protected]
skype : arnav.joy
also can you tell me where are the other forms and how much points to add for each form.
tfullen comments:
I am backing up the site just in case we have any issues, but in doing so, I realized that none of my custom fields are showing up in the admin database. Do you know of a plug in that will let me view all the data being collected in the wordpress dashboard?
I'll probably want to skype with you before giving you access to the files, just as a precaution.
Thanks.