control posts comments with customfields on or off

Support General control posts comments with customfields on or off

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #10875
    brkardbrkard
    Participant

    Hi.

    I have created a topic before but i cant open again here > https://support.metabox.io/topic/control-posts-comments-on-or-off/

    I want to use an select or radio field type for enable comments for posts or pages or any cpt. You have send me a snippet to do this like below:

    add_filter( 'rwmb_frontend_insert_post_data', function( $data, $config ) {
        if ( 'meta-box-id' == $config['id'] ) {
            $data['comment_status'] = 'closed';
        }
        return $data;
    }, 10, 2 );

    But i cant make it work.

    Can you explain this snippet a little bit more.

    I want to use a checkbox field to allow comments like the screenshot here > https://pasteboard.co/HxAvsqS.png

    I want to make this with your plugin works with also in front end.

    So if i can use the snippet you have send, can you explain a little bit more how to use it ?

    Thanks

    #10882
    Anh TranAnh Tran
    Keymaster

    Hi,

    Basically, the snippet change the post data before it's saved to the database. In this case, it set the comment status to closed for all submitted posts.

    If you want to make a checkbox for toggling comment status, you can add a checkbox field to the meta box and change the condition to something like this:

    // change
    // if ( 'meta-box-id' == $config['id'] ) {
    // to
    if ( 'meta-box-id' == $config['id'] && !empty( $_POST['checkbox_id'] ) ) {
    #10886
    brkardbrkard
    Participant

    Hi Anh..Thanks for your reply.

    I cant make it work with your snippet..

    But i found another way with another sniippet...Thank you very much for your help.

    Here it is working snippet:

    /**
     * Enable or disable comments based on custom field Allow Comments.
     *
     * @param bool $open    Whether comments should be open.
     * @param int  $post_id Post ID.
     * @return bool Whether comments should be open.
     */
    function wpdocs_comments_open( $open, $post_id ) {
        $post = get_post( $post_id );
            if (get_post_meta($post->ID, 'allow_comments', true)) {
            $open = true;
        }
        return $open;
    }
    add_filter( 'comments_open', 'wpdocs_comments_open', 10, 2 );
Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘control posts comments with customfields on or off’ is closed to new replies.