WordPress.org

Forums

Perform a wpdb->query on every new post submission (6 posts)

  1. Sabinou
    Member
    Posted 3 years ago #

    Hello,

    I want to perform a wpdb->query command every time I submit or update a post, is there a way to achieve that ?

    If you wonder - but explaining WHY would be too long, I'll just say there's a good reason for it in my case -, I want to have this SQL command ran to replace relative hyperlinks with absolute hyperlinks :

    UPDATE wp_posts SET post_content = REPLACE (
    post_content,
    '../',
    'http://www.mywebsite.net/');

    So, I want to have wordpress run, every time I update a post (since wordpress has that "bad" - in my case, haha - habit of resorting to relative hyperlinks), this :

    $wpdb->query(
    "
    UPDATE wp_posts SET post_content = REPLACE (
    post_content,
    '../',
    'http://www.myblog.net/');
    "
    );

    At the moment, I'm manually running this in phMmyAdmin once per day.
    It just struck me that I might save it to a php file to run from an URL since phpMyAdmin allows to get a php code version (brutal discoveries of things obvious to everyone else since long ago, I call that "nero burning ROM moments" ;) ).
    But, ideally, the very best would be to have wordpress perform the SQL query itself every time it is needed.

    Would you know how to achieve it, to make wordpress run this wpdb->query, every time I update or publish a blog note ?

    Thank you very much if you can help ! :)

    Sabinou

  2. vtxyzzy
    Member
    Posted 3 years ago #

    Take a look at the 'content_save_pre' filter in the Codex here:

    http://codex.wordpress.org/Plugin_API/Filter_Reference#Database_Writes

    I think you should be able to use a preg_replace on the content and do what you want without a query.

  3. Sabinou
    Member
    Posted 3 years ago #

    Thanks for the reply, Vtxyzzy ! :)

    Unless I'm mistaken, that would imply I have to write myself a custom-made plugin, something I never did.

    Wouldn't it end up in being even more complicated than to resort to the wbdb query function and finding a way to have it ran with every post update or creation ?!?

  4. vtxyzzy
    Member
    Posted 3 years ago #

    This does not require a plugin. You write a function that is assigned to the filter and changes the content before it is stored in the database. It is actually simpler than using a query, and avoids the overhead of a query.

  5. vtxyzzy
    Member
    Posted 3 years ago #

    Here is a sample of the code that would be added to functions.php (UNTESTED):

    function fix_relative_links($content) {
       $content = preg_replace('#\.\./#','http://www.myblog.net/',$content);
       return $content;
    }
    add_filter('content_save_pre','fix_relative_links');
  6. Sabinou
    Member
    Posted 3 years ago #

    Thank you once again !

    I must confess I had no idea that a template's functions.php could (in a way) act like a plugin, and be scanned by the wordpress engine for potential hooks triggering actions.

    I'll look into that tomorrow, when I can use a computer with WAMP (better be safe and not ruin a website already live, haha).

    You were really helpful, Vtxyzzy; thank you :)

Topic Closed

This topic has been closed to new replies.

About this Topic