Support » Plugin: DCO Comment Attachment » restrict attachment to specific post categories

  • I love your plugin, thanks for that.
    Can you help me with what I should add to my function.php in order to restrict adding attachment only to specific post categories (or disabling for specific post categories)
    thanks a lot in advance

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Denis Yanchevskiy

    (@denisco)

    Hello @pauldewin,

    I’m glad that the plugin was useful to you.

    You can try this snippet:
    (adding attachment will be allowed only for category with ID 56)

    function custom_dco_ca_disable_attachment_field() {
    	$url = "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
    	$post_id = url_to_postid( $url );
    	$cats = wp_get_post_categories( $post_id, array( 'fields' => 'ids' ) );
    	$allow_cat_id = 56;
    	if ( in_array( $allow_cat_id, $cats ) ) {
    		return false;
    	}
    		
    	return true;
    }
    
    add_filter( 'dco_ca_disable_attachment_field', 'custom_dco_ca_disable_attachment_field' );

    I just used this code and it worked seamlessly. Thanks so much, Denis!

    Well, I spoke too soon. While this code does restrict the upload option to a particular category, it also breaks the attachment upload functionality. Now whenever I upload an image, it posts the comment, but it doesn’t upload/attach the image. Any suggestions?

    Plugin Author Denis Yanchevskiy

    (@denisco)

    Hmm, indeed, the upload stopped working after applying the code. At the moment, I don’t know how to fix it. I’ll try to come back to this later.

    For a quick fix, you can try this snippet.

    function disable_upload_field( $markup ) {
    	if( ! in_category( 56 ) ) {
    		return '';
    	}
    	
    	return $markup;
    }
    
    add_filter( 'dco_ca_form_element_label', 'disable_upload_field' );
    add_filter( 'dco_ca_form_element_input', 'disable_upload_field' );

    It will not disable upload altogether, but will hide the upload field in all categories except the specified one.

    ryanworthen

    (@ryanworthen)

    Just tried that code and my site crashed. šŸ™ I’m back online now, but unfortunately I’m not savvy enough to troubleshoot why it went down.

    ryanworthen

    (@ryanworthen)

    Any ideas or suggestions on why that code snippet above would cause a critical error?

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.