Skip Navigation

[Resolved] Limiting User’s ability to change custom post

This thread is resolved. Here is a description of the problem and solution.

Problem:

I'm wondering if there is a way to limit the User from changing his/her custom post to a X number of times a month. For example, I only want to allow the User to change images twice a month.

Solution:

There isn't such kind of built-in feature, you can consider custom codes:

For example:
https://toolset.com/forums/topic/limiting-users-ability-to-change-custom-post/#post-1377659

Relevant Documentation:

This support ticket is created 4 years, 5 months 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 32 replies, has 3 voices.

Last updated by FelipeP5703 4 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#1387967

Minesh, thanks for your help, but Luo has already mentioned that and I have shown to him that there are no contractors available. I will just wait until Luo comes back from vacation.

I appreciate your input.

#1388509

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'll pass the ticket to Luo's queue and he will reply to you when you will back from vacation the next week.

However, even Luo should not be able to share the custom code solution with you as its beyond the scope of our support policy.

#1390777

I can get your website copy from your another thread, and trying to test it in my localhost, will update here if there is anything found.

#1390911

Since those custom images fields only exists in post form "Formulário para Editar Anúncio PODE"( ID 1355), so I am using this post form for example, please try these:

1) Create a custom field group with two fields:
hidden link
- Date field "last-modify-date", we can use it to store the last image changed date
- Numeric field "count", store the count number

I have done above step in your website, you need to do below steps manually.

2) Edit your theme file "functions.php", add below codes:


// Check images changed
add_action('cred_before_save_data', function($form_data){
	if($form_data['id'] == 1355){
		$post_id = $_POST['_cred_cred_prefix_post_id'];
		$pre_image['wpcf-foto-perfil'] = get_post_meta($post_id, 'wpcf-foto-perfil', true);
		$pre_image['wpcf-fotos-para-anuncio'] = get_post_meta($post_id, 'wpcf-fotos-para-anuncio');
		$new_image['wpcf-foto-perfil'] = $_POST['wpcf-foto-perfil']?$_POST['wpcf-foto-perfil']:$pre_image['wpcf-foto-perfil'];
		$new_image['wpcf-fotos-para-anuncio'] = $_POST['wpcf-fotos-para-anuncio']?$_POST['wpcf-fotos-para-anuncio']:$pre_image['wpcf-fotos-para-anuncio'];
		$changed = 0;
		if($pre_image != $new_image){
			$changed = 1;
		}
		$_POST['changed'] = $changed;
	}
}, 10, 1);

// Update change date and count
add_action('cred_save_data', function($post_id, $form_data){
	if($form_data['id'] == 1355){
		$changed = isset($_POST['changed'])?$_POST['changed']:0; 
		if($changed == '1'){
			$count = get_post_meta( $post_id, 'wpcf-count', true );
			if ( ! $count ) {
				$count = 0;  // if the meta value isn't set, use 0 as a default
			}
			$count++;
			update_post_meta($post_id, 'wpcf-count', $count );
			update_post_meta($post_id, 'wpcf-last-modify-date', current_time( 'timestamp'));
		}
	}
}, 10, 2);

// Check if user can change images shortcode
add_shortcode('can_change_images', function($atts, $content){
	$post_id = isset($_GET['f_editar'])?$_GET['f_editar']:0; // URL parameter f_editar
	
	if($post_id){
		$current_month = current_time( 'Y-m');
		$last_modify_month = types_render_field('last-modify-date', array('format' => 'Y-m', 'post_id' => $post_id));
		$count = types_render_field('count', array('post_id' => $post_id));
		$res = 1;
		if($count >=2 && $current_month == $last_modify_month){
			$res = 0;
		}
		return $res;
	}
});

3) Dashboard-> Toolset-> Settings-> Front-end Content
in option "Third-party shortcode arguments", add above shortcode name: can_change_images

4) Edit post form "Formulário para Editar Anúncio PODE":
hidden link

