how wordpress a post add comment only for logged users? - php

How a post in wordpress add comment only for logged users?
visitors must answer their own add in comments. but i need a post, leave a comment (only for logged users) and users must register to site beforce add comment to post
can use functions file to do it ?
i found that 2 code but not work and do not know how to use it:
in facing link find this:
https://codex.wordpress.org/Function_Reference/wp_get_current_user
Code:
<?php
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
// Not logged in.
} else {
// Logged in.
}
?>
and in facing link find this:
https://codex.wordpress.org/Function_Reference/comment_form
code:
'<p class="must-log-in">' . sprintf( __( 'You must be logged in to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>'
and from the combination of these two together, I built below function!
<?php
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
// Not logged in.
'<p class="must-log-in">' . sprintf( __( 'You must be logged in to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>'
} else {
// Logged in.
}
?>
and add this to my post with Insert PHP Code Snippet plugin for use only (a) post
please help me to do this. thank you all

Settings > Discussion you will find the option: Users must be registered and logged in to comment. - http://prntscr.com/k2f02d
Checked this option and save the settings your requirement will be fulfilled.

Review below menu navigation
Settings >>> Discussion
Checked the "Users must be registered and logged in to comment " checkbox

Related

php user role not displaying

what is wrong with this code?
I really just want to display the current users role, but it seems impossible :-(
I have tried this:
<?php if ( is_user_logged_in() ) {
global $current_user; get_currentuserinfo();
} else { }
global $current_user;
get_currentuserinfo();
echo 'User role: ' . $current_user->role . "";
?>
And this:
<?php
$current_user = wp_get_current_user();
printf( __( 'Netværksgruppe: %s', 'textdomain' ), esc_html(
$current_user->role ) ) ;
?>
and about 100 more, but nothing seems to work...
(I have tried with plugins, php snippets, functions.php, anything)
Any help would be much appreciated :-)
The user role isn't a string value you can output like that. Take a look at the WP_User object.
$user->roles is actually an array. Most WP installations result in each user having a single role. but there are many reasons why a user may have multiple roles. So you'll either need to output all of the roles using something like implode() or just output the first role by index.
// Current WP_User object
$user = wp_get_current_user();
// All Roles (implode the array)
printf( 'All my roles are: %s.', implode( ', ', $user->roles ) );
# Result: "All my roles are: administrator, custom_role, some_role."
// Or single role (only output the first result)
printf( 'My role is: %s.', $user->roles[0] );
# Result: "My role is: administrator."
See documentation of get_currentuserinfo() -
https://codex.wordpress.org/Function_Reference/get_currentuserinfo
$current_user - is object, not string. If you need email of curent user:
global $current_user;
get_currentuserinfo();
echo 'User email: ' . $current_user->user_email . "\n";

WordPress Multisite - (de)activate site action

I'm searching the WordPress codex for this but I can't seem to find any article on this issue.
When you have installed WordPress multisite and login as the superadmin you have the ability to archive,(de)activate and delete sites. This is however from the 'network' part of the installation, that only the superadmin can see. I want to place take the (de)activate option in every site's dashboard as well, so a specific (non (super)admin) role can use them to.
I want to know, is there a function I can use to display the (de)activate (depending on if the sites is active/deactive at that moment) links where I want to?
If there is not, where is the information about whether a site is active or not stored ? I was hoping for an option with a boolean in it but I can't seem to find it. This way I will be able to check myself if a site is active or not and depending on that display the correct link?
Sorry if my question is unclear or confusing.
Thanks in advance!
Deactivating the site, just adds the deleted attribute on it. And site administrators already can do that by visiting Tools -> Delete site. It is only a little different because it also removes the users from the site if it is clicked from there (it might be made in the future to be the same though).
You can use this function to see if a site with ID 2 for example has the attribute deleted (so it is deactivated):
if ( get_blog_status( 2, 'deleted' ) == 1 ) {
// The site with ID 2 is marked as deleted (it is deactivated)
}
For the specific username rights I used:
//give the right to deactivate sites to 'username' if the user doesn't have it already
$user = new WP_User( 'username' );
if ( ! $user->has_prop( 'can_manage_sites' ) ) {
$user->add_cap( 'can_manage_sites' );
}
if ( ! $user->has_prop( 'username' ) ) {
$user->add_cap( 'manage_sites' );
}
//ofcourse replace 'username' with the username you need
In my view (wich loops mu sites) I simply used an if statement to check if the site was active or not to know what link to use (activate/deactivate)
//deactivated, show activate link
if(get_blog_status( $site->blog_id, 'deleted' ) == '1'){
echo '<span class="dashicons dashicons-no" style="color:red;"></span> Niet actief <br/>';
echo '' . __( 'Activate' ) . '';
}
//activated, show deactivate link
else {
echo '<span class="dashicons dashicons-yes" style="color: green;"></span> Actief<br/>';
echo '<a style="color:red;" href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=deactivateblog&id=' . $site->blog_id ), 'deactivateblog_' . $site->blog_id ) ) . '">' . __( 'Deactivate' ) . '</a>';
}
This code will get you the link you need to (de)activate the site wich will get you to a confirmationpage.In this confirmationpage the user can see a 'sites'tab in the menu, when they click it they have the options for all the sites in the mu. This didn't matter for me since the people who will be using this functionality are colleages and know they shouldn't go there. But you might want to hide that if it concerns people who shouldn't be able to see that

