{"id":12200548,"date":"2020-11-19T18:09:31","date_gmt":"2020-11-19T15:09:31","guid":{"rendered":"https:\/\/ru.wordpress.org\/support\/topic\/%d0%ba%d0%b0%d0%ba-%d0%b2%d0%b5%d1%80%d0%bd%d0%be-%d1%81%d0%be%d1%85%d1%80%d0%b0%d0%bd%d0%b8%d1%82%d1%8c-%d0%b2-%d0%b1%d0%b4-repeated-%d0%b3%d1%80%d1%83%d0%bf%d0%bf%d1%83-%d0%bf%d0%be%d0%bb%d0%b5\/"},"modified":"2020-11-19T18:09:31","modified_gmt":"2020-11-19T15:09:31","slug":"%d0%ba%d0%b0%d0%ba-%d0%b2%d0%b5%d1%80%d0%bd%d0%be-%d1%81%d0%be%d1%85%d1%80%d0%b0%d0%bd%d0%b8%d1%82%d1%8c-%d0%b2-%d0%b1%d0%b4-repeated-%d0%b3%d1%80%d1%83%d0%bf%d0%bf%d1%83-%d0%bf%d0%be%d0%bb%d0%b5","status":"publish","type":"topic","link":"https:\/\/ru.wordpress.org\/support\/topic\/%d0%ba%d0%b0%d0%ba-%d0%b2%d0%b5%d1%80%d0%bd%d0%be-%d1%81%d0%be%d1%85%d1%80%d0%b0%d0%bd%d0%b8%d1%82%d1%8c-%d0%b2-%d0%b1%d0%b4-repeated-%d0%b3%d1%80%d1%83%d0%bf%d0%bf%d1%83-%d0%bf%d0%be%d0%bb%d0%b5\/","title":{"rendered":"\u041a\u0430\u043a \u0432\u0435\u0440\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u0411\u0414 repeated-\u0433\u0440\u0443\u043f\u043f\u0443-\u043f\u043e\u043b\u0435\u0439?"},"content":{"rendered":"<p>\u0417\u0430\u0434\u0430\u0447\u0430 \u0442\u0430\u043a\u0430\u044f &#8212; \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0435\u0442\u0430-\u0431\u043e\u043a\u0441, \u0433\u0434\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u043e\u043b\u044f\u0445, \u0442\u043e \u0435\u0441\u0442\u044c \u0433\u0440\u0443\u043f\u043f\u0430 \u043f\u043e\u043b\u0435\u0439, \u0447\u0442\u043e\u0431\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u043d\u0435\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0435 \u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u044b.<\/p>\n<p>\u0422\u0430\u043a\u0430\u044f \u0433\u0440\u0443\u043f\u043f\u0430:<br \/>\nval1: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 | val2: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 | val3: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p>\n<p>\u0418 \u0435\u0451 \u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435:<br \/>\nval1: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 | val2: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 | val3: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<br \/>\nval1: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 | val2: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 | val3: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<br \/>\nval1: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 | val2: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 | val3: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435&#8230;<\/p>\n<p>\u0423 \u043c\u0435\u043d\u044f \u0435\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043c\u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043c\u0430\u0441\u0441\u0438\u0432. \u0412\u043a\u0440\u0430\u0442\u0446\u0435, \u0441\u0430\u043c\u043e \u043f\u043e\u043b\u0435 \u0432 \u0430\u0434\u043c\u0438\u043d\u043a\u0435 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0442\u0430\u043a:<\/p>\n<pre><code>$input = &#039;\n          &lt;span class=&quot;item-personnel&quot;&gt;\n            &lt;input type=&quot;text&quot; name=&quot;&#039;. self::$meta_key .&#039;[]&quot; value=&quot;%s&quot;&gt;\n            &lt;span class=&quot;dashicons dashicons-trash remove-personnel-item&quot;&gt;&lt;\/span&gt;\n          &lt;\/span&gt;\n          &#039;;<\/code><\/pre>\n<p>\u041d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code>$input = &#039;\n  &lt;span class=&quot;item-personnel&quot;&gt;\n    VAL1 &lt;input type=&quot;text&quot; name=&quot;&#039;. self::$meta_key .&#039;[val1]&quot; value=&quot;%s&quot;&gt;\n    VAL2 &lt;input type=&quot;text&quot; name=&quot;&#039;. self::$meta_key .&#039;[val2]&quot; value=&quot;%s&quot;&gt;\n    VAL3 &lt;input type=&quot;text&quot; name=&quot;&#039;. self::$meta_key .&#039;[val3]&quot; value=&quot;%s&quot;&gt;\n  &lt;span class=&quot;dashicons dashicons-trash remove-personnel-item&quot;&gt;&lt;\/span&gt;\n&lt;\/span&gt;\n&#039;;<\/code><\/pre>\n<p>\u042f \u0434\u0443\u043c\u0430\u044e, \u0432\u0441\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043c\u0435\u0442\u0430-\u0431\u043e\u043a\u0441\u0430. \u0421\u0435\u0439\u0447\u0430\u0441 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code> if ( wp_is_post_autosave( $post_id ) )\n      return;\n\n    if ( isset( $_POST[self::$meta_key] ) &amp;&amp; is_array( $_POST[self::$meta_key] ) ) {\n      $addresses = $_POST[self::$meta_key];\n\n      $addresses = array_map( &#039;sanitize_text_field&#039;, $addresses );\n\n      $addresses = array_filter( $addresses );\n\n      if ( $addresses ) \n        update_post_meta( $post_id, self::$meta_key, $addresses );\n      else \n        delete_post_meta( $post_id, self::$meta_key );\n\n    }\n  }<\/code><\/pre>\n<p>\u0427\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 (\u0433\u0440\u0443\u043f\u043f \u043f\u043e\u043b\u0435\u0439)?<\/p>\n<p>\u0410 \u0432\u043e\u0442 \u0432\u0441\u044f \u043c\u043e\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0446\u0435\u043b\u0438\u043a\u043e\u043c:<\/p>\n<pre><code>new My_Custom_Fields;\n\nclass My_Custom_Fields {\n\n  public $post_type = &#039;page&#039;;\n\n  static $meta_key = &#039;my_meta_key&#039;;\n\n  public function __construct() {\n    add_action( &#039;add_meta_boxes&#039;, array( $this, &#039;add_metabox&#039; ) );\n    add_action( &#039;save_post_&#039; . $this-&gt;post_type, array( $this, &#039;save_metabox&#039; ) );\n    add_action( &#039;admin_print_footer_scripts&#039;, array( $this, &#039;show_assets&#039; ), 10, 999 );\n  }\n\n  public function add_metabox() {\n    add_meta_box( &#039;my_meta_key_form&#039;, &#039;my_meta_key&#039;, array( $this, &#039;render_metabox&#039; ), $this-&gt;post_type, &#039;advanced&#039;, &#039;high&#039; );\n  }\n\n  public function render_metabox( $post ) {\n\n    ?&gt;\n    &lt;table class=&quot;form-table my_meta_key_form&quot;&gt;\n\n      &lt;tr&gt;\n        &lt;th&gt;\n          My Meta Box &lt;span class=&quot;dashicons dashicons-plus-alt add-personnel&quot;&gt;&lt;\/span&gt;\n        &lt;\/th&gt;\n        &lt;td class=&quot;personnel-list&quot;&gt;\n          &lt;?php\n          $input = &#039;\n          &lt;span class=&quot;item-personnel&quot;&gt;\n            &lt;input type=&quot;text&quot; name=&quot;&#039;. self::$meta_key .&#039;[]&quot; value=&quot;%s&quot;&gt;\n            &lt;span class=&quot;dashicons dashicons-trash remove-personnel-item&quot;&gt;&lt;\/span&gt;\n          &lt;\/span&gt;\n          &#039;;\n\n          $addresses = get_post_meta( $post-&gt;ID, &#039;personnel&#039;, true );\n\n          if ( is_array( $addresses ) ) {\n            foreach ( $addresses as $addr ) {\n              printf( $input, esc_attr( $addr ) );\n            }\n          } else {\n            printf( $input, &#039;&#039; );\n          }\n          ?&gt;\n        &lt;\/td&gt;\n      &lt;\/tr&gt;\n\n    &lt;\/table&gt;\n\n    &lt;?php\n  }\n\n  public function save_metabox( $post_id ) {\n\n    if ( wp_is_post_autosave( $post_id ) )\n      return;\n\n    if ( isset( $_POST[self::$meta_key] ) &amp;&amp; is_array( $_POST[self::$meta_key] ) ) {\n      $addresses = $_POST[self::$meta_key];\n\n      $addresses = array_map( &#039;sanitize_text_field&#039;, $addresses );\n\n      $addresses = array_filter( $addresses );\n\n      if ( $addresses ) \n        update_post_meta( $post_id, self::$meta_key, $addresses );\n      else \n        delete_post_meta( $post_id, self::$meta_key );\n\n    }\n  }\n\n  public function show_assets() {\n    if ( is_admin() &amp;&amp; get_current_screen()-&gt;id == $this-&gt;post_type ) {\n      $this-&gt;show_styles();\n      $this-&gt;show_scripts();\n    }\n  }\n\n  public function show_styles() {\n    ?&gt;\n    &lt;style&gt;\n      .add-personnel {\n        color: #00a0d2;\n        cursor: pointer;\n      }\n      .personnel-list .item-personnel {\n        display: flex;\n        align-items: center;\n      }\n      .personnel-list .item-personnel input {\n        width: 100%;\n        max-width: 400px;\n      }\n      .remove-personnel-item {\n        color: brown;\n        cursor: pointer;\n      }\n    &lt;\/style&gt;\n    &lt;?php\n  }\n\n  public function show_scripts() {\n    ?&gt;\n    &lt;script&gt;\n      jQuery(document).ready(function ($) {\n\n        var $companyInfo = $(&#039;.album-personnel&#039;);\n\n        $(&#039;.add-personnel&#039;, $companyInfo).click(function () {\n          var $list = $(&#039;.personnel-list&#039;);\n            $item = $list.find(&#039;.item-personnel&#039;).first().clone();\n\n          $item.find(&#039;input&#039;).val(&#039;&#039;);\n\n          $list.append( $item );\n        });\n\n        $companyInfo.on(&#039;click&#039;, &#039;.remove-personnel-item&#039;, function () {\n          if ($(&#039;.item-personnel&#039;).length &gt; 1) {\n            $(this).closest(&#039;.item-personnel&#039;).remove();\n          }\n          else {\n            $(this).closest(&#039;.item-personnel&#039;).find(&#039;input&#039;).val(&#039;&#039;);\n          }\n        });\n\n      });\n    &lt;\/script&gt;\n    &lt;?php\n  }\n\n}<\/code><\/pre>\n","protected":false},"template":"","class_list":["post-12200548","topic","type-topic","status-publish","hentry","topic-tag-data-base","topic-tag-meta-box","topic-tag-php"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ru.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/12200548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ru.wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/ru.wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/ru.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/12200548\/revisions"}],"wp:attachment":[{"href":"https:\/\/ru.wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=12200548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}