in section "Form editor", line 121 ~ 130, wrap those two image field with below shortcodes, like this:

	[wpv-conditional if="( '[can_change_images]' eq '1' )"]<div class="row">
		<div class="form-group col-sm-6 profile-photo">
			<label>Imagem de Perfil (de capa, de rosto, tamanho max de 1MB) Melhor foto é de 300 x 300</label>
			[cred_field field="foto-perfil" force_type="field" class="form-control" output="bootstrap"]
		</div>
		<div class="form-group col-sm-6 limit-to-6 reptive-photos">
			<label>Fotos para Anúncio (Max de 3 fotos sozinhas(os) e Max de 3 fotos fazendo massagem,tamanho max de 1MB)</label>
			[cred_field field='fotos-para-anuncio' force_type='field' class='form-control limit-to-6' output='bootstrap']
		</div>
	</div>[/wpv-conditional]

And test again

More help:
https://toolset.com/documentation/programmer-reference/cred-api/

#1392779

Ok I have done what you asked above and I tried to change the images. Once I changed twice the form changed to the other one where there is no pictures available to change. Great!

But now what happens? After a month, does it go back to the original form so the user can change again, or do I have to manually change the count to zero?

#1392901

Yes, it should be able to go back to the original form so the user can change again in next month, see the PHP codes I mentioned above:

       $current_month = current_time( 'Y-m');
       $last_modify_month = types_render_field('last-modify-date', array('format' => 'Y-m', 'post_id' => $post_id));
        $count = types_render_field('count', array('post_id' => $post_id));
        $res = 1;
        if($count >=2 && $current_month == $last_modify_month){
            $res = 0;
        }

Those codes are used for checking the month values, you don't need to change it manually.

#1392905

And you are right, there might be a problem in the next month, please try to modify this line of PHP codes from:

            if ( ! $count ) {

To:

            $current_month = current_time( 'Y-m');
            $last_modify_month = types_render_field('last-modify-date', array('format' => 'Y-m', 'post_id' => $post_id));
            if ( ! $count ||  $current_month != $last_modify_month) {
 

It should be able to update the count field value automatically.

#1393359

Luo,

I had to change one thing. The conditional function cannot be added to the custom post form because of this: https://toolset.com/forums/topic/pics-are-removed-when-custom-post-is-edited/

Thus I included the conditional function in the View named view-to-show-the-correct-edit-form-conditionally with the AND function like this:

[wpv-conditional if="( '[types usermeta='mudar-fotos' user_current='true' output='raw'][/types]' eq '1' ) AND ( '[can_change_images]' eq '1' )"]
			[cred_form form='formulario-para-editar-anuncio-pode' post='[wpv-search-term param="f_editar"]']
		[/wpv-conditional]
		[wpv-conditional if="( '[types usermeta='mudar-fotos' user_current='true' output='raw'][/types]' eq '2' )"]
			[cred_form form='formulario-para-editar-anuncio' post='[wpv-search-term param="f_editar"]']
		[/wpv-conditional]

I also changed what you asked above and I just want to make sure I changed the code correctly, below is the complete code:

// Update change date and count
add_action('cred_save_data', function($post_id, $form_data){
    if($form_data['id'] == 1355){
        $changed = isset($_POST['changed'])?$_POST['changed']:0; 
        if($changed == '1'){
            $count = get_post_meta( $post_id, 'wpcf-count', true );
            $current_month = current_time( 'Y-m');
			$last_modify_month = types_render_field('last-modify-date', array('format' => 'Y-m', 'post_id' => $post_id));
			if ( ! $count ||  $current_month != $last_modify_month) {
                $count = 0;  // if the meta value isn't set, use 0 as a default
            }
            $count++;
            update_post_meta($post_id, 'wpcf-count', $count );
            update_post_meta($post_id, 'wpcf-last-modify-date', current_time( 'timestamp'));
        }
    }
}, 10, 2);

Also how can I test it to make sure it works well without waiting a whole month? Do I change the date field to tomorrow or something?

Thanks again

#1393429

Sorry to bother you again Luo, but this code below that I tried to add with the conditional display is only working half way.

[wpv-conditional if="( '[types usermeta='mudar-fotos' user_current='true' output='raw'][/types]' eq '1' ) AND ( '[can_change_images]' eq '1' )"]
            [cred_form form='formulario-para-editar-anuncio-pode' post='[wpv-search-term param="f_editar"]']
        [/wpv-conditional]
        [wpv-conditional if="( '[types usermeta='mudar-fotos' user_current='true' output='raw'][/types]' eq '2' )"]
            [cred_form form='formulario-para-editar-anuncio' post='[wpv-search-term param="f_editar"]']
        [/wpv-conditional]

It works when the "can_change_images" has not reached 2, but when it does reach 2, then instead of showing the bottom form, it just does not show anything.

I believe it is because of the first condition, custom field "mudar-fotos." This custom field is used by us Admin, to allow or not the User to change the images. So I tried the following code:

[wpv-conditional if="( '[types usermeta='mudar-fotos' user_current='true' output='raw'][/types]' eq '1' ) AND ( '[can_change_images]' eq '1' )"]
			[cred_form form='formulario-para-editar-anuncio-pode' post='[wpv-search-term param="f_editar"]']
		[/wpv-conditional]
      	[wpv-conditional if="( '[types usermeta='mudar-fotos' user_current='true' output='raw'][/types]' eq '1' ) AND ( '[can_change_images]' gte '3' )"]
			[cred_form form='formulario-para-editar-anuncio' post='[wpv-search-term param="f_editar"]']
		[/wpv-conditional]
		[wpv-conditional if="( '[types usermeta='mudar-fotos' user_current='true' output='raw'][/types]' eq '2' )"]
			[cred_form form='formulario-para-editar-anuncio' post='[wpv-search-term param="f_editar"]']
		[/wpv-conditional]

My thought was, if the first statement is true AND the second is TRUE then is should show the form. But again, when the "can_change_images" is above 2 the form is not shown. I'm not sure how to fix this.

#1393951

Q1) Also how can I test it to make sure it works well without waiting a whole month? Do I change the date field to tomorrow or something?

You can change the date field "last-modify-date" value to last month, for example 2019-10-11, then test again.

Q2) It works when the "can_change_images" has not reached 2, but when it does reach 2, then instead of showing the bottom form, it just does not show anything.
You can change the wpv-conditional shortcode like this:

