[wp-meta] [Making WordPress.org] #7175: User Meta for Memorial Accounts
Making WordPress.org
noreply at wordpress.org
Wed May 29 11:32:44 UTC 2024
#7175: User Meta for Memorial Accounts
----------------------------+---------------------
Reporter: mrfoxtalbot | Owner: (none)
Type: task (blessed) | Status: closed
Priority: normal | Milestone:
Component: General | Resolution: fixed
Keywords: |
----------------------------+---------------------
Comment (by networthnexz):
**To implement memorial profiles in WordPress, follow these steps:
Requirements**
==
Checkbox: "This is a memorial profile" in user profile settings.
Additional Fields: Displayed only if the checkbox is checked:
Date of Passing
Memorial Text
Memorial URL
Admin Access: Only Super Admins can view and edit these fields.
Display on Remembers Page: List all memorial profiles dynamically.
**Implementation**
==
Add Fields in User Profile:
{{{#!php
<?php
function add_memorial_fields($user) {
if (current_user_can('manage_options')) {
$is_memorial = get_user_meta($user->ID, 'is_memorial', true);
?>
<h3>Memorial Profile</h3>
<table class="form-table">
<tr>
<th><label for="is_memorial">This is a memorial
profile</label></th>
<td><input type="checkbox" name="is_memorial"
id="is_memorial" <?php checked($is_memorial, 'yes'); ?> /></td>
</tr>
<?php if ($is_memorial == 'yes') { ?>
<tr>
<th><label for="date_of_passing">Date of
Passing</label></th>
<td><input type="date" name="date_of_passing"
value="<?php echo esc_attr(get_user_meta($user->ID, 'date_of_passing',
true)); ?>" /></td>
</tr>
<tr>
<th><label for="memorial_text">Memorial
Text</label></th>
<td><textarea name="memorial_text"><?php echo
esc_textarea(get_user_meta($user->ID, 'memorial_text', true));
?></textarea></td>
</tr>
<tr>
<th><label for="memorial_url">Memorial
URL</label></th>
<td><input type="url" name="memorial_url" value="<?php
echo esc_url(get_user_meta($user->ID, 'memorial_url', true)); ?>" /></td>
</tr>
<?php } ?>
</table>
<?php
}
}
add_action('show_user_profile', 'add_memorial_fields');
add_action('edit_user_profile', 'add_memorial_fields');
function save_memorial_fields($user_id) {
if (current_user_can('manage_options')) {
update_user_meta($user_id, 'is_memorial',
isset($_POST['is_memorial']) ? 'yes' : 'no');
if (isset($_POST['is_memorial'])) {
update_user_meta($user_id, 'date_of_passing',
sanitize_text_field($_POST['date_of_passing']));
update_user_meta($user_id, 'memorial_text',
sanitize_textarea_field($_POST['memorial_text']));
update_user_meta($user_id, 'memorial_url',
esc_url($_POST['memorial_url']));
} else {
delete_user_meta($user_id, 'date_of_passing');
delete_user_meta($user_id, 'memorial_text');
delete_user_meta($user_id, 'memorial_url');
}
}
}
add_action('personal_options_update', 'save_memorial_fields');
add_action('edit_user_profile_update', 'save_memorial_fields');
}}}
**Display Memorial Profiles on the Remembers Page:
**
==
{{{#!php
<?php
function display_memorial_profiles() {
$args = array(
'meta_key' => 'is_memorial',
'meta_value' => 'yes',
'number' => -1
);
$memorial_users = get_users($args);
echo '<div class="memorial-profiles">';
foreach ($memorial_users as $user) {
$date_of_passing = get_user_meta($user->ID, 'date_of_passing',
true);
$memorial_text = get_user_meta($user->ID, 'memorial_text', true);
$memorial_url = get_user_meta($user->ID, 'memorial_url', true);
echo '<div class="memorial-profile">';
echo '<h2>' . esc_html($user->display_name) . '</h2>';
echo '<p>Date of Passing: ' . esc_html($date_of_passing) . '</p>';
echo '<p>' . esc_html($memorial_text) . '</p>';
if ($memorial_url) {
echo '<p><a href="' . esc_url($memorial_url) . '">More
Info</a></p>';
}
echo '</div>';
}
echo '</div>';
}
add_shortcode('memorial_profiles', 'display_memorial_profiles');
}}}
Use the Shortcode on the Remembers Page:
{{{#!php
<?php
[memorial_profiles]
}}}
**Testing**
==
Permissions: Ensure only Super Admins can see and edit fields.
Functionality: Verify fields appear and save correctly.
Display: Check that memorial profiles show up on the Remembers page.
--
Ticket URL: <https://meta.trac.wordpress.org/ticket/7175#comment:8>
Making WordPress.org <https://meta.trac.wordpress.org/>
Making WordPress.org
More information about the wp-meta
mailing list