前提・実現したいこと
WordPressで記事修正した際に、更新日を変更しない設定を取り入れようとしていました。あるサイトから目的のコードが見つかり、それをfunctions.phpにコピペした時に以下のエラーが発生しました。
発生している問題・エラーメッセージ
wp-content/themes/jstork_custom/functions.php ファイルがエラーのため、PHP コードの変更をロールバックしました。修正し、もう一度保存してください。 syntax error, unexpected '$level' (T_VARIABLE)
該当のソースコード
//管理画面が開いたときに実行 add_action( 'admin_menu', 'add_update_level_custom_box' ); //更新ボタンが押されたときに実行 add_action( 'save_post', 'save_custom_field_postdata' ); //カスタムフィールドを投稿画面に追加 function add_update_level_custom_box() { //ページ編集画面にカスタムメタボックスを追加 add_meta_box( 'update_level', '更新日の変更', 'html_update_level_custom_box', 'post', 'side', 'high' ); } //投稿画面に表示するフォームのHTMLソース function html_update_level_custom_box() { $post = isset($_GET['post']) ? $_GET['post'] :null; $update_level = get_post_meta( $post, 'update_level' ); $level = $update_level ? $update_level[0] : null; echo '<div style="padding-top: 3px; overflow: hidden;">'; echo '<div style="width: 100px; float: left;"><input name="update_level" type="radio" value="high" '; if( $level=="" || $level=="high" ) echo ' checked="checked"'; echo ' />更新する</div><div><input name="update_level" type="radio" value="low" '; if( $level=="low" ) echo ' checked="checked"'; echo '/>更新しない<br /></div>'; echo '<p class="howto" style="margin-top:1em;">更新日時を変更するかどうかを設定します。誤字修正などで更新日を変更したくない場合は「変更しない」にチェックを入れてください。</p>'; echo '</div>'; } //設定したカスタムフィールドの値をDBに書き込む記述 function save_custom_field_postdata( $post_id ) { $mydata = isset($_POST['update_level']) ? $_POST['update_level'] : null; if( "" == get_post_meta( $post_id, 'update_level' )) { /* update_levelというキーでデータが保存されていなかった場合、新しく保存 */ add_post_meta( $post_id, 'update_level', $mydata, true ) ; } elseif( $mydata != get_post_meta( $post_id, 'update_level' )) { /* update_levelというキーのデータと、現在のデータが不一致の場合、更新 */ update_post_meta( $post_id, 'update_level', $mydata ) ; } elseif( "" == $mydata ) { /* 現在のデータが無い場合、update_levelというキーの値を削除 */ delete_post_meta( $post_id, 'update_level' ) ; } } //「更新」以外は更新日時を変更しない add_filter( 'wp_insert_post_data', 'my_insert_post_data', 10, 2 ); function my_insert_post_data( $data, $postarr ){ $mydata = isset($_POST['update_level']) ? $_POST['update_level'] : null; if( $mydata == "low" ){ unset( $data["post_modified"] ); unset( $data["post_modified_gmt"] ); } return $data; }
試したこと
私は全くの初心者でサイトにはコピペするだけでo.kと書かれていたので、その通り実行しましたがエラーとなってしまいました。ご教示いただきますようお願いいたします。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー