WordPress.org

Forums

Remove hash from recent comments widget on WordPress (10 posts)

  1. evo252
    Member
    Posted 3 years ago #

    I'm building a WordPress theme with Ajax load, all is running fine except the links that have hash on their URLs, like the links of the recent comments widget for instance: myblog.com/blog-post/#comment-102

    These links aren't loading, and they block the ajax load.

    Is there a way to make these links generally not block the ajax loading ? If it's not possible, how can I hack the default recent comments widget of WordPress to remove hash from links and make them link to the general post, and not to the specific comment (e.g link to myblog.com/blog-post/ rather than myblog.com/blog-post/#cmment-102) ?

    Is someone may help me to achieve this, it would be fantastic.

  2. Pioneer Web Design
    Member
    Posted 3 years ago #

    Try escaping special characters for example replace # with %23 in the url or ensure that a function does it.

  3. evo252
    Member
    Posted 3 years ago #

    Thank you but how could I do that?

  4. Pioneer Web Design
    Member
    Posted 3 years ago #

    The # represents a bookmark that when used in all modern browsers take you to that location on the page and furthermore not using the default permalink setup for a comment would likely impact SEO, so let's not do that.

    See this:

    http://www.the-art-of-web.com/javascript/escape/

    and this

    http://codex.wordpress.org/Function_Reference/esc_url

  5. evo252
    Member
    Posted 3 years ago #

    Yes, I know this part of the problem. But how could I do to make ajax loading to not be blocked by hash on URLs ?

  6. Pioneer Web Design
    Member
    Posted 3 years ago #

    Remove the hash and replace with escaped character(s) as noted above and in the links.

  7. evo252
    Member
    Posted 3 years ago #

    May you show me a little example of a function that would make that because I'm a little lost on how to do it ?

  8. Pioneer Web Design
    Member
    Posted 3 years ago #

    I can link you to how this is done but if you do not understand how to apply a function as described in the codex I am not sure how else I can help.

  9. evo252
    Member
    Posted 3 years ago #

    If you can provide me a piece of code to implement to functions.php it would be so sweet, I do not have any idea about the way to do it...

  10. evo252
    Member
    Posted 3 years ago #

    I succeed to hack the Recent comments widget. For those who would need this hack, here is the code I used in my functions.php file:

    add_action( 'widgets_init', 'recent_comments_widgets' );
    
    function recent_comments_widgets() {
        register_widget('Recent_Comments');
    }
    
    class Recent_Comments extends WP_Widget {
        function Recent_Comments() {
            $widget_ops = array('classname' => 'recent_comments', 'description' => __('The most recent comments'));
            $this->WP_Widget('recent-comments', __('Recent Comments'), $widget_ops);
        }
    function widget($args, $instance) {
        extract($args);
    	$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Recent Comments' ) : $instance['title'], $instance, $this->id_base );
        $number = $instance['number'];
    // Begin Widget
    echo $before_widget;
    
    if ($title) echo $before_title . $title . $after_title; ?>
    
    <div id="recent-comments">
    
        <ul>
    
            <?php
            global $wpdb;
            $request = "SELECT * FROM $wpdb->comments";
            $request .= " JOIN $wpdb->posts ON ID = comment_post_ID";
            $request .= " WHERE comment_approved = '1' AND post_status = 'publish' AND post_password =''";
            $request .= " ORDER BY comment_date DESC LIMIT $number";
            $comments = $wpdb->get_results($request);
            if ($comments) { foreach ($comments as $comment) {
            ob_start(); ?>
    
                <li>
    
                    <div class="comment-excerpt">
                        <span class="comment-author"><?php echo($comment->comment_author) ?> <?php _e('on','artister'); ?> <a href="<?php echo get_permalink( $comment->comment_post_ID ); ?>"><?php echo get_the_title($comment->comment_post_ID) ?></a></span>
                    </div>
    
                </li>
    
            <?php ob_end_flush(); }} else { // If no comments ?>
    
                <li></li>
    
            <?php } ?>
    
        </ul>
    
    </div>
    
    <?php echo $after_widget;
    // End Widget
    }
    
    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['number'] = strip_tags($new_instance['number']);
        return $instance;
    }
    
    function form( $instance ) {
        $defaults = array('title' => 'Recent Comments', 'number' => '5'); $instance = wp_parse_args((array) $instance, $defaults ); ?>
    
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
            <br/><input class="widefat" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
        </p>
    
        <p>
            <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of comments to show:'); ?></label>
            <input type="text" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" size="3" value="<?php echo $instance['number']; ?>" />
        </p>
    
        <?php
    }
    }

Topic Closed

This topic has been closed to new replies.

About this Topic