Results 1 to 8 of 8

Thread: wp_insert_post_data Help

  1. #1
    WPSardine is offline Hello World
    Join Date
    Jun 2010
    Posts
    14

    Default wp_insert_post_data Help

    I'm struggling getting the wp_insert_post_data filter to work as I would like. Can anyone see anything that I'm doing fundamentally wrong in this code?

    Within a researcher class I have:
    Code:
    function add_description($post_id)
    {
    	if(!empty($_POST['researcher_content']))
    	{
    		add_filter('wp_insert_post_data', array(&$this, 'add_description_filter_handler'), 99);
    	}
    }
    
    function add_description_filter_handler($data, $postarr)
    {
    	$data['post_content'] = $_POST['researcher_content'];
    	return $data;
    }
    add_description is called earlier in the class that is then called by a method of another function which is called on the save_post action.

    I feel like I'm simply missing the point on something. Can anyone slap me back into the correct direction?

    Thanks!

  2. #2
    WPSardine is offline Hello World
    Join Date
    Jun 2010
    Posts
    14

    Default

    I think there might be some issue with calling add_filter within a class method. I've tried to insert code inside of the filter handler function that would cause WP to die, but it's not working. I'm quite confused.

    I also used this similar code with a test plugin and it worked just fine.

  3. #3
    WPSardine is offline Hello World
    Join Date
    Jun 2010
    Posts
    14

    Default

    Ok...so here's what I've learned. When I use "add_filter" from the class constructor, it works. When I use "add_filter" within any other class method. It does not work. Even when I call a method from the constructor that then utilizes "add_filter", it does not work. Can anyone explain this?

  4. #4
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    If your filter function takes more than one argument you must specify amount in add_filter() call.

    Had you checked that filter is getting added at all?
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

  5. #5
    WPSardine is offline Hello World
    Join Date
    Jun 2010
    Posts
    14

    Default

    Quote Originally Posted by Rarst View Post
    If your filter function takes more than one argument you must specify amount in add_filter() call.
    I caught this error early on. Surprisingly, I took this code straight from the codex.

    Quote Originally Posted by Rarst View Post
    Had you checked that filter is getting added at all?
    I did check and it appeared that it wasn't being added. I just checked by putting in code that would cause problems and it didn't actual throw any errors. Is there a better way to do this?

    I still cannot figure out why it won't work outside of the constructor method, but I've made it work by calling it at that time.

  6. #6
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    I did check and it appeared that it wasn't being added. I just checked by putting in code that would cause problems and it didn't actual throw any errors. Is there a better way to do this?
    Do you have errors set to show?..

    As for better way to look inside of hook you can add debug function to 'all' action (runs for every hook) and do something when current_filter() equals hook you are interested in. I am currently tinkering on some debug functions for that, will post them some time soon.
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

  7. #7
    WPSardine is offline Hello World
    Join Date
    Jun 2010
    Posts
    14

    Default

    Quote Originally Posted by Rarst View Post
    Do you have errors set to show?..

    As for better way to look inside of hook you can add debug function to 'all' action (runs for every hook) and do something when current_filter() equals hook you are interested in. I am currently tinkering on some debug functions for that, will post them some time soon.
    Thanks for the response Rarst. I'd be interested in this code when you have it. I've moved on with another way to handle my problem, but I'd still like to come back to this issue to see what the problem was.

  8. #8
    Rarst's Avatar
    Rarst is offline Big Tipper
    Join Date
    Jul 2009
    Posts
    322

    Default

    I posted my set of debug functions for WP hooks earlier this week
    http://www.rarst.net/script/debug-wordpress-hooks/

    I keep finding rough spots after post went live (typical), but mostly they work fine. :)
    Rarst.net - cynical thoughts on software and web (and sometimes WP) | @Rarst | I seem to be non-GPL-compliant person. Beware my poisonous thoughts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •