最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp insert post - How can I stop wp_update_post messing up HTML example code?

programmeradmin2浏览0评论

I'm showing little pieces of example HTML/PHP code on the frontend (with syntax highlighting). The entries are displayed as FAQ custom posts, and have a plugin (CMS Tree page) to alter the menu order, at which these are displayed. Whenever I change the order, it calls a wp_update_post, updating ID, menu_order, post_parent, post_type. For debug, I output the post_content property right before updating, and right after, and it gets changed in a horrible way.

Before (correct):

[html light="true"]</body>[/html]

After (messy):

[html light="true"]</body>[/html]

If I do it a couple of times it'll become living hell to revert:

[html light="true"]</body>[/html]

It looks exactly like htmlspecialchars() with ENT_NOQUOTES has been cast on it...

Please note that these are from the "source code" view and not the frontend. The browsers correctly handle the "Before" state, and render it as

</body>

How can I prevent it going messy?

I'm showing little pieces of example HTML/PHP code on the frontend (with syntax highlighting). The entries are displayed as FAQ custom posts, and have a plugin (CMS Tree page) to alter the menu order, at which these are displayed. Whenever I change the order, it calls a wp_update_post, updating ID, menu_order, post_parent, post_type. For debug, I output the post_content property right before updating, and right after, and it gets changed in a horrible way.

Before (correct):

[html light="true"]&lt;/body&gt;[/html]

After (messy):

[html light="true"]&amp;lt;/body&amp;gt;[/html]

If I do it a couple of times it'll become living hell to revert:

[html light="true"]&amp;amp;amp;lt;/body&amp;amp;amp;gt;[/html]

It looks exactly like htmlspecialchars() with ENT_NOQUOTES has been cast on it...

Please note that these are from the "source code" view and not the frontend. The browsers correctly handle the "Before" state, and render it as

</body>

How can I prevent it going messy?

Share Improve this question edited Jun 3, 2015 at 19:13 Firsh - justifiedgrid asked Jun 3, 2015 at 18:59 Firsh - justifiedgridFirsh - justifiedgrid 5892 gold badges6 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

It was https://wordpress/plugins/syntaxhighlighter/ plugin's fault. There are some functions in it that have something to do with this, namely encode_shortcode_contents_slashed_noquickedit, encode_shortcode_contents_callback.

Now I replaced it to https://wordpress/plugins/crayon-syntax-highlighter/ but by the time I confirmed it was the other plugin's fault I had already written a solution.

function wpse190396_insert_post_data($data, $postarr){
    if($postarr['filter'] == 'db'
        && ($data['post_type'] == 'fix' || $data['post_type'] == 'faq')
        && (strpos($data['post_content'],'&amp;lt;') !== false
            || strpos($data['post_content'],'&amp;nbsp;') !== false
            || strpos($data['post_content'],'&amp;gt;') !== false
            )){
        $data['post_content'] = htmlspecialchars_decode($data['post_content']);
    }
    return $data;
}

add_action( 'wp_insert_post_data', 'wpse190396_insert_post_data', 10, 2 );
发布评论

评论列表(0)

  1. 暂无评论