Stop filtering WordPress function wp_insert_post

2020-04-05 17:56发布

问题:

I have to make a few minor changes to a website built in WordPress, and I came across some problems with the wp_insert_content function. This function sanatizes input, taking some HTML tags completely out (like ) and deleting attributes on others. I would like to switch this off, and everywhere I find a link to http://pp19dd.com/2010/06/unfiltered-wp_insert_post/ where the solution is to add

'filter' => true

to the posts input array.

This article is from 2010 though, and from what I found an extra line was added in 2011 (http://web.archiveorange.com/archive/v/TDTh42SUwDEc1GFmSrvU). This line reads:

unset( $postarr[ 'filter' ] );

and is called right after merging the input with the defaults, and before sanitizing. It seems to me that this line cancels the 'filter' => true statement above. Indeed, sanitizing happens if the line is there, and disappears if the line is taken out.

So the easy solution would be to add the 'filter' => true and delete the extra line in the function. Problem though is that I have no idea where else the function is used, and I wonder if it's smart at all to hack right in the WP code. An update of WP would restore it anyway. So ... is there any other way to stop this function from sanitizing the input?

回答1:

It is possible that some other plugin may be trying to sanitize the content, in such case remove all the filters is the only option, try the following:

remove_all_filters("content_save_pre");
$post_id = wp_insert_post($post);

Other possible filter tags are:

  • pre_content
  • pre_post_content
  • content_pre Try them one by one also if it does not work.


回答2:

i'm also in the process of importing posts with s in the content body. i've been searching all morning for a solution, there have been some discussions on the topic. so far

kses_remove_filters();

has done nothing for me, neither did commenting these two lines:

unset( $postarr[ 'filter' ] ); // overrides 'filter' => true since 3.0 i guess
$postarr = sanitize_post($postarr, 'db');

in wp_insert_post() in post.php (defaults at line 2704).

there's another interesting bit at line 2874:

$data = apply_filters('wp_insert_post_data', $data, $postarr);

which seems like could be the source of our troubles, but i'm too much of a newbie to figure this out on my own.

really, truly, desperately need help! oh, and i can't comment, sorry for the spam action. i would like to keep this thread open and find a solution.

I guess it worked before i posted, but the content i was importing already had HTML tags stripped
the easiest workaround would be to manually overwrite the content after inserting the post, but this only works for isolated environments or for importing

$wpdb->update($wpdb->posts, array("post_content"=>$content->content), array("ID"=>$postid), array("%s"), array("%d"));

anyhow,

remove_all_filters("content_save_pre");

did the trick!



标签: wordpress