Skip Navigation

[Closed] Allow users to input a set number of posts via User Form

This support ticket is created 4 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1880383

Tell us what you are trying to do?

I am looking to build a toolset form, to allow users to input a custom post via the front end. However, I want to be able to control how many posts the user can input. Ideally, I would like to be able to input a number into the users profile (so if they have manually paid me for 1 credit, then they can input 1 post, if they have paid me for 2 then I can manually input 2), then once they have input a post, that number gets automatically subtracted by 1 (or if I have to manually change that number then so be it).
Then if a user tries to input a post, but they have 0 'credits', then they do not have the ability to input any more posts (and maybe they see a message telling them 'they need to purchase more credits'.
Is this possible with Toolset User Forms please?

Is there any documentation that you are following? I looked through the forum but I couldn't find anything

Is there a similar example that we can see?

What is the link to your site? Local site underdevelopment

#1881727
Screen Shot 2020-12-20 at 10.34.06 AM.png

Hello, it sounds like you need these features:
1. The ability to store a number value in each User's profile, representing the number of post "credits".
2. The ability to test whether or not a User has any remaining credits, and allow them to create a new post using Forms based on whether or not they have any remaining credits
3. The ability for Users to create posts from the front-end of the site using Forms
4. The ability to automatically subtract one "credit" each time the User posts a new post with Forms

Features 1, 2, and 3 are possible in Toolset without any extra custom coding, but Feature #4 requires a small custom code snippet I can help you implement.

First, you must create a number custom field in User Profiles. For example, I would call this field something like "credits". You can create this field in Toolset > Custom Fields > User Fields tab. You should probably set a default value of 0 here, so new Users have no credits.

Next, you need a new post Form that allows your Users to submit posts from the front-end of the site. You'll create that Form in Toolset > Post Forms.

Once the Form has been created, you need to display it to some Users conditionally, based on the value of that User's "credits" field. You can use Toolset's conditional block to create a condition that compares the current logged-in User's "credits" field to see if it is greater than or equal to a static value of 1. Since it is not possible to select User fields in the conditional builder yet, you must use the Advanced Editor to edit the conditional terms.
The correct conditional syntax looks like this:

  ( ( '[types usermeta="credits" output="raw" current_user="true"][/types]' gte '1' ) ) 

See the screenshot here. You would replace credits with the slug of your custom number field, if it is not "credits".

Then you will drag a Form block into that conditional block, so that the Form is only displayed if the condition is true. If the field is not greater than or equal to 1, no Form will be displayed.
https://toolset.com/course-lesson/using-toolset-conditional-block/

Finally, you need to implement a custom code snippet to automatically decrement the custom field value whenever this Form is submitted successfully. We have a PHP API cred_save_data that can be used for this purpose. You can create a new snippet in Toolset > Settings > Custom Code, or add it to your child theme's functions.php file. Here is an example you can customize for your site:

// automatically subtract 1 from the current user's credits field
// when a new post form is submitted
// https://toolset.com/forums/topic/allow-users-to-input-a-set-number-of-posts-via-user-form/
add_action('cred_save_data', 'tssupp_decrement_credits',10,2);
function tssupp_decrement_credits($post_id, $form_data) {

  // edit these values as needed
  $slug = 'credits';
  $forms = array( 123 );

  // do not edit below this line
  if ( in_array( $form_data['id'], $forms ) )
  {
    $user = wp_get_current_user();
    $credits = get_user_meta( $user->ID, 'wpcf-' . $slug, true );
    if( $credits >= 1 )
    {
      $credits = $credits-1;
      update_user_meta( $user->ID, 'wpcf-' . $slug, $credits );
    }
  }
}

Where the code says "edit these values as needed", you should replace credits with the slug of the custom field you created for the User Profile. You should also replace 123 with the numeric ID of the new post Form which your Users will use to create new posts. If there are multiple new post Forms, you can supply a comma-separated list of numeric Form IDs like this:

$forms = array( 123, 456, 789 );

One thing you should be aware of is if you create an Edit User Form, or a New User Form, you will probably NOT want to include the credits field in that Form, which would allow the User to manually modify their number of credits. You probably want to manage that only in wp-admin instead.

Let me know if you have questions about this, or if I've misunderstood what you want to accomplish.

#1887873

Hi Christian,

Thank you SO MUCH for such a detailed and helpful response - I really REALLY appreciate it 🙂
I'm going to give this a try on a development site and see if I can get this to work as described.

Thanks again for your excellent support,

Keith

#1888757

No problem, I'll stand by for your update.

The topic ‘[Closed] Allow users to input a set number of posts via User Form’ is closed to new replies.