Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

We currently have a setup where anyone can register as a user and their own 'page' gets created manually by admin. We need a menu option to be something like 'View Your Page' and direct a user to their own page. I can't get my head around it as there is no current link between the user and the page that's created manually...any thoughts?

Thanks.

share|improve this question
    
Are you sure you want to manually associate a "page" with each user? Wouldn't you rather have a "user" page which is rendered differently depending on the user? Perhaps if you were a bit more elaborate on what are your goals, it could be easier to help you. –  Sunyatasattva Mar 4 '13 at 2:04
add comment

2 Answers

up vote 1 down vote accepted

I've answered a similar Question and the same code can be adapted for this case. This creates a Meta Box where you can assign a user to a specific post/page, and the information is stored as a Custom Field. The following can be put in functions.php or in a functionality plugin (see: Where to put my code: plugin or functions.php?):

/* Define the custom box */
add_action( 'add_meta_boxes', 'authors_meta_box_wpse_89213' );

/* Do something with the data entered */
add_action( 'save_post', 'save_postdata_wpse_89213', 10, 2 );

function authors_meta_box_wpse_89213() 
{
    global $current_user;
    if( !current_user_can( 'delete_plugins' ) )
        return;

    add_meta_box(
        'sectionid_wpse_89213',
        __( 'Page of user' ), 
        'authors_box_wpse_89213',
        'page',
        'side',
        'high'

    );
}

function authors_box_wpse_89213() 
{
    global $post;

    $selected_user = get_post_meta( $post->ID, 'users_dropdown', true);
    $users_list = get_users();

    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'noncename_wpse_89213' );

    echo '<div class="element">
        <select name="users_dropdown" id="users_dropdown">
        <option value="">- Select -</option>';

    foreach( $users_list as $user ) {
          echo '<option value="'.$user->ID.'" ' . selected( $selected_user, $user->ID, false ) . '>'.$user->data->display_name.'</option>';
    }

    echo '</select></div>';
}

function save_postdata_wpse_89213( $post_id, $post_object ) 
{
    // verify if this is an auto save routine. 
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )  
        return;

    // verify this came from the our screen and with proper authorization,
    if ( !isset( $_POST['noncename_wpse_89213'] ) || !wp_verify_nonce( $_POST['noncename_wpse_89213'], plugin_basename( __FILE__ ) ) )
        return;

    // Correct post type
    if ( isset( $_POST['post_type'] ) && 'page' != $_POST['post_type'] ) 
        return;

    // OK, we're authenticated: we need to find and save the data

    //sanitize user input
    $u_id = ( isset( $_POST['users_dropdown'] ) ) ? intval( $_POST['users_dropdown'] ) : false;

    if( $u_id )
        update_post_meta( $post_id, 'users_dropdown', $_POST['users_dropdown'] );
    else
        delete_post_meta( $post_id, 'users_dropdown' );
}

With that in place, you can do something like this in a theme template file to display the "User Page" link:

<?php
if( is_user_logged_in() ) 
{
    global $current_user;
    $args = array(
            'post_type' => 'page',
            'numberposts' => 1,
            'meta_key'=>'users_dropdown',
            'meta_value'=>$current_user->ID,
            'meta_compare'=>'='
        );
    $user_page = get_posts( $args );
    if( $user_page )
    {
        $permalink = get_permalink( $user_page[0]->ID );
        echo "<a href='$permalink'>View your page</a>";
    }
}
?>
share|improve this answer
    
Does the first code section go in the functions.php file and the second in the page template file? –  Sean Hawkins Mar 10 '13 at 12:05
    
@SeanHawkins, updated the Answer to clarify the position of each block of code. –  brasofilo Mar 10 '13 at 12:16
    
Thanks, will give it a go. –  Sean Hawkins Mar 10 '13 at 12:24
    
What would need changing to do the same thing for a post? Also, could the 'view your page' link appear in navigation rather than a static link? Thanks for your help. –  Sean Hawkins Mar 10 '13 at 12:31
    
Your Question asks specifically for page. Do you want both or changed your mind about the post type? IMO, the navigation issue would deserve a new Question. –  brasofilo Mar 10 '13 at 12:35
show 3 more comments

IF you name the pages with the username for example www.site.com/author-page/jhon_doe you can try something like this;

<?php
$user =  wp_get_current_user();

$username = $user->user_login;
?>
<a href="http://www.site.com/author-page/<?php echo $username;?>"> View your page</a>

Try to use a unique field or ID to name the pages to make it work. See more of wp_get_current_user() to see all fields available

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.