Advertisement
Guest User

wp-stack-exchange-309235

a guest
Jul 27th, 2018
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.96 KB | None | 0 0
  1. <?php
  2. // See https://wordpress.stackexchange.com/q/309235/137402 for details.
  3.  
  4. add_action( 'admin_menu', 'my_add_admin_menu' );
  5. function my_add_admin_menu() {
  6.     // I used `add_options_page()`, but you can use similar function. The point
  7.     // is, we'd need the `$hook_suffix` variable.
  8.     $hook_suffix = add_options_page( 'WPSE 309235', 'WPSE 309235', 'manage_options', 'wpse-309235', 'my_options_page' );
  9.  
  10.     // Registers and enqueues our repeatable field script. You shouldn't "hotlink"
  11.     // to CodePen. Instead, save the file to my-repeatable-field.js and upload it
  12.     // to your site, and then link to that file.
  13.     add_action( 'load-' . $hook_suffix, function(){
  14.         wp_register_script( 'my-repeatable-field', 'https://codepen.io/anon/pen/vaJaGZ.js', [ 'jquery' ], '20180727' );
  15.         wp_enqueue_script( 'my-repeatable-field' );
  16.     } );
  17. }
  18.  
  19. add_action( 'admin_init', 'my_register_settings' );
  20. function my_register_settings() {
  21.     register_setting( 'my_group', 'my_options', 'my_validate' );
  22. }
  23.  
  24. function my_options_page() {
  25.   echo '<div class="wrap">';
  26.     echo '<h2>'.esc_html( get_admin_page_title() ).'</h2>';
  27.     echo '<h3>Setup / Settings</h3>';
  28.     //settings_errors(); // In my case, this isn't necessary.
  29.     echo '<form action="options.php" method="post">';
  30.       settings_fields("my_group");
  31.       echo '<table class="form-table repeatable-field">';
  32.         $phrases_arr = my_options( 'phrases' );
  33.         $count = count( $phrases_arr );
  34.  
  35.         echo '<tr valign="top"><th colspan="2">Data Input</th></tr>';
  36.         // Displays 3 initial rows.
  37.         $rows = max( $count, 3 );
  38.         for ( $i = 0, $j = 1; $i < $rows; $i++, $j++ ) {
  39.             $phrase_str = isset( $phrases_arr[ $i ] ) ? $phrases_arr[ $i ] : '';
  40.             echo '<tr valign="top" class="repeatable-field-row">' .
  41.                 '<th scope="row">Phrase [<span class="repeatable-field-number">' . $j . '</span>]</th>' .
  42.                 '<td><input name="my_options[phrases][]" value="' . esc_attr( $phrase_str ) . '" size="32" type="text" class="repeatable-field-input">' .
  43.                     '<span class="repeatable-field-buttons"></span></td>' .
  44.             '</tr>';
  45.         }
  46.  
  47.         $phrases_url = my_options( 'phrases_url' );
  48.         echo '<tr valign="top">' .
  49.             '<th>Phrases URL (*Non-repeatable* Field)</th>' .
  50.             '<td><input type="text" name="my_options[phrases_url]" value="' . esc_attr( $phrases_url ) . '" class="regular-text" placeholder="URL"></td>' .
  51.         '</tr>';
  52.       echo '</table>';
  53.       submit_button();
  54.     echo '</form>';
  55.   echo '</div>';
  56. }
  57.  
  58. function my_validate($input){
  59.   $phrases_arr = [];
  60.  
  61.   foreach ($input['phrases'] as $phrase) {
  62.     if ( $phrase = esc_html( $phrase ) ) {
  63.       $phrases_arr[] = $phrase;
  64.     }
  65.   }
  66.  
  67.   $input['phrases'] = $phrases_arr;
  68.  
  69.   return $input;
  70. }
  71.  
  72. function my_options( $key = null ) {
  73.     $options = wp_parse_args( get_option( 'my_options' ), [
  74.         'phrases'     => [],
  75.         'phrases_url' => '',
  76.     ] );
  77.  
  78.     // Returns a single option.
  79.     if ( $key ) {
  80.         return isset( $options[ $key ] ) ? $options[ $key ] : null;
  81.     }
  82.  
  83.     // Returns all the options.
  84.     return $options;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement