Frontend Buddypress Notifications - Show for Current User Only - wordpress

I've added a custom slide-out box to show a user's Buddypress notifications on any page.
https://imgur.com/ellepqp
I'm essentially echoing the template part from the Buddypress plugin Youzify (formerly known as Youzer). Youzify is just an extension to redesign Buddypress so I believe this problem is not necessarily related to that but rather native Buddypress.
I am able to get the Notifications for the current user when I'm on any standard Wordpress page. However, if I'm on a user's profile, it shows THEIR Notifications instead! I know that by default, Admins can actually see all Notifications if they simply add /notifications to any profile slug, but this is happening for standard Users too.
This is the current code:
<?php global $bp;
if( bp_has_notifications($bp->loggedin_user->id) ) : ?>
<?php bp_get_template_part( 'members/single/notifications/notifications-loop' ); ?>
<div class="read-all-notifs"> View All Notifications</div>
<?php else : ?>
<?php bp_get_template_part( 'members/single/notifications/feedback-no-notifications' ); ?>
<?php endif; ?>
I assume it's something to do with the template part and some function that forces the bp_displayed_user ID when on a Buddypress page, so how can I override that to make sure the Notifications in my popup are always for the current user on ANY page? Anyone have any ideas? Thanks.

I figured this out with help from a developer. This is the correct formatting for the initial part:
<?php if ( is_user_logged_in() && bp_has_notifications( array('user_id' => bp_loggedin_user_id(), 'per_page' => 10, 'search_terms' => false ) ) ) : ?>
The search terms part was added because on any search results page it was showing the 'No Notifications' loop for everyone.

Related

WordPress - Enter password in a password-protected page but page keeps redirecting back to enter password

I've implemented a password-protected page on WordPress, but every time I put in the correct password, I get redirected back to the same page to enter the password again.
Here is my code to implement a password-protected page on WordPress:
<?php if ( !post_password_required() ) { ?>
// put content here
<?php } else { ?>
<?php echo get_the_password_form(); ?>
<?php } ?>
I've implemented this same code for other websites and it has worked fine in the past, but I'm unsure why this one is not working.
Would anyone have experience as to what is going on? Maybe there's a cache issue?
Thank you so much!
Your global $post probably isn't set up wherever you're showing this code. According to the docs, post_password_required() takes an optional WP_Post object or $post_id as an argument.
Try if ( ! post_password_required( get_the_ID() ) ) {
Otherwise it means the "correct password cookie" isn't being set - make sure that you have cookies enabled.

Post comments using post ids Wordpress

I’m trying to use blogs(posts) with two different designs
First, for non-loggedin(guest) users which access blogs from website (Example URL – Click Here)
Second is for logged in users which access blogs from their frontend dashboard
For the first case, I am using single.php and
For the second case, I’m using custom template in user’s dashboard and fetching posts using post ids.
QUERY
——————–
So the question is how to post comments to post using post id i.e for the second case?
E.g post url is: http://yourdomain.com/blogs/?pid=23
Here is attached video which highlights the query
Unable to display content. Adobe Flash is required.
Further, is there any other way to achieve the above scenario i.e. two different designs
Let me know in case of more clarification.
You can use a normal loop in your custom query for logged in users
for example
<?php
$args = array(
'post__in' => array(1,2,4,6),
'post_type' => 'post',
);
// the query
$the_query = new WP_Query( $args ); ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
// put your code here
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
this way you should not be worry about getting comments by post id (the loop will handle the id for you)

WooCommerce Empty Cart if User Go to Page or Site Other than Current Checkout Page

I'm not sure if this is right question to ask but still, I did not find any solutions for it. I am trying to create a method where when user go to page/site other than the current Checkout page, user's cart will automatically emptied. I wonder if this is possible because I have changed the purchase flow of my custom E-shop. I have tried search on the Internet and try many methods, but it doesn't seem to work. I am currently having a hard time on understanding how WooCommerce's hooks & functions work.
Now, let me tell you guys how my code works from the beginning:-
These are the code for the page that display the whole available product. As you can see, when user wanted to buy a product, they will click the Buy button and my E-shop will automatically add that product into the cart.
<?php
// Querying of product information retrieval
$args = array( 'post_type' => 'product', 'posts_per_page' => 4, 'orderby' =>'menu_order', 'order' =>'ASC');
$loop = new WP_Query( $args );
// Display each retrieved product
while ( $loop->have_posts() ) :
$loop->the_post();
// WooCommerce global product variable
global $product;
global $woocommerce;
the_title();
the_excerpt();
echo $product->get_price_html();
$id = $product->id;
?>
/**** Select the product & automatically add to cart ****/
<button type="button"> BUY</button>
<?php endwhile; ?>
<?php wp_reset_query(); // After the loop ended, quit the custom loop and reset back the main loop
?>
When my E-shop have completed the 'add to cart' process, the user will be redirected to the Checkout Page. The function for it is as below.
add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
Basically what I am trying to tell is, the user will directly redirected to the Checkout page after they click to buy that product. This method skipped the Single Product page & Add to Cart page.
But right now, I need help on determining the right method if user get out of that page or log-out, the cart will be automatically empty. Is it possible? If not, sorry for my lack of knowledge.
I have been trying the code below as it is the closest what I want it to do. Close, but not working.
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if ( !is_page('93') ) {
$woocommerce->cart->empty_cart();
}
}
With the above function, whenever I tried to go back to home page, or go to other link and click buy again, the cart just keep adding. This thing is making me losing my mind. I hope you guys can help me out.
This is a pretty dangerous path to go down since users that open multiple tabs of your website will be affected by the clear cart code when any other tabs become active.
WordPress conditional dont work when init (its too early to use is_page('93')) . so use something that dont relies on WordPress conditionals:
get the page url of is_page('93') lets say : www.domain.name/checkout
then use this code in functions.php:
/*empty cart if user come to homepage*/
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if ($_SERVER['REQUEST_URI'] !=== '**www.domain.name/checkout**') {
$woocommerce->cart->empty_cart();
}
}

