Changing css for specific user depends of php variable - php

I have wordpress theme which make and post new div with unique id for episode <div class = "post-2122 episode type-episode status-publish has-post-thumbnail hentry"> and how can i change border in css only for specific user and dependable on php variable? post-xxxx is uniquefor every episode.

Maybe you could also add the user ID in body_class()
add_filter( 'body_class', 'my_class_names' );
function my_class_names( $classes ) {
// add 'class-name' to the $classes array
global $current_user;
$user_ID = $current_user->ID;
$classes[] = 'user-id-' . $user_ID;
// return the $classes array
return $classes;
}

If you want to display a special border on specific user when is logged, you can try :
<?php
if ( is_user_logged_in() ) {
echo '<div class="is-logged">';
} else {
echo '<div>';
}
?>
And then apply a CSS to .is-logged

you can use:
<div class="post-<?php $postid = get_the_ID(); ?> episode type-episode status-publish has-post-thumbnail hentry">
here is the reference how to get post id
hope your problem will be solve.!

You can use get_current_user_id() to get the logged in ID of the user.
<?php $user = 'user_' . get_current_user_id(); ?>
<div class="<?php echo $user; ?> post-2122 episode type-episode status-publish has-post-thumbnail hentry">
If the user is not logged in, $user will echo user_0, if they are logged in, $user will echo user_xx with their ID in place of xx.
Codex: https://developer.wordpress.org/reference/functions/get_current_user_id/

Related

display Woocommerce subscription actions to any of custom page or dashboard

I am using WooCommerce subscriptions and am trying to create a custom flow in the dhasboard for the user.
Currently user logs in > dashboard shows, I have custom code to show if subscription status is "active" or "on-hold". If it is on hold, the user currently has to click on view subscription, then click on a listed subscription and then click renew in the actions section.
I want to move this actions button to renew an "on-hold" subscription out of the subscription-details.php file and into the dashboard.php file to reduce these steps.
Here is the snippet i've found that I think relates to the renew action button:
<?php do_action( 'woocommerce_subscription_before_actions', $subscription ); ?>
<?php $actions = wcs_get_all_user_actions_for_subscription( $subscription, get_current_user_id() ); ?>
<?php if ( ! empty( $actions ) ) : ?>
<tr>
<td><?php esc_html_e( 'Actions', 'woocommerce-subscriptions' ); ?></td>
<td>
<?php foreach ( $actions as $key => $action ) : ?>
<?php echo esc_html( $action['name'] ); ?>
<?php endforeach; ?>
</td>
</tr>
<?php endif; ?>
<?php do_action( 'woocommerce_subscription_after_actions', $subscription ); ?>
I tried to bring this into the dashboard.php file, however I get an error saying the site is experiencing technical difficulties.
Any ideas on how I could bring this renewal action button into the dashboard.php file instead?
Thanks in advance for any help!!!
If you want to show any of action in your custom page or dashboard then use below code and set action according to your requirements
function addCancelButton($subscription) {
$actions = wcs_get_all_user_actions_for_subscription( $subscription, get_current_user_id() );
if(!empty($actions)){
foreach ( $actions as $key => $action ){
if(strtolower($action['name']) == "cancel"){
$cancelLink = esc_url( $action['url'] );
echo "<a href='$cancelLink' class='button cancel'>".$action['name']."</a>";
}
}
}
}
add_action( 'woocommerce_my_subscriptions_actions', 'addCancelButton', 10 );
If you want to edit My account page then i suggest to use this hook in your child theme
woocommerce_account_dashboard
Here is code for same
add_action( 'woocommerce_account_dashboard','add_account_content_kiki' );
function add_account_content_kiki() {
if( has_active_subscription() ){ // Current user has an active subscription
echo '<div class="woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info"><a class="woocommerce-Button button" href="www.google.com">Test Now</a>Test link - shop now</div>';
// Example of displaying something
echo 'You have active subscription';
}
}
you have to add this function also in function file
function has_active_subscription( $user_id='' ) {
// When a $user_id is not specified, get the current user Id
if( '' == $user_id && is_user_logged_in() )
$user_id = get_current_user_id();
// User not logged in we return false
if( $user_id == 0 )
return false;
return wcs_user_has_subscription( $user_id, '', 'active' );
}
Reference of hook - https://docs.woocommerce.com/wc-apidocs/hook-docs.html
All hook you can use from here for woo commerce my account page - https://businessbloomer.com/woocommerce-visual-hook-guide-account-pages/

