WordPress.org

Forums

HELP: custom comments link with plugin (9 posts)

  1. oats
    Member
    Posted 8 years ago #

    Hello,
    I am trying to modify the comments link generated by wordpress, but I can't seem to get the plugin to work. I am trying to filter/replace the "get_comments_link" function. If I change the source file, I get the change I want, but I can't seem to get the plugin to do anything - the original function always gets called instead.

    What am I doing wrong??

    //==================================================================
    // Comments Link Replacement
    //==================================================================
    function get_comments_link_hijack() {
    	...
    
    }
    
    /*
    THE ORIGINAL, DEFINED IN comment-template.php
    function get_comments_link() {
    	return get_permalink() . '#comments';
    }
    */
    
    // Create hook for wordpress
    add_filter('get_comments_link','get_comments_link_hijack');
  2. Doodlebee
    Member
    Posted 8 years ago #

    You're doing it wrong.

    The hooks are used to place stuff into the areas that are created for it: wp_head(), wp_meta(), etc. You can't add a hook to an already-defined function.

    You can write a function based on the existing code, but you have to rename it, and then call it in. So instead of calling in your sidebar with get_comments_link() (as you normally would), you would use get_comments_link_hijack() to use your version.

  3. oats
    Member
    Posted 8 years ago #

    Thanks for the reply... it sounds like what you are recommending is that I define a new function, and then update my template to use the modified function.

    I was hoping for a solution which wouldn't require changes to templates, but just modify this function globally. Is it possible? Is there some other hook I could use?

  4. oats
    Member
    Posted 8 years ago #

    Perhaps I can use this pluggable function technique? Can I plug any core function I want, or only certain functions?

    http://codex.wordpress.org/Pluggable_Functions

  5. Doodlebee
    Member
    Posted 8 years ago #

    it sounds like what you are recommending is that I define a new function, and then update my template to use the modified function.

    Yes.

    I don't know about pluggable functions - never tried it. But if you're trying to write a plugin for people to use, then you *do* want to define a custom function for them to edit their own theme for. You don't want something that messes with the core code - what if they want to shut it off, or they decide they don't like it?

  6. oats
    Member
    Posted 8 years ago #

    But if you're trying to write a plugin for people to use,.... You don't want something that messes with the core code - what if they want to shut it off, or they decide they don't like it?

    Umm, thats exactly what a plugin does - it doesn't mess with the core code, and allows people to easily turn it on/off.

  7. frames
    Member
    Posted 7 years ago #

    I'm looking for the exact same thing without hacking the core or doing complex things.

    What I simply want to do is change #comments for #comentarios (in Spanish) to the comment links. Looks like get_comments_link is the function to change, which is in comment-template.php (in the wp-includes directory, of course). This is it:

    /**
     * get_comments_link() - Retrieves the link to the current post comments
     *
     * @since 1.5
     *
     * @return string The link to the comments
     */
    function get_comments_link() {
            return get_permalink() . '#comments';
    }

    As doodlebee points out, defining a new function would work. But, comments_popup_link makes use of comments_link, which is at the same time making use of get_comments_link again. That means, if I change the get_comments_link function to, say, get_comments_link_sp, then I also have to change comments_link to comments_link_sp, and finally, comments_popup_link to comments_popup_link_sp. Changing at least one line in each function.

    Like I say, that works, but it means copying and pasting three functions, then changing the code.

    That's a nightmare! Seriously, hacking the core would be easier if this is the only route. At least I would save the hassle (and possible errors) of copying and pasting each time the core code changes.

    Plus, I think any other plugins or code that would use the 'standard' WordPress code would not work for me. Which means more hacking?

    Furthermore, there's other functions to be changed, such as get_comment_link. Again, more pain.

    I have found out all of this without any PHP knowledge, but I feel there must be a way to make all of this much simpler.

    Any advise or help would be much appreciated.

    Cheers!

  8. frames
    Member
    Posted 7 years ago #

    To make things worse, I have found out that the original get_comments_link (as expected) is widely used not only in themes, but also in other parts of WordPress.

    For instance, all the comments feeds (global and per-entry) and even worse, in admin sections. If I use a customised function, it will break both the feeds and the admin.

    Like I said, I don't know any PHP, but I'd be happy to investigate this if anyone can shred some light here.

  9. PostedPost
    Member
    Posted 7 years ago #

    I met this problem too. Fist I want add a filter, later want redeclare the function, of course cannot at PHP. Finally I just rewrite the get_comment_link function in includes/comment-template.php file and make a note for check the change on every time apply patch or update.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.