S
S
sk8er_boi2018-05-21 15:18:17
WordPress
sk8er_boi, 2018-05-21 15:18:17

Doesn't the shortcode work inside the attribute value?

Good day!
There is a page created in the WordPress engine. There is a processing of shortcodes. The same example shortcode is inserted in different parts of the page. The first time processing occurs, the second - no.

Page in engine

В шестой строке и в поле ввода фамилии (value) шорткод [get_errors]
<form data-name="fn_wp_insert_user" method="post">
  <table class="noborder">
    <tbody>
      <tr>
        <td class="aligncenter" colspan="2">
          <div id="response">[get_errors]</div></td>
      </tr>
      <tr>
        <td><label for="email">* Email</label></td>
        <td><input id="email" name="user_email" required="" type="email" autofocus="" data-required="1" /></td>
      </tr>
      <tr>
        <td><label for="password">* Пароль</label></td>
        <td><input id="password" name="user_pass" required="" type="password" data-required="1" /></td>
      </tr>
      <tr>
        <td><label for="confirm">* Повторите пароль</label></td>
        <td><input id="confirm" name="confirm" required="" type="password" data-required="1" /></td>
      </tr>
      <tr>
        <td><label for="last_name">* Фамилия</label></td>
        <td><input id="last_name" name="last_name" required="" type="text" data-required="1" value="[get_errors]" /></td>
      </tr>
      <tr>
        <td><label for="first_name">* Имя</label></td>
        <td><input id="first_name" name="first_name" required="" type="text" data-required="1" /></td>
      </tr>
      <tr>
        <td><input id="remember" name="remember" type="checkbox" /></td>
        <td><label for="remember">Запомнить меня</label></td>
      </tr>
      <tr>
        <td class="aligncenter" colspan="2">
          <div class="button"><button class="fa fa-unlock fa-padding-right-before" name="fn_action" type="submit" value="fn_wp_insert_user">Зарегистрироваться</button></div></td>
      </tr>
      <tr>
        <td class="aligncenter" colspan="2">
          <div class="button"><a class="fa fa-sign-in fa-padding-right-before" href="/user/login/" data-action="fn_get_page_login">Войти</a> <a class="fa fa-question fa-padding-right-before" href="/user/remind/" data-action="fn_get_page_remind">Забыли пароль?</a></div></td>
      </tr>
    </tbody>
  </table>
</form>

Code processing, for example
add_shortcode('get_errors', function($atts) {
  global $errors;
  $html = null;
  $error_list = (object)array('error' => null, 'message' => null, 'success' => null);
  foreach ($errors->error_data as $key => $value) {
    $error_list->$value[] = current($errors->errors[$key]); // $error_list = (object){'error' => {...}, 'message' => {...}, 'success' => {...}}
  }
  foreach ($error_list as $error_name => $error_class) {
    if ($error_class) {
      $html .= '<p class="' . $error_name . '">';
      foreach ($error_class as $error) {
        $html .= $error . '<br />';
      }
      $html .= '</p>';
    }
  }
  return $html;
});

Result
<form data-name="fn_wp_insert_user" method="post">
<table class="noborder">
<tbody>
<tr>
<td class="aligncenter" colspan="2">
<div id="response"><p class="error">Этот email-адрес уже используется. Пожалуйста, воспользуйтесь формой восстановления пароля или выберите другой адрес<br />Минимальная длина пароля - 8 символов<br />Поле &laquo;Фамилия&raquo; заполнено некорректно<br />Поле &laquo;Имя&raquo; заполнено некорректно<br /></p></div>
</td>
</tr>
<tr>
<td><label for="email">* Email</label></td>
<td><input id="email" name="user_email" required="" type="email" autofocus="" data-required="1" /></td>
</tr>
<tr>
<td><label for="password">* Пароль</label></td>
<td><input id="password" name="user_pass" required="" type="password" data-required="1" /></td>
</tr>
<tr>
<td><label for="confirm">* Повторите пароль</label></td>
<td><input id="confirm" name="confirm" required="" type="password" data-required="1" /></td>
</tr>
<tr>
<td><label for="last_name">* Фамилия</label></td>
<td><input id="last_name" name="last_name" required="" type="text" data-required="1" value="[get_errors]" /></td>
</tr>
<tr>
<td><label for="first_name">* Имя</label></td>
<td><input id="first_name" name="first_name" required="" type="text" data-required="1" /></td>
</tr>
<tr>
<td><input id="remember" name="remember" type="checkbox" /></td>
<td><label for="remember">Запомнить меня</label></td>
</tr>
<tr>
<td class="aligncenter" colspan="2">
<div class="button"><button class="fa fa-unlock fa-padding-right-before" name="fn_action" type="submit" value="fn_wp_insert_user">Зарегистрироваться</button></div>
</td>
</tr>
<tr>
<td class="aligncenter" colspan="2">
<div class="button"><a class="fa fa-sign-in fa-padding-right-before" href="/user/login/" data-action="fn_get_page_login">Войти</a> <a class="fa fa-question fa-padding-right-before" href="/user/remind/" data-action="fn_get_page_remind">Забыли пароль?</a></div>
</td>
</tr>
</tbody>
</table>
</form>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sk8er_boi, 2018-05-25
@sk8er_boi

The issue was resolved by generating the entire field via a shortcode. For example,

add_shortcode('get_nonce', function($atts) { // генерация nonce
  return '<input type="hidden" name="extra_fields_nonce" value="' . wp_create_nonce(__FILE__) . '" />';
});

E
Eugene, 2018-05-21
@iamd503

Not familiar with the engine, but look in functions.php where the shortcode is registered. Maybe there should be "show only in a specific template." Or something like that.
You can also see here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question