0

I would like to open a modal/popup on my homepage but only to specific users using their roles.

I have a plugin (CP Poll) and I would like only some people to see it. It gives me a shortcode. For example, editors, and show them only once. How can I do that ? I've tried using many extensions but none of them helped me to aim only specific roles. I've also tried with this code

<?php if( current_user_can('editor') || current_user_can('administrator') ) {  ?> 
// stuff here for admins or editors
<?php } ?>

That I've found here but it doesn't work.

Thank you

2
  • are you putting that code in a php file or are you trying to put it in the content in a page editor in your dashboard?
    – rudtek
    Commented Mar 2, 2017 at 17:28
  • I tried to put it in functions.php. I don't know where to put it elsewhere
    – ferdi_
    Commented Mar 2, 2017 at 17:29

1 Answer 1

0

You need to put that code in the php file of your child theme relative to where you want it to appear and on what pages. So if you want it to appear on a blog page you would place it in your single.php file right under your call for content ( the_content() )

Alternatively, if you're putting it in your functions.php and you wanted it to show up after your content you could put it in a function like this:

function poll_after_the_content( $content ) {
    $custom_content = $content;
      if( current_user_can('editor') || current_user_can('administrator')) { 
         $custom_content .= '<h2>I am an admin or editor!</h2>';
        // stuff here for admins or editors
        }     
    return $custom_content;
    }
add_filter( 'the_content', 'poll_after_the_content' );

This code would add "I am an admin or editor!" to all posts/pages when logged in as an admin or editor.

Change this line:

         $custom_content .= '<h2>I am an admin or editor!</h2>';

to

         $custom_content .= do_shortcode ('[your_short_code]');

to echo out your poll after the content.

2
  • Thank you very much for your answer. But is that possible to put it in a popup inside the window ? Before the user can access the content ? Thank you again
    – ferdi_
    Commented Mar 3, 2017 at 9:45
  • I assumed that was part of your shortcode. If it's not and your theme doesn't make them, you'll need to get a modal plugin. from there you'll just add the shortcode of the modal to surround that of the poll and place it as noted above.
    – rudtek
    Commented Mar 3, 2017 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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