Hi I’m using the smarty templating engine in wordpress to generate HTML like the one below:
<li>
<div class="checkbox_container" data-asin="B0009Y7APU"><input type="checkbox" class="ecom_compare_products" data-asin="B0009Y7APU" value="B0009Y7APU"/>
</div>
<div class="small_img_container"><img class="related_product_image" src="https://wordpress.stackexchange.com/questions/97303/img.jpg" alt="Case Logic JDS-2 USB Drive Shuttle 2-Capacity (Black/Blue)">
</div>
<div class="title_container"><a href="">Case Logic JDS-2 USB Drive Shuttle 2-Capacity (Black/Blue)</a>
</div>
</li>
The problem is that it seems like WordPress is removing the data attributes from this markup when the post is published. The markup above is what I actually got when I sent an email to myself.
$post_content = $smarty->fetch( 'product_detail.tpl' );
wp_mail('[email protected]', 'debug posting of products', 'content: ' . $post_content);
$post = array(
'post_title' => $item_name,
'post_content' => $post_content,
'post_type' => 'post',
'tags_input' => $amazon_keywords,
'comment_status' => $allow_comments,
'ping_status' => $allow_pingbacks,
'post_status' => 'publish'
);
wp_insert_post($post);
But when I edited the post that was published here’s what I got:
<li>
<div class="checkbox_container"></div><div class="small_img_container">
<img class="related_product_image" src="https://wordpress.stackexchange.com/questions/97303/img.jpg" alt="Case Logic JDS-2 USB Drive Shuttle 2-Capacity (Black/Blue)">
</div>
<div class="title_container">
<a href="">Case Logic JDS-2 USB Drive Shuttle 2-Capacity (Black/Blue)</a>
</div>
</li>
Its really weird. I don’t have an idea how it turned out to be like this.
But what’s even weirder is that this only happens when the post is published using a scheduled event which I trigger by calling it from AJAX. The code for the mons_post_product is the actual publishing of the post.
add_action('ecom_scheduler', 'mons_post_product');
function ecom_schedule_event(){
wp_schedule_single_event(time(), 'mons_scheduler');
}
add_action('wp_ajax_schedule', 'mons_schedule_event');
add_action('wp_ajax_nopriv_schedule', 'mons_schedule_event');
When hook up the method in the admin_menu there’s no problem:
add_action('admin_menu', function(){
mons_post_product();
});
Any ideas?
2 Answers
Found the answer. I hope this would be useful to others that might have this problem in the future. All you have to do is remove the content_save_pre
filter and content_filtered_save_pre
this will remove all Kses input form content filters.
//temporarily disable
remove_filter('content_save_pre', 'wp_filter_post_kses');
remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
wp_update_post($post);
//bring it back once you're done posting
add_filter('content_save_pre', 'wp_filter_post_kses');
add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
This works with wp_insert_post
as well.