Quantcast
Viewing all articles
Browse latest Browse all 3201

Mark Wilkinson on "Passing HTML, Inside XML as Post Content with wp_insert_post"

I am using an XML file which contains the data for adding a post in WordPress. An example of my file is below:

<?xml version="1.0" encoding="UTF-8"?>
<job>
	<username>xxx</username>
	<password>xxx</password>
	<command>add</command>
	<Area>London</Area>
	<Category>Industrial</Category>
	<Title>This is a Testing Job</Title>
	<Teaser>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In hendrerit elit at arcu consectetur, sed porttitor nisl auctor. Morbi condimentum feugiat ipsum, nec vehicula velit porta non.</Teaser>
	<StartDate></StartDate>
	<Details><h2>Heading 2 Title</h2><p>Content</p></Details>
	<Salary>30</Salary>
	<Salary_per>hour</Salary_per>
	<salary_currency>GBP</salary_currency>
	<Contact_email>email@email.com</Contact_email>
	<Application_email>email@email.com</Application_email>
	<Job_type>Temporary</Job_type>
	<unique>REF123456</unique>
	<advertlife>28</advertlife>
</job>

The details tag above is going to be used as the posts content. I am using the following to get the contents of the file and load all the tags into a variable as an array.

$wpbb_xml_content = file_get_contents( 'php://input' );

/* parse the retreived xml file */
$wpbb_params = json_decode( json_encode( simplexml_load_string( $wpbb_xml_content ) ), 1 );

Therefore my post content is now available as wpbb_params[ 'Details' ];

However when I try to use this in `wp_insert_post' like so:

$wpbb_job_post_args = array(
	'post_type' => 'wpbb_job',
	'post_title' => wp_strip_all_tags( $wpbb_params[ 'Title' ] ),
	'post_content' => $wpbb_params[ 'Details' ],
	'post_status' => 'publish'
);

$wpbb_job_post_id = wp_insert_post( $wpbb_job_post_args );

I get the following error message.

Warning: stripslashes() expects parameter 1 to be string, array given in /var/sites/l/xxxx/public_html/xxxxx/wp-includes/kses.php on line 1315

Warning: strip_tags() expects parameter 1 to be string, array given in /var/sites/l/xxxxx/public_html/xxxxx/wp-includes/formatting.php on line 3625

I think this is because the HTML tags inside the Details tag are making the details tag an array with each HTML entity being treated as a nested XML entity.

It is strange because I have been using this code for a while and it has worked no problem. I wonder if a change in sanitise_post() has been made.

Anyone any ideas how I can solve this? Any help would be greatly appreciated.


Viewing all articles
Browse latest Browse all 3201

Trending Articles