[wpv-conditional if="( '[types usermeta='mudar-fotos' user_current='true' output='raw'][/types]' eq '1' ) AND ( '[can_change_images]' eq '1' )"]
Here display form with image fields
[cred_form form='formulario-para-editar-anuncio-pode' post='[wpv-search-term param="f_editar"]']
[/wpv-conditional]

[wpv-conditional if="( '[types usermeta='mudar-fotos' user_current='true' output='raw'][/types]' eq '1' ) AND ( '[can_change_images]' eq '1' )" evaluate="false"]
Here display another edit form without  image fields...
[/wpv-conditional]

More help:
hidden link

evaluate (opt):
true | false
It can be set to true or false and controls whether we want the condition to be met or not. The default value is true.

#1394475
error_while_updating_post.jpg

Yes, it worked great! Thank you!

But when I went to change the "last-modify-date" custom field to test if it would would work, after clicking update post, I get the error (screenshot).

It might be that error you mentioned that you guys already know about.

#1395063

As I mentioned in your another thread:
https://toolset.com/forums/topic/expiration-is-not-saving-the-date/page/2/#post-1390775
It is a known issue.
Currently, you can disable the multiple instance image field, for example, edit field group:
hidden link

Edit image field "Fotos para Anúncio", switch to option: This field can have only one value

#1395975

Luo,

I understand that that field can only have one value for now, but how are the User's suppose to show all their images in their custom post? Do I have to create 5 other custom image fields? Seem redundant.

Also, is this a priority for the development team since it seems to be a big one? I bet I'm not the only one having this issue.

I appreciate your help btw!

#1397693

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Luo is on vacation. Do you need urgent help? If no: Luo will be back to work on this Thursday and will take care of this ticket.

#1397745

Minesh, it's not urgent, thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.