WordPressでプラグインを作成しています。
プラグイン画面にて入力したテキストを固定ページにfor文で連続で表示させたいです。
躓いているのはプラグインのphpで function get_up_1() ~ function get_up_30() までをfor文で定義できないのか、というところと、
固定ページのphpでプラグインの関数を呼び出す際、for文中に .esc_html($showtext->get_up_.$num()). のように書いても呼び出せないという部分です。
なにかいい方法あればご教示願います。
固定ページ
php
1 <select id="select-up" name="a"> 2 <option class='select-color select-box-up-1' value=".radio-select-up-1" name='color-up' data-target='.bg-up-1' data-target-class='[class^=bg-up]' selected><?php echo esc_html($showtext->get_up_1()); ?> 3 <p></p> 4 </option> 5 <?php 6 for ($num = 2; $num < 31; $num++) { 7 echo "<option class='select-color select-box-up-$num' value='.radio-select-up-$num' name='color-up' data-target='.bg-up-$num' data-target-class='[class^=bg-up]'>".esc_html($showtext->get_up_.$num())."</option>"; 8 }; 9 ?> 10 </select>
プラグイン
php
1<?php 2/* 3Plugin Name: Color Name 4Plugin URI: http://www.example.com/plugin 5Description: テキストを表示するだけのプラグイン 6Author: my name 7Version: 0.1 8Author URI: http://www.example.com 9*/ 10 11class ShowText 12{ 13 function __construct() 14 { 15 add_action('admin_menu', array($this, 'add_pages')); 16 } 17 function add_pages() 18 { 19 add_menu_page('テキスト設定', 'テキスト設定', 'level_8', __FILE__, array($this, 'show_text_option_page'), '', 26); 20 } 21 function show_text_option_page() 22 { 23 //オブション画面に表示する内容 24 25 //$_POST['showtext_options'])があったら保存 26 if (isset($_POST['showtext_options'])) { 27 check_admin_referer('shoptions'); 28 $opt = $_POST['showtext_options']; 29 update_option('showtext_options', $opt); ?> 30 <div class="updated fade"> 31 <p><strong><?php _e('Options saved.'); ?></strong></p> 32 </div><?php 33 } 34 ?> 35 <div class="wrap"> 36 <div id="icon-options-general" class="icon32"><br /></div> 37 <h2>カラーネーム設定</h2> 38 <form action="" method="post"> 39 <?php 40 wp_nonce_field('shoptions'); 41 $opt = get_option('showtext_options'); 42 for ($num = 1; $num < 31; $num++) { 43 ${"show_text" . $num} = isset($opt['text'.$num]) ? $opt['text'.$num] : null; 44 }; 45 ?> 46 <?php 47 for ($num = 1; $num < 31; $num++) { 48 echo "<table class='form-table'> 49 <tr valign='top'> 50 <th scope='row'><label for='inputtext'>バンドの色$num</label></th> 51 <td><input name='showtext_options[text$num]' type='text' id='inputtext' value='${"show_text" .$num}' class='regular-text' /></td> 52 </tr> 53 </table>"; 54 }; 55 ?> 56 <p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存" /></p> 57 </form> 58 <!-- /.wrap --> 59 </div> 60<?php 61 } 62 63 function get_up_1() 64 { 65 $opt = get_option('showtext_options'); 66 return isset($opt['text1']) ? $opt['text1'] : null; 67 } 68 69 function get_up_2() 70 { 71 $opt = get_option('showtext_options'); 72 return isset($opt['text3']) ? $opt['text3'] : null; 73 } 74 75 function get_down_1() 76 { 77 $opt = get_option('showtext_options'); 78 return isset($opt['text2']) ? $opt['text2'] : null; 79 } 80 81 function get_down_2() 82 { 83 $opt = get_option('showtext_options'); 84 return isset($opt['text4']) ? $opt['text4'] : null; 85 } 86} 87$showtext = new ShowText; 88
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/02/10 11:53
2022/02/10 15:37