Wordpress dashboard metabox with 'My Recent Pages'

I am trying to create a Wordpress dashboard metabox which lists the 5 most recent pages created by the currently logged in user. Each page would be listed as a link to the individual page. Any help in doing this would be much appreciated.
The code I've created lists posts but not pages.
Here is an example:
add_action('wp_dashboard_setup', 'recent_pages');
function recent_pages() {
global $wp_meta_boxes;
wp_add_dashboard_widget('My Recent Pages', 'My Recent Pages', 'my_recent_pages');
}
function my_recent_pages() {
if ( is_user_logged_in() ):
global $current_user;
wp_get_current_user();
$author_query = array('posts_per_page' => '5,','author' => $current_user->ID);
$author_pages = new WP_Query($author_query);
while($author_pages->have_posts()) : $author_pages->the_post();
?>
<p><?php the_title(); ?></p>
<?php
endwhile;
else :
echo "not logged in";
endif;
}

Hidden checkout field Woocommerce for current user

I would like to collect and save Wordpress profile photo of the current user who submits checkout form in Woocommerce. I would achieve this with hidden checkout field. Here is what I have so far. Not sure how to get profile photo of the current user and output hidden photo :
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_hidden_field', 10, 1 );
function my_custom_checkout_hidden_field( $checkout ) {
// Get an instance of the current user object
$user = wp_get_current_user();
// Profile photo
// Output hidden photo
}
All you need to do is pass the current users email address into the get_avatar() function.
<?php
$current_user = wp_get_current_user();
if ( ($current_user instanceof WP_User) ) {
echo get_avatar( $current_user->user_email, 32 );
}
?>
Here are some links for specifics:
get_avatar();
wp_get_current_user();
To output the profile picture
<?php
echo '<img src="'. get_the_author_meta( 'user_custom_avatar', $current_user->ID, 32 ) .'" />';
?>

Add `active` class to li of current single page wordpress

Looking for your help and advices.
I have a list of links to single in Wordpress. I need to place class active only to li of active page.
Should be like on this image
But it is:
My wp-code:
<ul class="inline-list medium-6 large-4 skill-items">
<?php
$id = get_the_ID();
// echo $id;
$class;
$skills = new WP_Query( array(
'post_type' => 'skills',
'order' => 'ASC'
));
if ($skills->have_posts()) : while ($skills->have_posts()) : $skills->the_post();
// echo $post->ID;
if( $id == $post->ID) {$class = 'active';} else {$class = '';}
?>
<li class="<?php echo $class; ?>"><?php the_title(); ?></li>
<?php endwhile; endif; wp_reset_query();
?>
</ul>
That's not the proper way to do a menu in Wordpress. You should use wp_nav_menu instead of doing a custom WP_Query.
First, in functions.php add the following to register a new menu:
add_action('after_setup_theme', 'register_my_menu');
function register_my_menu() {
register_nav_menu('skills_menu', __( 'Skills menu', 'your-theme-name' ));
}
Then log in your administration, you will see that new menu placement under Appearance > Menu. Create a new menu for that placement - you have the possibility to automatically add top level new pages to this menu.
Then in your template add the following in order to display the menu:
<?php wp_nav_menu(array('theme_location' => 'skills_menu')); ?>
Wordpress will automatically handle the active class by adding current-menu-item to the appropriate li. No need for any filter for that.
Found an answer for my question using Javascript/jQuery
jQuery(document).ready(function($){
var pgurl = window.location.href;
$("ul.skill-items li").each(function(){
if($(this).find('a').attr("href") == pgurl || $(this).find('a').attr("href") == '' )
$(this).addClass("active");
})
});
pgurl contains your current url. After that for each item we are looking for anchor and its link. Then we are comapring those links, and if their are equal, we add class active to li
You may try this:
function my_alter_classes_on_li( $classes, $item ){
global $post;
if( in_array( $post->post_type, $classes ) ) $classes[] = 'current-menu-item';
return $classes;
}
add_filter( 'nav_menu_css_class', 'my_alter_classes_on_li', 10, 2 );

