Need help with wordpress hook..

Discussion in 'WordPress' started by kajol, Mar 24, 2010.

  1. #1
    Hi,

    I want to get the post data (post_title, post_content, post_tags & post_categories) before the post data is saved into the database. The reason is, I want to update my own contents with the submitted post data before it's saved into db.

    I have tried 'content_save_pre', but there I can get only the post_content.

    Thanks in advance.
     
    kajol, Mar 24, 2010 IP
  2. petervanderdoes

    petervanderdoes Member

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #2
    I think you're looking for the filter
    wp_insert_post_data
    http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data
    Description
    A filter hook called by the wp_insert_post function prior to inserting into or updating the database.
     
    petervanderdoes, Mar 24, 2010 IP
  3. kajol

    kajol Well-Known Member

    Messages:
    523
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #3
    Thanks mate.. It's little okey, I am able to get post_title & post_content, but not tags & category...

     
    kajol, Mar 24, 2010 IP
  4. angelomaniac

    angelomaniac Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You should call another function to get the tags and categories:

    To get categories, use get_the_category($post->ID) function
    Reference: codex.wordpress.org/Function_Reference/get_the_category

    For tags, use get_the_tags($post->ID) function
    Reference: codex.wordpress.org/Function_Reference/get_the_tags
     
    angelomaniac, Mar 24, 2010 IP
  5. kajol

    kajol Well-Known Member

    Messages:
    523
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #5
    $post->ID is not available while using 'wp_insert_post_data'. Here is the available data:

    'post_author',
       'post_date',
       'post_date_gmt',
       'post_content',
       'post_content_filtered',
       'post_title',
       'post_excerpt',
       'post_status',
       'post_type',
       'comment_status',
       'ping_status',
       'post_password',
       'post_name',
       'to_ping',
       'pinged',
       'post_modified',
       'post_modified_gmt',
       'post_parent',
       'menu_order',
       'guid'
    
    Code (markup):



     
    kajol, Mar 25, 2010 IP
  6. angelomaniac

    angelomaniac Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ah, I see.. then you should hook to 'publish_post' action instead, which provide the inserted post ID. But you should get the rest of post data using function get_post().

    Unfortunately, it's done after the post is saved, not before it like you want.

    Hmm
     
    angelomaniac, Mar 25, 2010 IP
  7. kajol

    kajol Well-Known Member

    Messages:
    523
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #7
    Yes, that's what. I want to update the post content before it is getting saved into db.
     
    kajol, Mar 25, 2010 IP
  8. hassan.seth

    hassan.seth Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Can yu please write heree.. how did you updated 'post_content' before it is saved to DB??

    i want to add my tags to it! before it is saved! via plugin! and filter!

    is it possible?
     
    Last edited: Jun 9, 2010
    hassan.seth, Jun 9, 2010 IP
  9. hassan.seth

    hassan.seth Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I am trying the following code!

    but does not work, when i post a new 'POST'

    <?php
    /*
    Plugin Name: MS_Link
    Plugin URI: http://de-shell.co.cc
    Description: MS to HREF MS =P
    Version: 0.0
    Author: Hafiz Hassan Farid
    Author URI: http://de-shell.co.cc
    */
    ?>
    
    <?php
    /*
    function email_friends($post_ID)  {
        $friends = 'k08075@nu.edu.pk';
    $text = "HASSAN";
    $post = get_post($post_ID);
    $post->post_content = $text;
    
    $post = get_post($post_ID);
    $ppost = $post->post_content;
        mail($friends, "sally's blog updated", $ppost);
        return $post_ID;
    }*/
    
    function filter_MS($content, $raw) {
        $keys = 'MICROSOFT';
        //$content->post_content=str_ireplace($keys,"<a href='http://microsoft.com>MICROSOF</a>", $content->post_content);
    $content->post_content = str_replace($keys, "<a href='http://microsoft.com>MICROSOF</a>");
        return $content;
    }
    
    //add_action ( 'edit_post', 'email_friends');
    add_filter ( 'wp_insert_post_data', 'filter_MS');
    
    ?>
    Code (markup):
     
    hassan.seth, Jun 9, 2010 IP