Function and usage of get_edit_post_link() in wordpress


Answers ( 1 )


    The get_edit_post_link() function in WordPress is a useful utility for theme and plugin developers. This function retrieves the edit link for a specific post, which can be used to create a direct link to the post's edit screen in the WordPress admin area. Here's a detailed explanation of its usage:

    Function Signature

    get_edit_post_link( int|WP_Post $post, string $context = 'display' ): string|null

    Function Details

    • Purpose:

      • The get_edit_post_link() function is designed to retrieve the URL that leads to the edit screen for a given post in the WordPress dashboard. This is especially handy when you need to provide a quick edit link in your theme or plugin.
    • Description:

      • This function can be used both within the WordPress loop and outside of it. It's versatile and supports various post types including pages, posts, attachments, revisions, global styles, templates, and template parts.


    1. $post (int|WP_Post, optional):

      • This parameter accepts either a post ID or a WP_Post object.
      • The default value is the global $post object, which represents the current post in the loop.
    2. $context (string, optional):

      • This parameter specifies how to output the '&' character.
      • Options include 'display' (default) or 'raw'.
      • In the 'display' context, the '&' character is encoded as &. In the 'raw' context, it is used as a plain &.

    Return Value

    • Type: string|null
    • Description:
      • The function returns the edit post URL as a string if the post type exists and allows for an editing UI.
      • If the post type does not exist or does not have an editing UI, the function returns null.

    Sample Usage

    Here's an example of how you might use get_edit_post_link():

    // Inside the loop
    if ( have_posts() ) {
        while ( have_posts() ) {
            $edit_link = get_edit_post_link();
            if ( $edit_link ) {
                echo '<a href="' . esc_url( $edit_link ) . '">Edit this post</a>';
    // Outside the loop
    $post_id = 123; // Example post ID
    $edit_link = get_edit_post_link( $post_id );
    if ( $edit_link ) {
        echo '<a href="' . esc_url( $edit_link ) . '">Edit post</a>';

    In this example, the function is used both within the WordPress loop (to generate edit links for each post in the loop) and outside the loop (to generate an edit link for a specific post by its ID). The use of esc_url() is important for security, ensuring that the URL is safe to output.

Leave an answer