Redirect from my-account to the user's single listing

  • Jonathan
    Newbie 6 points
    April 29, 2015 at 2:52 pm

    Goal:I want that the my-account page will redirect automatically to the user’s (hopefully-single) listing.

    Background:In my Business Directory-based site, I want each user to have a single listing, that I will create when creating the user itself. Therefore, when a user logs-on, I don’t want him to see the full “my-account” page, but get directly to edit his own listing.

    My Solution
    1. Create a new page called “my-account-redirect”, and set My Account to it in General Settings => Page & Button Links.
    2. Install the plugin Insert PHP Code Snippet by XYZScript.
    3. Create a PHP snippet with the following code:
    $q = ‘post_type=’ . THEME_TAXONOMY . ‘_type&author=’ . get_current_user_id();
    $the_query = new WP_Query( $q );

    if ( $the_query->have_posts() ) {
    $the_query->the_post(); # Get the first post – I assume there’s only one
    $newUrl = ‘/add-listing/?eid=’ . get_the_ID();
    wp_redirect($newUrl, /*status*/ 301);
    } else {
    # TODO: Create new listing

    4. Add the snippet to “my-account-redirect” page.
    5. Add text to the page: “Oops… If you can see this page, this means that your listing is missing. Please contact the site administrator so he’ll create your listing.”

    Does this look right? Any suggestions about the code or process?

  • Hemanta Chakladar
    Hemanta Chakladar
    Super Guru 1,710 points
    June 5, 2015 at 9:23 pm

    Have you get it done ?

Viewing 2 posts - 1 through 2 (of 2 total)

New Business Themes for WordPress 2020

Start your new website today!

View Business Themes