Using WordPress ‘get_link_to_edit()’ PHP function

The get_link_to_edit() WordPress PHP function retrieves link data based on its ID.

Usage

get_link_to_edit( $link_id );

Example:

Input:

echo get_link_to_edit( 12 );

Output:

https://example.com/wp-admin/link.php?action=edit&link_id=12

Parameters

  • $link_id (int|stdClass) (Required) – Link ID or object to retrieve.

More information

See WordPress Developer Resources: get_link_to_edit()

Examples

Retrieve the edit link for the link with ID 5 and display it as a clickable link.

// Get the edit link for link ID 5
$edit_link = get_link_to_edit( 5 );
echo '<a href="' . $edit_link . '">Edit Link</a>';

Loop through an array of link IDs and display their edit links as clickable links.

$link_ids = array(1, 3, 5, 7);

// Loop through link IDs
foreach ($link_ids as $link_id) {
    // Get the edit link for the current link ID
    $edit_link = get_link_to_edit( $link_id );
    echo '<a href="' . $edit_link . '">Edit Link ' . $link_id . '</a><br>';
}

Retrieve the edit link for the link with ID 10 and display it as a clickable link, but only if the current user has permission to edit links.

if (current_user_can('manage_links')) {
    $edit_link = get_link_to_edit( 10 );
    echo '<a href="' . $edit_link . '">Edit Link</a>';
} else {
    echo 'You do not have permission to edit links.';
}

Retrieve all links from a specific category, and display their edit links as clickable links.

$category_id = 2;
$links = get_links( $category_id );

// Loop through the links
foreach ($links as $link) {
    $edit_link = get_link_to_edit( $link->link_id );
    echo '<a href="' . $edit_link . '">Edit Link ' . $link->link_id . '</a><br>';
}

Retrieve all links with a specific keyword, and display their edit links as clickable links.

$keyword = 'wordpress';
$links = get_links_by_search( $keyword );

// Loop through the links
foreach ($links as $link) {
    $edit_link = get_link_to_edit( $link->link_id );
    echo '<a href="' . $edit_link . '">Edit Link ' . $link->link_id . '</a><br>';
}