Custom Link User Profile

User
wp-admin/user-edit.php?user_id=75166
Add custom link to this page in wordpress wp admin by hook
Assistant
To add a custom link to the **Edit User** page in WordPress admin (`wp-admin/user-edit.php?user_id=75166`), you can use the `edit_user_profile` or `edit_user_profile_update` action 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! 🚀

User
add in above section after profile link-
Assistant
To add a custom link after the **"Profile"** link in the WooCommerce order details page, you can use the `woocommerce_admin_order_data_after_billing_address` hook.

### 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! 🚀

User
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 '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

Assistant
If you want to add the **custom link at the top of the user edit page** (instead of under the profile details), you should use the `admin_notices` hook and check for the `user_id` in the URL.

### 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! 🚀

User
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=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

Assistant
To ensure the custom link appears **only** on the **user profile edit page** (`wp-admin/user-edit.php?user_id=75166` or `wp-admin/profile.php`), modify the code like this:

### 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! 😊

Leave a Comment

Your email address will not be published. Required fields are marked *