Detect dashboard of WooCommerce "my account" pages

How can I detect if the "myaccount/my-account.php" template is used on the Dashboard.
Currently I use:
<?php
global $wp;
if ( !isset($wp->query_vars['page']) ) {
?>
Back to my Account
<?php } ?>
<div class="myaccount_content">
<?php
do_action( 'woocommerce_account_content' );
?>
</div>
But that feels kind of hacky. Isn't there something like a is_myaccount_dashboard() function?
Update: Detecting specifically the My account "Dashboard" page
<?php
global $wp;
$request = explode( '/', $wp->request );
// If NOT in My account dashboard page
if( ! ( end($request) == 'my-account' && is_account_page() ) ){
?>
Back to my Account Dashboard
<?php
}
?>
<div class="myaccount_content">
<?php
do_action( 'woocommerce_account_content' );
?>
</div>
Tested and works.
Original answer:
Yes of course there is is_account_page() native WooCommerce conditional that returns true on the customer’s account pages.
Here is an example using is_account_page() and is_user_logged_in(). To get the my account link url you can use: get_permalink( get_option('woocommerce_myaccount_page_id') ).
if ( !is_account_page() ) { // User is NOT on my account pages
if ( is_user_logged_in() ) { // Logged in user
// Link to "My Account pages dashboard".
?>
<?php _e( 'My Account', 'woocommerce' ); ?>
<?php }
else { // User is NOT logged in
// Link to "Login / register page".
?>
<?php _e( 'Login / Register', 'woocommerce' ); ?>
<?php
}
}
?>
Reference:
Official WooCommerce Conditional Tags
Display My Account link in a template file
After that you can Override WooCommerce Templates via a Theme using my account templates to fine tune even more WooCommerce behaviors…
To detect the exact page you're in, within the My Account area, (to allow you to determine which template is being used), I don't think Woocommerce provides a way.
I think you'll have to get the current URL, with vanilla PHP, and compare it to the URL of the page that is set to be the Dashboard/My Account Home page.
e.g.
$current_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$dashboard_url = get_permalink( get_option('woocommerce_myaccount_page_id'));
if($dashboard_url == $current_url){
// do your stuff here
}
Woocommerce's is_account_page() conditional function will return true for ALL the My Account sub pages, so can't be used to determine if you're specifically on the Dashboard page.
I had the same question (years later, lol). For people looking at the answer and wondering why it isn't helpful there are endpoint detecting functions available in woocommerce that do exactly what you're looking for. You can read the list of functions available here.
This is taken directly from the woocommerce docs. I am just copying it just incase the link is broken in the future
is_account_page() =>
Returns true on the customer’s account pages.
is_wc_endpoint_url() =>
Returns true when viewing any WooCommerce endpoint
is_wc_endpoint_url( 'order-pay' ) =>
When the endpoint page for order pay is being displayed.
is_wc_endpoint_url( 'order-received' ) =>
When the endpoint page for order received is being displayed.
is_wc_endpoint_url( 'view-order' ) =>
When the endpoint page for view order is being displayed.
is_wc_endpoint_url( 'edit-account' ) =>
When the endpoint page for edit account is being displayed.
is_wc_endpoint_url( 'edit-address' ) =>
When the endpoint page for edit address is being displayed.
is_wc_endpoint_url( 'lost-password' ) =>
When the endpoint page for lost password is being displayed.
is_wc_endpoint_url( 'customer-logout' ) =>
When the endpoint page for customer logout is being displayed.
is_wc_endpoint_url( 'add-payment-method' ) =>
When the endpoint page for add payment method is being displayed.
Actually I found out this condition that seems to work fine in order to detect the WC Dashboard page with native WC code only:
if (is_user_logged_in() && is_account_page() && !is_wc_endpoint_url()) {
echo 'WC Dashboard';
} else {
echo 'no WC Dashboard';
}
<?php if(is_page("account") && !is_wc_endpoint_url()) { ?>
Assuming your account page is at /account/, this will detect your dashboard.
If you were to only do is_page("account"), the conditional would trigger for all account pages. However, because the dashboard isn't considered a WC endpoint like 'view-order' or 'last-password' is, this simple check will do the job.
I also needed to identify the dashboard specifically and found this question but I didn't like any of the answers and WooCommerce still has no built-in tag to do this...
I have 2 issues with the answers, the first is using is_wc_endpoint_url() (not infallible) and the second is comparing URLs (personal taste I guess?)
is_wc_endpoint_url() can return false for endpoints which are
added by a theme or plugin so you can get a false negative. There's
a filter so you can add your own but you can't trust that a plugin
will do that.
Comparing URLs feels hacky to me. You can probably get
trustworthy results and it's pretty straightforward, so it's not
necessarily a bad way to do it. Although I would forgo hardcoding parts of the URL(s) or at least allow for translating.
Now if you think about it, WooCommerce itself knows without fail when to load dashboard.php so I just took that code and refactored it to simply identify the dashboard:
function is_dashboard(){
global $wp;
if( ! empty( $wp->query_vars ) ){
foreach ( $wp->query_vars as $key => $value ) {
// Ignore pagename param.
if ( 'pagename' === $key ) {
continue;
}
if ( has_action( 'woocommerce_account_' . $key . '_endpoint' ) ) {
return false;
}
}
}
return true;
}
For me, this is the most foolproof way to do it, although if done correctly, comparing URLs might be sufficient. You just can't trust is_wc_endpoint_url() for this issue.
Hope this helps anyone still looking for an is_dashboard() or is_account_page('dashboard')

Display conditionally php content depending on user role

I have a line that prints the contact information of a user in his profile. Only logged in user can see the contact information.
<?php
// Contact Info
?>
I have 2 user roles - supervisor, manager
What I am trying to achieve is that if the logged in user is a supervisor then he can see his own contact info, but he cannot see manager's contact.
But If the logged in user is a manager then he can see his own contact info, but he can also see supervisor's contact info.
I have been trying to use the standard if current_user_is function, but this will only display one or the other.
<?php global $current_user;
get_currentuserinfo();
if(current_user_is("manager"))
echo 'Contact Info Here';
else if(current_user_is("supervisor"))
echo 'Contact Info Here';
else if(current_user_is_not("manager"))
echo 'Restricted contact, must be a manager';
?>
I just can't figure out how to make this work, so it displays conditionally.
I also have these rules in the global rule, not sure how relevant it is :
$store_user = get_userdata( get_query_var( 'author' ) );
$store_info = get_store_info( $store_user->ID );
$user_meta = get_user_meta($store_user->ID);
From the question, it looks like you'll need to call the get_users function and pass the "supervisor" role to get users of that type and display their contact info to the "manager" role.
As you've written it, the contact info being displayed is for the current user. You'll need to grab another role's contact info if that's what you want to display.
Untested but this may be close...
//if current user is the manager...
$supervisors = get_users( 'role=supervisor' );
foreach ($supervisors as $supervisor) {
echo '<span>' . esc_html( $supervisor->user_email ) . '</span>';
}
I'm nothing more than a beginner with php but using 'OR' may help you aggregate roles' conditions on a single piece of content:
<?php global $current_user;
get_currentuserinfo();
if(current_user_is("manager") OR current_user_is("supervisor"))
echo 'Contact Info Here';
else()
echo 'Restricted contact, must be a manager';
?>

Trying to strip Sidebar Login code down to just log out (wordpress / woocommerce)

I'm quite the php novice, but I'm trying to slay this dragon as if i'm not.
My wordpress/woocommerce site is going to be used as a store, but there's a bug preventing a simple solution to log out as a customer (not a meta logout - just a shop/user level logout).
I'm trying to repurpose this code http://wordpress.org/extend/plugins/sidebar-login/ to simply display a "Log Out" when someone is logged in and display nothing when a user is logged out.
[Sidebar Login is on github here.][1]
[1]: https://github.com/mikejolley/sidebar-login Thanks for any assistance anyone might provide.
Under sidebar-login/includes/class-sidebar-login-widget.php
https://github.com/mikejolley/sidebar-login/blob/master/includes/class-sidebar-login-widget.php#L239-L284
Replace Line: 239 - 284
with this:
if ( is_user_logged_in() ) {
$logged_in_title = $this->replace_tags( apply_filters( 'sidebar_login_widget_logged_in_title', $logged_in_title ) );
if ( $logged_in_title )
echo $before_title . $logged_in_title . $after_title;
do_action( 'sidebar_login_widget_logged_in_content_start' );
if ( $show_avatar == 1 )
echo '<div class="avatar_container">' . get_avatar( $this->user->ID, apply_filters( 'sidebar_login_widget_avatar_size', 38 ) ) . '</div>';
echo '<ul class="pagenav sidebar_login_links"><li class="logout-link">Logout</li></ul>';
do_action( 'sidebar_login_widget_logged_in_content_end' );
// Logged out user
} else {}

Categories