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.

I want to setup a page that will display different content for each registered user of my site who falls within a specially-created user role ('map_user'). So, for example, if a site user who is in the 'map_user' category logs into the site they're automatically forwarded to a specific page. This page then refers to their username, calls specific external content (html) based on that username and includes it in the page.

I've managed to solve the first problem (forwarding users based on their user role) through a combination of plugins and functions. But I'm struggling with the second part, once they've arrived at the destination page.

I've created a template for the page and included the following php within it:

<?php if ( is_array( $user->roles ) && in_array( 'map_user', $user->roles ) ) : ?>
    <div id="mapContent">
        <?php require("/mapping/{$user->data->user_login}.html/"); ?>
    </div>
<?php endif; ?>

Where am I going wrong? (My php skills are very much of the copy/paste or "steal and adapt" variety, so I'm prepared to have stupid mistakes pointed out to me :) )

share|improve this question
    
Does this code gives you an error? For example, a file that couldn't been found? In all cases, remove the last / in your require. –  Mike Madern Mar 4 '13 at 15:54
    
Hi @MikeMadern - thanks for the pointer. The code doesn't appear to work on my localhost tester. No errors are given. For testing I replaced the 'requires' section above with a simple paragraph (<p>test</p>) - but this simply doesn't appear when the page is loaded by my test account (which has the appropriate user role), so it's as if the code has been excised or ignored. –  shngrdnr Mar 4 '13 at 15:58
add comment

1 Answer 1

up vote 1 down vote accepted

Try to do this:

<?php global $current_user;?>
<?php if(is_user_logged_in()) :?>
    <?php get_currentuserinfo();?>
    <?php if(is_array( $current_user->roles ) && in_array( 'map_user', $current_user->roles  ) ) : ?>
        <div id="mapContent">
            <?php require("/mapping/".$current_user->user_login.".html/"); ?>
        </div>
    <?php endif; ?>
<?php endif; ?>

Source codex.wordpress.org

share|improve this answer
    
Are you sure about the first slash in the path (require("/map...)? This will be an absolute path from the root of your webhost. –  Camil Staps Mar 4 '13 at 17:34
    
It only depends on the locations of the files you want to include. To be sure that you are including the right files you can also use $main_folder = $_SERVER['DOCUMENT_ROOT']; and change the code to require($main_folder."/mapping/"...) –  Sephiroth Mar 4 '13 at 17:42
    
Great stuff, this works fine (I had to remove the final "/" from the 'require' line though). Folder location isn't an issue, I can work that out now the principle's in place - thanks all. –  shngrdnr Mar 5 '13 at 10:26
    
I am glad I could help :) –  Sephiroth Mar 5 '13 at 11:24
    
Quick note - the above works fine on my localhost version, but when uploading to the live site the requires breaks. This is presumably because of misdirected links (I get a 'file not found' error). Any chance of showing how to integrate the DOCUMENT_ROOT code into the snippet you've given Sephiroth? –  shngrdnr Mar 5 '13 at 16:34
show 1 more 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.