• Hello, I’m reaching out to report a potential issue with the comments_like_dislike function provided by your plugin, which adds like/dislike buttons to the comments section of WordPress themes. I appreciate the functionality your plugin offers, and my objective is to display these buttons next to the reply button, which requires positioning them directly within the theme files rather than at the end of the comment text, which is the default behavior of the plugin.

    # Issue Description

    When attempting to directly insert the like/dislike buttons using the comments_like_dislike($comment_id) function within my theme’s comments.php file, I encountered a fatal error. Below are the steps I took to reproduce the error:

    In my-theme/comments.php:

    wp_list_comments([
      'callback' => 'my_comment_template',
    ]);

    In my-theme/functions.php:

    function my_comment_template($comment, $args, $depth) {
      ...
      comments_like_dislike($comment->comment_ID);
      ...
    }

    # Encountered Error:

    Fatal error: Uncaught Error: Using $this when not in object context in /var/www/html/wp-content/plugins/comments-like-dislike/inc/views/frontend/like-dislike-html.php:30

    # Analysis

    Upon inspecting the plugin’s source code, I noticed that the comments_like_dislike function directly includes a PHP file (like-dislike-html.php) that presumably requires context from the CLD_Comments_Hooks class to function correctly. This inclusion strategy works when the plugin automatically appends like/dislike buttons but fails when like-dislike-html.php is loaded through the theme, as the class context ($this) is lost.

    # Proposed Solution

    To circumvent this issue, I devised a custom function, my_comments_like_dislike, which aims to correctly hook into the plugin’s functionality without direct file inclusion, preserving the necessary class context:

    function my_comments_like_dislike($comment = null) {
        if (isset($_REQUEST['comment'])) {
            return '';
        }
        if (is_admin() && !wp_doing_ajax()) {
            return '';
        }
        $post_id = get_the_ID();
        do_action('cld_like_dislike_output', $comment, $post_id);
    }

    This approach appears to resolve the issue on my end. However, I’m reaching out to see if you could confirm this as a viable solution or suggest an alternative method to achieve the same result without encountering the error. Your guidance on how to properly use comments_like_dislike within theme files would be greatly appreciated.

    # Additional Request

    Furthermore, I would appreciate it if the plugin could be adjusted to allow the removal of the default filter that appends like/dislike buttons to the comment text. This filter is currently added as follows:

    add_filter('comment_text', array($this, 'comments_like_dislike'), 200, 2);

    Removing this filter from within a theme’s functions.php file does not seem feasible without access to the class that $this references. An option to disable this default behavior through the plugin settings or a documented method to remove the filter would greatly enhance flexibility and allow for custom placement of the like/dislike buttons.

    Thank you for considering my request and issue report. Your plugin greatly enhances WordPress comment interaction, and I believe these adjustments will make it even more versatile for theme developers.

    • This topic was modified 8 months, 3 weeks ago by fyklr.
  • The topic ‘Issue with Custom Function “comments_like_dislike” when used in theme’ is closed to new replies.