Problem with remove_post_type_support () in Wordpress

1

I'm having a little problem with a Wordpress function, and although in principle everything is correct, the action is not executed.

  

What I need

For custom entry types, disable the add comments feature. If possible, I would like to have to edit the minimum number of templates, since this could be done by modifying the course template and adding the condition there, but I would like to make the minimum possible editions on the template, and be able to do it with the API itself of WP to avoid possible future incompatibilities.

  

Environment

  • Wordpress 4.8.3
  • StoreFront template
  • LearnDash LMS
  • Local server with Windows 10 and WAMP server.
  

What I have

$lvCursePostTypes = array('sfwd-courses','sfwd-lessons','sfwd-topic','sfwd-quiz', 'sfwd-certificates');

function remove_courses_post_comment(){
    global $lvCursePostTypes;

    foreach($lvCursePostTypes as $postType){
        remove_post_type_support($postType, 'comments');
    }
}
add_action('init', 'remove_courses_post_comment');
  

I've also tried (in case it was an array / loop problem)

function remove_courses_post_comment(){      
        remove_post_type_support('sfwd-courses', 'comments');
}
add_action('init', 'remove_courses_post_comment');
  

References

link

  

Conclusions

Is it possible that after disabling with remove_post_type_support, the template will be forced again ?, I have not found any code about it.

Through post_type_supports ('sfwd-courses', 'comments') I verify that in theory that Post Type does not have permission for comments, it still shows the form to leave a comment.

    
asked by jonilgz 10.11.2017 в 12:30
source

1 answer

1

I have solved the problem, the issue is that the StoreFront template does not check if the Post Type has the capacity to admit comments, it is done in the following way:

  

Original function

if ( ! function_exists( 'storefront_display_comments' ) ) {
    /**
     * Storefront display comments
     *
     * @since  1.0.0
     */
    function storefront_display_comments() {
        // If comments are open or we have at least one comment, load up the comment template.
        if ( comments_open() || '0' != get_comments_number() ) :
            comments_template();
        endif;
    }
}
  

Edited function

if ( ! function_exists( 'storefront_display_comments' ) ) {
    /**
     * Storefront display comments
     * EDITED: && post_type_supports(get_post_type(), 'comments') == true
     * @since  1.0.0
     */
    function storefront_display_comments() {
        // If comments are open or we have at least one comment, load up the comment template.
        if ( (comments_open() || '0' != get_comments_number()) && post_type_supports(get_post_type(), 'comments') == true ) :
            comments_template();
        endif;
    }
}

Also, the code that I had to withdraw that capacity was correct, it would be:

$lvCursePostTypes = array('sfwd-courses','sfwd-lessons','sfwd-topic','sfwd-quiz', 'sfwd-certificates');

function remove_courses_post_comment(){
    global $lvCursePostTypes;

    foreach($lvCursePostTypes as $postType){
        remove_post_type_support($postType, 'comments');
    }
}
add_action('init', 'remove_courses_post_comment');

Greetings,

    
answered by 10.11.2017 / 13:17
source