0

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

2
  • you can't use php in post content, it has to be done in the template or via a filter.
    – Milo
    Commented Sep 14, 2013 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
    Commented Sep 14, 2013 at 15:04

2 Answers 2

0

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]
2
  • 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! Commented Sep 14, 2013 at 15:37
  • U r welcome. Take a look at WordPress Roles and Capabilities to figure out what capability to use. Commented Sep 14, 2013 at 15:42
3

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.

1
  • 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 Commented Sep 14, 2013 at 15:38

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.