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/"); ?>
<?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 :) )

  • 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

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/"); ?>
    <?php endif; ?>
<?php endif; ?>

Source codex.wordpress.org

  • Are you sure about the first slash in the path (require("/map...)? This will be an absolute path from the root of your webhost. – user26607 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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