How to handle a custom url with Wordpress?

i want to handle a custom url with WordPress theme files, for example http://www.example.com/wordpress/login
the question is that how i handle this /login with WordPress theme files like page.php , index.php or a custom template file for this reason, if exists!.
This plugin might be useful to you.
https://wordpress.org/plugins/login-logout/
It appears you could create a page called 'Login' which Wordpress would automatically create as http://www.website.com/login/
This plugin appears to be a widget, but I like that it has the ability to manage where a user is re-directed after successful login/logout.
You could register a new widget in functions.php, then call the widget with a conditional to the 'Login' page that way it could be in the main content area rather than look like a widget.
Example:
<?php } if ( is_page(9) ) { ?>
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('login-widget') ) : ?>
<?php endif; ?>
<?php } else { } ?>
My example would assume page 9 = your login Page and 'login-widget' would be the name of the widget you assigned in functions.php

Password protecting not working for custom post type single-template.php? - Wordpress

I have created a custom post-type 'Clients' where admin user can create new clients, add pictures and details to post, then password protect the page so only a particular client can access the content.
For displaying content of this post-type on the front end, I'm using a single-clients.php template. It displays the content perfectly, but the password protect function does not display the form and hide the content, even if I'm in a different browser, cache cleared/logged out of Wordpress (viewing it as a regular end-user would).
What might I be doing wrong here?
<?php get_header(); ?>
<div class="container-client">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
Display all fields and content for post-type
<?php endif; ?>
<?php endwhile; else: ?>
<div class="alert-box error">Sorry, this page no longer exists :( — Back to home</div>
<?php endif; ?>
</div>
<?php get_footer(); ?>
This is roughly how my single-clients.php page is setup. Is there any way to manually display the password function so that when end-user visits page, the content is hidden and password form is displayed?
I had exactly this problem and after some trying and reading the codex I came up with this solution:
<?php
add_filter('single_template', function($single_template) {
global $post;
if ($post -> post_type == 'your-custom-post-type') {
if (!post_password_required()) {
$single_template = 'path-to-your/single-your-custom-post-type.php';
}
}
return $single_template;
});
?>
This way the page is only rendered in the custom sinlge view after entering a password, if it is password protected.

Resources