Forums

[resolved] Modifying links to add date to the database (6 posts)

  1. termserv
    Member
    Posted 6 months ago #

    In my WP installations, I use links alot, and these links are added in different categories.

    I have a custom way to show the links. First the category, then the latest five links in each category.

    But the way I wan't to modify this view even more, is to have the category with the newest link shown at the top.

    I've explored the links database table, and notice that there is a field for date and time, but it's not used by WP. After looking up in the WP code I've seen that WP uses the add_link() function to actually add the link, but I can't find the function itself.

    Does anyone know where this is, or even better, how to get WP to add date and time to the links in the database?

  2. MichaelH
    moderator
    Posted 6 months ago #

    You can use http://wingrep.com (or something like it) to search the WordPress files for "add_link" or "link_updated"

    In theory, WordPress could use that link_updated field (see wp-admin/update-links.php), why not sort your links by ID in descending order?

  3. termserv
    Member
    Posted 6 months ago #

    Well, I am sorting the links itself by ID in descending order. A mockup of how it looks is like this:

    Category 1

    • New link
    • Not so new link

    Category 2

    • Newest link
    • Old link

    Since Category 2 holds the newest link (Newest link), I want the the whole Category 2 before Category 1. To make that work, I could pull the links out directly from the database using custom code, but since WP doesn't use the date/time field in the links table, I have no way to sort it like this.

    So I thought I could modify the function that actually adds the links to the database, so it would update that field.

  4. termserv
    Member
    Posted 6 months ago #

    I made it work! :)

    The actual code is in wp-admin/includes/bookmark.php

    I edited the following:

    Original line 192:
    link_rel = %s, link_notes = %s, link_rss = %s

    New line 192:
    link_updated = NOW(), link_rel = %s, link_notes = %s, link_rss = %s

    Original line 200:
    if ( false === $wpdb->query( $wpdb->prepare( "INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",

    New line 200:
    if ( false === $wpdb->query( $wpdb->prepare( "INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_description, link_visible, link_owner, link_rating, link_updated, link_rel, link_notes, link_rss) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, NOW(), %s, %s, %s)",

    So what I basically did was that WordPress is consequently leaving the field link_updated out, I just added it to the INSERT and UPDATE queries. WordPress doesn't use them, but now I can make use of them by writing custom code.

  5. miekd
    Member
    Posted 3 months ago #

    For updating the link_updated field, I created a micro-plugin that hooks into the add and edit actions of links.

    This way you can update Wordpress without problems and don't have to get into the core PHP files.

    Linky: http://wordpress.org/extend/plugins/link-updated/

  6. hadiaminiv
    Member
    Posted 3 weeks ago #

Reply

You must log in to post.

About this Topic