• On my website I have created a custom post type for listings. Clients can add listings on the front-end from the my-account page, and when they do, the admin is being notified by an email, containing the edit url of the newly added listing post. So when the admin clicks on the url in the email, he is being redirected directly to the edit screen of the listing post in the backend of WordPress.

    This is how the e-mail looks like: https://prnt.sc/11wyj3r

    This all works fine when the admin is not logged in yet, before clicking on the link in the email. But when the admin is already logged in on the site, he is being redirected to the default blog post overview page in the backend.

    Why is that and more importantly, how can we fix it?

    My code:

    function wgoe_send_admin_notification( $post_id, $post, $update ) {
    
        // If the post type is not a project, don't send the email
        if (get_post_type($post_id) != 'project')
        return;
    
        // If this is a revision, don't send the email.
        if ( wp_is_post_revision( $post_id ) )
        return;
    
        // If post is being published, don't send the email
        if (get_post_status( $post_id ) == 'publish' )
        return;
     
        $post_url = get_edit_post_link( $post_id );
        $subject = 'A listing has been added or updated';
     
        $message = "Howdy Admin! \n\n\n";
        $message .= "A user has added or updated a listing on your website. Please check the listing for publication:\n\n";
        $message .= $post->post_title . ": " . $post_url . "\n\n\n";
        $message .= "Dive Business For Sale";
     
        // Send email to admin.
        $adminemail = get_option( 'admin_email' );
        // $adminemail = '[email protected]';
        wp_mail( $adminemail, $subject, $message );
    
        remove_action( 'wp_insert_post', 'wgoe_send_admin_notification', 10 );
    }
    
    add_action( 'wp_insert_post', 'wgoe_send_admin_notification', 10, 3 );
    • This topic was modified 3 years, 11 months ago by bcworkz. Reason: obfuscated email in code

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I cannot replicate the behavior on my site using a very similar link. TBH, I didn’t click an email link, I just pasted the link into a new browser tab. Should lead to the same result though. Do you have some sort of custom login plugin or similar? Such a thing could influence behavior.

    Is the behavior the same using a regular post ID in the link? If not, perhaps there something unusual about how the post type was registered. In particular its rewrite rule. Try visiting the permalinks screen to cause rewrite rules to be regenerated. It might instead have to do with the post type’s capabilities not being quite right.

    If the same behavior occurs for regular post IDs, some plugin or your theme is interfering. Try switching to the twentytwentyone theme and deactivating all plugins. The regular edit screens ought to load from a post edit link. (The CPTs aren’t available when their related registration code is deactivated. They’ll be back when the code is restored). Restore your theme and plugins, one at a time, testing after each. When the behavior returns, the last activated is the culprit.

Viewing 1 replies (of 1 total)
  • The topic ‘WordPress custom post type edit post url’ is closed to new replies.