ใช้ stylesheet คนล่ะอันยังไง

เริ่มโดย mr.zatan, 27 เมษายน 2020, 08:55:50

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

mr.zatan

อยากให้แต่ล่ะ user role ใช้ stylesheet  คนล่ะอันสามารถทำได้มัยครับ

** wordpress

romance69

ไกด์ประมาณนี้นะครับ

สร้าง function ในธีมไฟล์ functions.php

add_action( 'wp_enqueue_scripts', 'add_style_user_role' );
function add_style_user_role() {
    $user = wp_get_current_user();
    if ( in_array( 'author', (array) $user->roles ) ) {
        wp_enqueue_style( 'style-user-role-author', get_stylesheet_uri() . '/css/user-role-author.css' );
    }
}


ดู role name ได้ที่ https://wordpress.org/support/article/roles-and-capabilities/
ขายปลั๊กอิน Wordpress ตรวจหวยออนไลน์ อัปเดตอัตโนมัติ ราคา 2,500 บาท ใส่โค้ด THAISEO10 รับส่วนลด 10%
รับเขียนเว็บไซต์, รับเขียน Theme wordpress, รับเขียน Plugin wordpress

mr.zatan

#2
ถามต่อครับ..


Roles user จะมีประมาณนี้

Author , Contributor, Subscriber, Administrator

แล้วถ้า user ขาจรทั่วๆๆไปที่ไม่ได้ login เรียกว่าอะไรครับ

romance69

#3
อ้างถึงจาก: mr.zatan ใน 28 เมษายน 2020, 07:59:07
ถามต่อครับ..


Roles user จะมีประมาณนี้

Author , Contributor, Subscriber, Administrator

แล้วถ้า user ขาจรทั่วๆๆไปที่ไม่ได้ login เรียกว่าอะไรครับ


ถ้าไม่ได้ login ให้ใช้ is_user_logged_in() เช็คครับ

ตัวอย่าง
if (is_user_logged_in()) {
  return true;
} else {
  return false;
}

อ้างอิง: https://developer.wordpress.org/reference/functions/is_user_logged_in/
ขายปลั๊กอิน Wordpress ตรวจหวยออนไลน์ อัปเดตอัตโนมัติ ราคา 2,500 บาท ใส่โค้ด THAISEO10 รับส่วนลด 10%
รับเขียนเว็บไซต์, รับเขียน Theme wordpress, รับเขียน Plugin wordpress


mr.zatan

ใส่แบบนี้ใช่มัยครับ


add_action( 'wp_enqueue_scripts', 'wholesale_customer_logo' );
function wholesale_customer_logo(){
    if( in_array( 'is_user_logged_in()', (array) wp_get_current_user()->roles ) ){
        echo '<style>
            img.fusion-standard-logo {
                background: url(http://example.com/logo-for-wholesale-customers.png) no-repeat;
            }
        </style>';
    }
}

romance69

#6
อ้างถึงจาก: mr.zatan ใน 29 เมษายน 2020, 21:17:44
ใส่แบบนี้ใช่มัยครับ


add_action( 'wp_enqueue_scripts', 'wholesale_customer_logo' );
function wholesale_customer_logo(){
    if( in_array( 'is_user_logged_in()', (array) wp_get_current_user()->roles ) ){
        echo '<style>
            img.fusion-standard-logo {
                background: url(http://example.com/logo-for-wholesale-customers.png) no-repeat;
            }
        </style>';
    }
}


ถ้าจะแท็ก style inline เข้าไปแบบนั่นก็เปลี่ยน HookName จาก wp_enqueue_scripts เป็น wp_head แทนครับ

ตัวอย่าง
add_action( 'wp_head', 'wholesale_customer_logo', 10, 40 );
function wholesale_customer_logo(){
    if ( is_user_logged_in() ) {
      if( in_array( 'author', (array) wp_get_current_user()->roles ) ){
          echo '<style>
              img.fusion-standard-logo {
                  background: url(http://example.com/logo-for-wholesale-customers.png) no-repeat;
              }
          </style>';
      }
    }
    else {
        // ถ้าสมาชิกไม่ได้ล็อคอินจะให้ใช้ style อะไรก็เขียนเข้าไปได้
    }

}
ขายปลั๊กอิน Wordpress ตรวจหวยออนไลน์ อัปเดตอัตโนมัติ ราคา 2,500 บาท ใส่โค้ด THAISEO10 รับส่วนลด 10%
รับเขียนเว็บไซต์, รับเขียน Theme wordpress, รับเขียน Plugin wordpress

mr.zatan

#7
ขอบคุณครับ..

ผมมีอีกแบบนึง แบบใส่เข้าไปใน css เลย ต้องใส่ is_user_logged_in() ยังไงครับ


-style.css

.user-role-customer .catalog_top {
    display: none;
}


-functions.php

add_filter( 'body_class', function( $classes ){
   foreach( (array) wp_get_current_user()->roles as $role ){
       $classes[] = "user-role-$role";
   }
   return $classes;
});

romance69

#8
อ้างถึงจาก: mr.zatan ใน 30 เมษายน 2020, 09:17:59
ขอบคุณครับ..

ผมมีอีกแบบนึง แบบใส่เข้าไปใน css เลย ต้องใส่ is_user_logged_in() ยังไงครับ


-style.css

.user-role-customer .catalog_top {
     display: none;
}


-functions.php

add_filter( 'body_class', function( $classes ){
    foreach( (array) wp_get_current_user()->roles as $role ){
        $classes[] = "user-role-$role";
    }
    return $classes;
});

ผมไม่เข้าใจคำถามนะครับ แต่จะตอบถามที่ผมเข้าใจก็แล้วกัน (สรุปเข้าใจหรือไม่เข้าใจเนี้ยะ 555++)

ถ้าให้ใส่คลาสสำหรับสมาชิกที่ยังไม่ได้ล็อกอินนะครับ ตามที่ผมตอบไปข้างบนคือให้เอา is_user_logged_in() มาตรวจสอบสมาชิกก่อนครับเช่น

if ( is_user_logged_in() ) {
  // หากสมาชิกล็อคอินอยู่ก็ให้เพิ่มคลาสนี้เข้าไป
  $classes[] = 'user-logged';
} else {
  // หากสมาชิกไม่ได้ล็อคอิน
  $classes[] = 'user-guest';
}


แต่ถ้าจะเอามาใช้เช็คสถานะจริงควรจะเป็นประมาณนี้ ผมอิงจากโค้ดที่ท่านส่งมาเลยนะครับ

add_filter( 'body_class', function( $classes ){
if ( is_user_logged_in() ) {
    $classes[] = 'user-logged';

    foreach( (array) wp_get_current_user()->roles as $role ){
        $classes[] = "user-role-$role";
    }
} else {
    $classes[] = 'user-guest';
}
    return $classes;
});


แล้วนอกนั่นก็ไปเพิ่มคลาส css เอาต่อในไฟล์ styles.css ครับ


// สีพื้นหลังสำหรับสมาชิกที่ไม่ได้ล็อคอิน
body.user-guest {
   background: red;
}

// สีพื้นหลัง user-logged
body.user-logged {
   background: yellow;
}

// สีพื้นหลัง role author
body.user-role-author {
   background: black;
}




ขายปลั๊กอิน Wordpress ตรวจหวยออนไลน์ อัปเดตอัตโนมัติ ราคา 2,500 บาท ใส่โค้ด THAISEO10 รับส่วนลด 10%
รับเขียนเว็บไซต์, รับเขียน Theme wordpress, รับเขียน Plugin wordpress