[Waiting for user confirmation] Restrict user to select one category only

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.

You are not logged in. You can view support threads, but not post.
If you are already logged in, please refresh your browser.

Related documentation: Front-end Content Submission and Editing Forms

This topic contains 25 replies, has 2 voices, and was last updated by  Christian Cox 9 minutes ago. The support staff assigned to this topic is Christian Cox.

Viewing 15 posts - 1 through 15 (of 26 total)
Author
Posts
#546377

I have a cred form to add new post. Is it possible to restrict the user so they can only select ONE category?

#546571

Christian Cox
Forum moderator

Supporter languages: English (English )

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

Hi, you can modify the category field in the CRED form editor. Change the display parameter to 'select' instead of 'checkbox', then add the 'single_select' parameter:

[cred_field field='category' display='select' single_select='true' output='bootstrap']

This will modify your CRED form to display a single select field for category.

#547052

That's great much more what I'm aiming for.

Is it possible to restrict which categories?

My situation is that I have 7 main categories and all have several sub-categories. This makes the list rather long, so I would love to be able to have a separate drop down box selector for each of the main categories

#547056

Christian Cox
Forum moderator

Supporter languages: English (English )

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

I see what you mean. It's possible to accomplish something like this, but not from the standard wp-admin area. It would require using several generic fields (one for each top-level category) and populating them with options for each subcategory. Then you would capture the selections made by your user and store them using the CRED API (cred_save_data hook). So it would require some additional PHP code to hook it all up. I have some more information about this process.

CRED generic fields:
https://wp-types.com/documentation/user-guides/inserting-generic-fields-into-forms/

CRED API details:
https://wp-types.com/documentation/user-guides/cred-api/

If you need specific instructions on how to set this up, feel free to let me know and I can lend some advice.

#547242

Thank you Christian, but that is beyond me! If you are able to give specific instructions that would be fantastic!

#547464

Christian Cox
Forum moderator

Supporter languages: English (English )

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

Ok first can you share the following information:
- Can a user select more than one item from each input? For example can they select two subcategories in the same category? If so, is a group of checkboxes more appropriate for each category instead of a dropdown (select)?
- Please share a spreadsheet or outline showing the categories, the subcategories, and all their respective slugs
- Please let me know if there are any requirements for these input fields. For example, is it required to select at least one subcategory from each category? Can a user select the main category, but not any of its subcategories? Are there any other logical structures I need to be aware of?

#547504

1) No. They can only select ONE subcategory from ONE main category

2) I haven't added them all yet, but there will be a LOT. Is there a way that I can export them, or do I have to do them manually?

3) They can only select ONE subcategory from any category. The can NOT select the main category.

#547544

Christian Cox
Forum moderator

Supporter languages: English (English )

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

Ok it sounds like it will be best to make the options dynamic since you will have a lot of them and they are not all defined yet. No need to worry about providing them right now, I will set it up so that it will work as your options for subcategories grow.

1. Create at least one main category and a few of its subcategories manually for now.
2. Create at least one post for each of the subcategories you created, so that each subcategory term will be associated with at least one post.
3. Create a View of your taxonomy. In the Query Filter section, filter by Taxonomy parent.
4. Select the top-level category from step 1 as the parent filter.
5. Add the following code to your theme's functions.php file:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 10, 3);

function prefix_clean_view_output( $out, $id ) {
  if ( $id == '12345' ) { //Please adjust to your Views ID
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}

- Replace '12345' with the numeric ID of the View you just created. You can find that in the URL when you edit your View.
- Place the View on some custom page to test it out. You should see results that look like this:

{"value":"slug-1","label":"Term 1"} ,{"value":"slug-2","label":"Term 2"} ,{"value":"slug-3","label":"Term 3"}

Let me know once you have this working and we can continue. Otherwise if you get stuck let me know and I can lend a hand.

- edited to fix code error

#549082

Hi Christian. Unfortunately the code for my functions.php gives me the white page of death!

#549228

Christian Cox
Forum moderator

Supporter languages: English (English )

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

My apologies, my text editor truncated a line and I didn't realize it. Please use the following code:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 10, 3);

function prefix_clean_view_output( $out, $id ) {
  if ( $id == '12345' ) { //Please adjust to your Views ID
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}
#549247

Thanks for that - no more white screen 🙂

I completed steps 1 to 5.
I had already created a taxonomy view for the parent category. This displays a list of the sub-categories - not your

{"value":"slug-1","label":"Term 1"}

etc

#549254

Christian Cox
Forum moderator

Supporter languages: English (English )

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

Ok great, the next step will resolve that. Erase all the code in your View's Loop Output editor. Paste in the following:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>[wpv-item index=1]{"value":"[wpv-post-id]","label":"[wpv-post-title]"}[wpv-item index=other],{"value":"[wpv-post-id]","label":"[wpv-post-title]"}</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

Now your View should output the desired format:

{"value":"slug-1","label":"Term 1"},{"value"....

Once this is working as expected, we will use the output of this View to supply the options for a generic field. In your CRED form editor, insert a generic select field. Give the field a name and where it says "Use a shortcode instead of options" paste in the shortcode to display the View you have been working on:

[wpv-view name="your-view-slug"]

When you finish inserting the field, you will find shortcode added to your CRED form like this:

[cred_generic_field field='someselectfieldname' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="your-view-slug"] ]
}
[/cred_generic_field]

Test this out and see if you find the desired options in your select field. We can go from there.

#549356

Thanks Christian.

Cred form does indeed have the shortcode like yours, but does not display anything at all.

#549558

Christian Cox
Forum moderator

Supporter languages: English (English )

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

Okay, so if you paste the View shortcode by itself outside the cred generic field shortcode, what does it display?

Test View shortcode: [wpv-view name="your-view-slug"]

Is the CRED form visible online somewhere I can test this out? I will enable private reply fields here so you can provide login credentials for me if necessary.

#549598

Christian Cox
Forum moderator

Supporter languages: English (English )

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

Ok I made the proper adjustments.

1. The code I originally provided for your Loop Output works when your View is showing posts, not taxonomy terms. My mistake, I have modified the Loop Output code to the following:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-item index=1]{"value":"[wpv-taxonomy-slug]","label":"[wpv-taxonomy-title]"}[wpv-item index=other],{"value":"[wpv-taxonomy-slug]","label":"[wpv-taxonomy-title]"}</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found]<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>[/wpv-no-items-found][wpv-layout-end]

2. The code in functions.php was referencing a different View ID, 67, but the correct View ID is 288. Will you be using a different View for each main category? The best approach is to use a single View filtered by parent category, and supply that category in a shortcode parameter. This way you don't need to maintain multiple Views that do the same thing. More information about that here:
https://wp-types.com/documentation/user-guides/passing-arguments-to-views/

Viewing 15 posts - 1 through 15 (of 26 total)

You are not logged in. You can view support threads, but not post.
If you are already logged in, please refresh your browser.

Known issues and their solutions

We document known issues and their solutions in Toolset erratas. Save time by checking there first and seeing if what you are facing is a known issue. This list is kept short. We do our best to resolve all known issues with every release of Toolset plugins.

Support Policy

Toolset staff will help you use the Toolset plugins correctly, provide any information you need, help troubleshoot problems and advice on best-practices.

Please make sure to issue only one topic per each support ticket.

For more information, read our full Customer Support Policy.

Need custom programming work for your site? Check out our certified partners.