###やりたいこと
wordpressでフロントエンドからカスタムフィールドの値を変更したいです。
###やってみたこと
下記コードは、カスタムフィールドcustom_fluits
の値を変更するフォームです。
この値はまずバックエンドで入力しておりまして、フロントエンドではフォームと現状の値が正しく出力されます。そこまでは問題ありません。
しかしフロントエンドのフォームでsubmit
時にupdate_post_meta
が効かないようで値が変更してくれず、行き詰っております。
php
1<?php if ( get_current_user_id() == 999 ) : ?> 2 3<?php 4 global $post; 5 6 if ( isset( $_POST['submit'] ) ) 7 { 8 if( ! isset( $post ) ) { 9 echo 'Error: Post Object Not Set'; 10 die(); 11 } 12 else if( ! isset( $_POST['custom_fluits'] ) && ! empty( $_POST['custom_fluits'] ) ){ 13 echo 'Error: custom_fluits Not Set'; 14 die(); 15 } 16 17 update_post_meta( $post->ID, 'custom_fluits', sanitize_text_field( $_POST['custom_fluits'] ) ); 18 } 19 20 $custom_fluits = get_post_meta($post->ID, 'custom_fluits', true); 21 22 echo print_r($custom_fluits); 23?> 24 25<form method="post" action=""> 26 <input type='text' name='custom_fluits' value='<?php echo isset($custom_fluits) ? $custom_fluits : ''; ?>' /> 27 <input type='submit' value='save' /> 28</form> 29 30<?php endif; ?>
原因がお分かりになる方がいらっしゃいましたら、修正点についてアドバイス頂戴できませんでしょうか。
また、もしセキュリティ面での不安点もございましたら併せてご指摘頂けますと幸いです。
宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/17 04:59 編集