やりたいこと
wordpressの記事投稿時に公開・更新ボタンを押したら記事を取得し、htmlタグにstyle属性があったら取り除くということをやりたかった。
公開・更新ボタンを押したらstyle属性を取り除くのはできたが、
「変更をプレビュー」を押したときにはstyle属性が取り除けていなかった。
やったこと
コード
function.php
add_action( 'save_post', 'delete_style' ); function delete_style() { remove_action('save_post', 'delete_style'); if(!empty($_POST['publish']) || !empty($_POST['save'])){ $subject = stripslashes($_POST['post_content']); $pattern = '/style=".*"/'; $replacement = ""; $output = preg_replace( $pattern, $replacement, $subject ); $_POST['post_content'] = $output; wp_update_post($_POST); } add_action('save_post', 'delete_style'); }
このコードだと
公開・更新したときにしか動かない
試してみたこと
function.php
add_action( 'save_post', 'delete_style' ); function delete_style() { remove_action('save_post', 'delete_style'); if(!empty($_POST['publish']) || !empty($_POST['save'])){ $subject = stripslashes($_POST['post_content']); $pattern = '/style=".*"/'; $replacement = ""; $output = preg_replace( $pattern, $replacement, $subject ); $_POST['post_content'] = $output; wp_update_post($_POST); } else { //プレビュー時 $subject = stripslashes($_POST['content']); $pattern = '/style=".*"/'; $replacement = ""; $output = preg_replace( $pattern, $replacement, $subject ); $_POST['content'] = $output; wp_update_post($_POST); } add_action('save_post', 'delete_style'); }
これで記事内の方はstyle属性を取り除けたが、プレビュー画面ではstyle属性を取り除く前が表示されてしまっている。
これを解決するには何が足りないのでしょうか?教えてください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/07/26 04:50