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'm trying to add a "Click here to edit this page" button in some Wordpress pages.

This button should only appear if the user is admin, this is the code that should appear if the user is admin:

if ( current_user_can( 'manage_options' ) ) {

<div class="action">
<div class="pe-container"><section class="row-fluid">
<div class="span12">
<h5>To edit this page <a href="wp-admin/post.php?post=179&amp;action=edit">CLICK HERE <i class="icon-right-open-mini"></i></a></h5>
</div>
</section></div>
</div>

} else {
    return false;
}

This code outputs this:

Code Output

share|improve this question
    
you can't use php in post content, it has to be done in the template or via a filter. –  Milo Sep 14 '13 at 14:38
    
imagine if you could, anyone could put a PHP snippet to call wpdb and print out every users password in a comment or a draft post and preview it, and why would we need shortcodes? –  Tom J Nowell Sep 14 '13 at 15:04
    
Thanks for the info guys! –  Lucas B Sep 14 '13 at 15:39
add comment

2 Answers 2

up vote 0 down vote accepted

As @Rarst said, use PHP code inside a post content is bad habit. But you can use another workaround to show a section only for appropriate users.

Add the following code into your functions.php file:

add_shortcode( 'if_user_can', 'wpse8170_show_content_if_user_can' );
function wpse8170_show_content_if_user_can( $atts, $content = '' ) {
    $atts = shortcode_atts( array( 'capability' => 'edit_posts' ), $atts );
    return current_user_can( $atts['capability'] ) ? $content : '';
}

This snippet will allow you to use [if_user_can capability="..."]...[/if_user_can] shortcode in your posts. You can pass a capability into shortcode by using capability="..." attribute. If an user has required capability, then shortcode content will be shown, otherwise nothing will appea on a page.

Use it like this:

[if_user_can capability="edit_posts"]
<div class="action">
<div class="pe-container"><section class="row-fluid">
<div class="span12">
<h5>To edit this page <a href="wp-admin/post.php?post=179&amp;action=edit">CLICK HERE <i class="icon-right-open-mini"></i></a></h5>
</div>
</section></div>
</div>
[/if_user_can]
share|improve this answer
    
It worked perfectly, thanks for the solution! I'll study it and try to adapt it to my future needs, but it worked straight away...!! Thanks for the lesson! –  Lucas B Sep 14 '13 at 15:37
    
U r welcome. Take a look at WordPress Roles and Capabilities to figure out what capability to use. –  Eugene Manuilov Sep 14 '13 at 15:42
add comment

WordPress does not allow PHP code in content. While there are some solutions to circumvent that, it's typically bad idea from security point of view.

Edit links are often implemented in theme's template files rather than content. If placing it in content arbitrarily is hard requirement your best option is probably to create shortcode for it, using Shortcode API.

share|improve this answer
    
Thank you Rarst, I never studied programming languages, I think it's about time I go to some course or something... It's a shame to be a site maker and don't understand a thing of PHP and Javascript –  Lucas B Sep 14 '13 at 15:38
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.