I have a custom plugin that moves posts from one category to another and then inserts text after the post is moved. This works fine except for I am trying to insert some CSS like you see below in the $start_text variable. The wp_update_post function is stripping out the <style type='text/css'> tags completely and treating the contents withing the style tags as simple text. How do I make sure those are kept in place when the function runs so the page uses the style I'm trying to import into the post?


$start_text="<style type='text/css'>#footerlink { display:none; }</style>Beginning Of posts\n";

The actual Wordpress function:

function mohit_cron_hook_action(){
$my_query22 = new WP_Query;
    global $start_text;
    global $catfrom;
    global $catto;
    global $dayago;

                    $old_cat = $catfrom;
                    $new_cat = absint($catto);
                    $posts   = $my_query22->query(array('category__in'=>$old_cat, 'post_type'=>'post', 'nopaging'=>'true','date_query' => array('column' => 'post_date_gmt','before'  => $age)));
                    foreach ($posts as $post) {

                        $current_cats = wp_get_post_categories($post->ID);
                        $current_cats = array_diff($current_cats, $old_cat);
                        if ($new_cat != -1) {
                            $current_cats[] = $new_cat;

                        if (count($current_cats) == 0) {
                            $current_cats = array(get_option('default_category'));
                        $current_cats = array_values($current_cats);
                        wp_update_post(array('ID'=>$post->ID,'post_category'=>$current_cats,'post_content'=>$start_text.get_post_field('post_content', $post->ID)));

  • 2
    This is a known feature, that originates from the current_user_can( 'unfiltered_html' ) check in the kses_init() function. This check fails for wp_cron calls when there's no user logged in. So try to search this site for possible workarounds, must be various methods available. But maybe you could solve this differently than inject an inline CSS into the post content? It sounds rather inflexible to do so!
    – birgire
    Feb 14, 2015 at 16:57
  • Got it, thanks. Yes, it's ugly but the application is very specific so it wont' require much additional after this is working. Feb 14, 2015 at 17:28
  • I did find this link which I am experimenting with, hopefully it may help someone if they come across the same issue: wordpress.org/support/topic/… Feb 14, 2015 at 18:14
  • Ok great. You can always add your own answer and accept it (external links may disappear in the future) ;-) But this might also be closed as duplicate?
    – birgire
    Feb 14, 2015 at 18:17
  • Why a duplicate @birgire? Does stackexchange count wordpress.org posts as duplicate posts? Feb 14, 2015 at 18:29


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.