-
Hey folks I'm struggling a bit catching the Here's my <?php
namespace SBX\Theme\Package;
class UserMeta
{
// Instantiate the WP_Error object
public $error = '';
public function __construct() {
$this->$error = new \WP_Error();
}
public function run() {
// Update user meta from profile/user/author pages
add_action( 'template_redirect', [$this, 'updateUserMetaFromFrontend'] );
}
// function to update meta fields from auhtor/profile pages
public function updateUserMetaFromFrontend() {
// Check if the user is authenticated.
if ( ! is_user_logged_in() ) {
return;
}
// Check if we have all necessary data.
if ( empty( $_POST['update_meta_nonce'] ) ) {
return;
}
// Verify the nonce.
if ( ! wp_verify_nonce( $_POST['update_meta_nonce'], 'update-meta' ) ) {
return;
}
// Update optional user meta
$this->updateField('nickname', true, 'Please enter a name.');
$this->updateField('city');
// Redirect back to the previous page.
wp_safe_redirect( wp_get_referer() );
exit;
}
public function updateField($field, $mandatory = false, $errormsg = null) {
if (!empty($_POST[$field]) && is_string($_POST[$field])) {
update_user_meta( get_current_user_id(), $field, htmlspecialchars($_POST[$field]) );
}else {
if($mandatory) {
$this->$error->add('general', '<strong>' . __('ERROR') . '</strong>: ' . $errormsg, array('form-field' => $field));
}
}
}
} My <?php
$context = Timber::context();
$context['errors'] = is_wp_error($error);
Timber::render( array( 'author.twig'), $context ); And my {% if errors %}
<pre>
{{ dump(errors) }}
</pre>
{% endif %}
<form method="post" action="{{ site.url }}" id="profile-edit-form">
<fieldset class="ProfileEdit__accounts">
<label>
<span>Name<small class="ProfileEdit__required-note">required</small></span>
<input
type="text"
name="nickname"
placeholder="Enter your name"
value="{{user.nickname}}"
autocomplete="off"
required>
</label>
<label>
<span>City</span>
<input
type="text"
name="city"
placeholder="Your City"
value="{{user.account_city}}"
autocomplete="off">
</label>
</fieldset>
{% do function('wp_nonce_field', 'update-meta', 'update_meta_nonce') %}
<button type="submit" class="submit">Update profile</button>
</form> So the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I see a couple of things. I think the following line in author.php might be the first issue: $context['errors'] = is_wp_error($error); I don’t see where the The Also, I don’t see where you actually instantiate the author.php <?php
$user_meta = new UserMeta();
$context = Timber::context();
$context['errors'] = is_wp_error( $user_meta->get_error() );
Timber::render( array( 'author.twig'), $context ); But then, your You then have to make sure that the form loads the same page instead of redirecting to the home page. Check the
You could maybe use a <?php
$user_meta = new UserMeta();
$user_meta->updateUserMetaFromFrontend();
$context = Timber::context();
$context['errors'] = $user_meta->get_errors();
Timber::render( array( 'author.twig'), $context ); To iterate further, you could also add <?php
$user_meta = new UserMeta();
$user_meta->updateUserMetaFromFrontend();
$context = Timber::context();
$context['user_meta'] = $user_meta;
Timber::render( array( 'author.twig'), $context ); In Twig, you could then use: {% if user_meta.get_errors is not empty %}
<pre>
{{ dump(user_meta.get_errors) }}
</pre>
{% endif %} Then, the next problem is when you create an empty public function __construct() {
$this->$error = new \WP_Error();
} The I hope that helps you to get this working |
Beta Was this translation helpful? Give feedback.
-
Hey @gchtr! Thanks for the detailed answer. Checking for errors with Thanks and Gruess us Bärn 👋 |
Beta Was this translation helpful? Give feedback.
I see a couple of things. I think the following line in author.php might be the first issue:
I don’t see where the
$error
variable is coming from. Did you only publish a part of the template file, or is that it?The
errors
variable in Twig is always false, because$error
is always undefined andis_wp_error()
returns only true if$error
is an instance ofWP_Error
.Also, I don’t see where you actually instantiate the
UserMeta
class. The template file doesn’t automatically have access to theUserMeta
class. You would have to instantiate it there. I’m thinking something like this might work:author.php