Disable profile editing for one single account

I have made a public account in wordpress which I will send to 100 users.
So the login would be:
Username: public
Password: 123example
The only thing I want is to hide the profile page for this specific user account so they can't change password, emailadress, etc.
How to achieve this? Maybe change some php?
The last portion in #aSeptik's answer could be a little more WP friendly.
function force_profile_redirect() {
global $pagenow, $current_user;
get_currentuserinfo();
if ($pagenow == 'profile.php' && $current_user->user_login == 'public') {
wp_redirect(home_url());
}
}
add_action('admin_init', 'force_profile_redirect');
this script cover all the aspects of the question, read the code comments for further explanation.
<?php
/**
* this make sure the public user where redirected
* to home instead of profile page
*/
function redirect_user_to($redirect_to, $request, $user)
{
global $user;
if ($user->user_login == 'public') {
return home_url();
}
else {
return home_url("/wp-admin/");
}
}
add_filter('login_redirect', 'redirect_user_to', 10, 3);
/**
* this remove the profile links from
* the top nav menu
*/
function remove_edit_profile()
{
global $wp_admin_bar, $current_user;
get_currentuserinfo();
if ($current_user->user_login == 'public') {
$wp_admin_bar->remove_menu('edit-profile');
$wp_admin_bar->remove_menu('my-account-with-avatar');
$wp_admin_bar->remove_menu('my-account');
}
}
add_action('wp_before_admin_bar_render', 'remove_edit_profile', 0);
/**
* this remove the "Site Admin" link from
* the WP meta widget, usually placed in
* the side bar.
*/
function my_unregister_widgets()
{
unregister_widget('WP_Widget_Meta');
register_widget('MY_Widget_Meta');
}
add_action('widgets_init', 'my_unregister_widgets');
class MY_Widget_Meta extends WP_Widget
{
function MY_Widget_Meta()
{
$widget_ops = array(
'classname' => 'widget_meta',
'description' => __("Log in/out, admin, feed and WordPress links"),
);
$this->WP_Widget('meta', __('Meta'), $widget_ops);
}
function widget($args, $instance)
{
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? __('Meta') : $instance['title']);
echo $before_widget;
if ($title) {
echo $before_title.$title.$after_title;
}
?>
<ul>
<?php
global $current_user;
get_currentuserinfo();
if ($current_user->user_login == 'public') {
}
else {
wp_register();
}
?>
<li>
<?php wp_loginout();?>
</li>
<li>
<a href="<?php bloginfo('rss2_url');?>" title="<?php echo esc_attr(__('Syndicate this site using RSS 2.0'));?>">
<?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>');?></a>
</li>
<li>
<a href="<?php bloginfo('comments_rss2_url');?>" title="<?php echo esc_attr(__('The latest comments to all posts in RSS'));?>">
<?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>');?></a>
</li>
<li>
WordPress.org
</li>
<?php wp_meta();?>
</ul>
<?php
echo $after_widget;
}
}
/**
* this prevent from non authorized user ( public )
* to pointing to the profile page by writing into
* the address bar.
*/
function force_profile_redirect()
{
global $pagenow, $current_user;
if (strtolower($current_user->user_login) == 'public') {
wp_redirect(home_url());
}
}
add_action('admin_init', 'force_profile_redirect');
?>
You'd need to modify your profile page code, to make it not show the editable areas, and not run the "update profile" action, if the user ID is [xyz].
For the page which actually does the updating of the profile, you can just put at the top something like
// Change this line to match however you identify your logged-in user
// And change the id number to the ID of the public user
global $current_user;
get_currentuserinfo();
if ($current_user->ID == 1)
{
// Stop them seeing this page
header('Location: index.php');
// And for good measure
die();
}
For the page on which they can change the profile fields before they submit the form, you can do something like this
// Change this line to match however you identify your logged-in user
// And change the id number to the ID of the public user
global $current_user;
get_currentuserinfo();
if ($current_user->ID == 1)
{
// Say no
echo '<p>You cannot edit your profile on this account.</p>';
// And for good measure
die();
}
Without seeing your code, it's hard to be more specific, but this should work at a push, even if it's not exactly how you want it to work.

Categories