Custom Link User Profile
February 21, 2025 · AI Conversations
Add custom link to this page in wordpress wp admin by hook
Here’s how you can add a custom link to the user profile page:
### Solution:
Add the following code to your theme’s `functions.php` file or a custom plugin:
function add_custom_link_to_user_profile($user) {
if (!current_user_can('edit_users')) {
return;
}
$user_id = $user->ID;
$custom_url = admin_url("admin.php?page=custom-page&user_id={$user_id}");
echo '<h2>Custom Actions</h2>';
echo '<table class="form-table">
<tr>
<th>Custom Link</th>
<td>
<a href="' . esc_url($custom_url) . '" class="button button-primary" target="_blank">Go to Custom Page</a>
</td>
</tr>
</table>';
}
add_action('edit_user_profile', 'add_custom_link_to_user_profile');
add_action('show_user_profile', 'add_custom_link_to_user_profile');
### Explanation:
– This hook adds a **"Custom Actions"** section to the user profile page.
– It generates a **custom link button** that redirects to `admin.php?page=custom-page&user_id=75166`.
– The link dynamically includes the `user_id` of the current profile being viewed.
– The button is only shown to users with the **edit_users** capability (typically administrators).
Let me know if you need further customizations! 🚀
### Solution:
Add the following code to your theme’s `functions.php` file or a custom plugin:
function add_custom_link_after_profile($order) {
$customer_id = $order->get_customer_id();
if (!$customer_id) {
return;
}
$custom_url = admin_url("user-edit.php?user_id={$customer_id}");
echo '<p><a href="' . esc_url($custom_url) . '" class="button">Custom Link</a></p>';
}
add_action('woocommerce_admin_order_data_after_billing_address', 'add_custom_link_after_profile');
### Explanation:
– This retrieves the **customer ID** from the order.
– It generates a **custom link** pointing to the **user-edit.php** page of the respective user.
– The link appears after the **Profile** section in the WooCommerce order details page.
#### If you want to add a different link (e.g., to a custom page):
Change:
$custom_url = admin_url("user-edit.php?user_id={$customer_id}");
To:
$custom_url = admin_url("admin.php?page=custom-page&user_id={$customer_id}");
Let me know if you need further customizations! 🚀
if (!current_user_can('edit_users')) {
return;
}
$user_id = $user->ID;
$custom_url = admin_url("admin.php?page=custom-page&user_id={$user_id}");
echo 'Custom Actions';
echo '
Custom Link
Go to Custom Page
';
}
add_action('edit_user_profile', 'add_custom_link_to_user_profile');
add_action('show_user_profile', 'add_custom_link_to_user_profile');
add top of the page
### Updated Code to Add Custom Link at the Top:
Add this to your theme’s `functions.php` file or a custom plugin:
function add_custom_link_to_top_of_user_profile() {
// Check if we are on the user edit page
if (!isset($_GET['user_id']) || !current_user_can('edit_users')) {
return;
}
$user_id = intval($_GET['user_id']);
$custom_url = admin_url("admin.php?page=custom-page&user_id={$user_id}");
echo '<div class="notice notice-info" style="padding: 15px; margin-bottom: 20px;">
<p><strong>Custom Actions:</strong>
<a href="' . esc_url($custom_url) . '" class="button button-primary" target="_blank">Go to Custom Page</a></p>
</div>';
}
add_action('admin_notices', 'add_custom_link_to_top_of_user_profile');
### Explanation:
– Uses `admin_notices` to display a message **at the top of the page**.
– Checks if `user_id` is set to ensure it runs only on **user-edit.php**.
– Displays a **styled notice box** with a button linking to your custom page.
This will place the **"Go to Custom Page"** button at the very **top of the user edit page**.
Let me know if you need any modifications! 🚀
// Check if we are on the user edit page
if (!isset($_GET['user_id']) || !current_user_can('edit_users')) {
return;
}
$user_id = intval($_GET['user_id']);
$custom_url = admin_url("admin.php?page=customer-order-notes&user_id={$user_id}");
echo '
View all order notes
';
}
add_action('admin_notices', 'add_custom_link_to_top_of_user_profile');
display only on show profile/edit profile page
### Updated Code:
function add_custom_link_to_top_of_user_profile() {
global $pagenow;
// Ensure we are on user-edit.php (editing another user) or profile.php (editing own profile)
if (!in_array($pagenow, ['user-edit.php', 'profile.php'])) {
return;
}
// Ensure the current user has permission to edit users
if (!isset($_GET['user_id']) || !current_user_can('edit_users')) {
return;
}
$user_id = intval($_GET['user_id']);
$custom_url = admin_url("admin.php?page=customer-order-notes&user_id={$user_id}");
echo '<div class="notice notice-info" style="padding: 15px; margin-bottom: 20px;">
<p>
<a href="' . esc_url($custom_url) . '" class="button button-primary" target="_blank">View all order notes</a></p>
</div>';
}
add_action('admin_notices', 'add_custom_link_to_top_of_user_profile');
### Explanation:
✅ **Checks if the page is `user-edit.php` or `profile.php`**
✅ **Ensures the `user_id` is set** to avoid errors
✅ **Restricts access to users with `edit_users` capability**
✅ **Displays the link only on the user profile edit pages**
Now, the button will **only appear on user profile pages** and not on other admin pages. 🚀
Let me know if you need any modifications! 😊