Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop filtering WordPress function wp_insert_post

Tags:

wordpress

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?

like image 991
Michael Nieuwenhuizen Avatar asked Dec 06 '13 12:12

Michael Nieuwenhuizen


Video Answer


1 Answers

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.
like image 130
Dharmang Avatar answered Oct 04 '22 01:10

Dharmang