第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

聯(lián)系表格 7 (CF7) 單選按鈕以更改 WordPress 上的用戶角色

聯(lián)系表格 7 (CF7) 單選按鈕以更改 WordPress 上的用戶角色

PHP
元芳怎么了 2022-06-11 09:48:43
我有一個(gè)表單設(shè)置供用戶在我的 WordPress 網(wǎng)站上注冊(cè)后填寫。我使用 Contact Form 7 設(shè)置了表單,并有一個(gè)名為 radio-766 的單選按鈕,它有兩個(gè)選項(xiàng):訂閱者和客戶。我希望用戶選擇這兩個(gè)選項(xiàng)之一,然后當(dāng)他們提交表單時(shí),它將改變他們的用戶角色。以下是我到目前為止所擁有的......我從網(wǎng)上抓取了片段并嘗試創(chuàng)建自己的片段,但它不起作用。關(guān)于如何讓它發(fā)揮作用的任何想法?function tm_cf7_roles_posted_data( $posted_data ) {    // Stop if user is not logged in.    if ( ! is_user_logged_in() )        return;    ob_start();    $role = sanitize_key( $posted_data['radio-766'] );    if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )        return;    $user = new WP_User( get_current_user_id() );    $index = key( $user->roles );    $user_role = $user->roles[ $index ];    $output = ob_get_contents();    ob_end_clean();    return $output;} add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );我應(yīng)該在任何地方包含表單名稱或 ID 嗎?找不到這方面的信息任何幫助都非常感謝!編輯我覺得這個(gè)函數(shù)和名為“LinkedIn 之后”的 CF7 表單沒有任何聯(lián)系,所以我找到了這段代碼,只是不知道如何集成和工作if (!isset($cfdata->posted_data) && class_exists('WPCF7_Submission')) {        // Contact Form 7 version 3.9 removed $cfdata->posted_data and now        // we have to retrieve it from an API        $submission = WPCF7_Submission::get_instance();        if ($submission) {            $formdata = $submission->get_posted_data();        }    } elseif (isset($cfdata->posted_data)) {        // For pre-3.9 versions of Contact Form 7        $formdata = $cfdata->posted_data;    } else {        // We can't retrieve the form data        return $cfdata;    }      // Check this is the user registration form    if ( $cfdata->title() == 'After LinkedIn') {
查看完整描述

2 回答

?
尚方寶劍之說

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊

根據(jù) Contact form 7 插件作者is_user_logged_in()將在下面處理表單提交的情況:

  1. subscribers_only: true在表格的附加設(shè)置中設(shè)置。或者

  2. 設(shè)置WPCF7_VERIFY_NONCE為真使用add_filter( 'wpcf7_verify_nonce', '__return_true' );

欲了解更多信息,請(qǐng)單擊此處。

此外,要更改用戶角色,您可以執(zhí)行以下操作:

add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );

function tm_cf7_roles_posted_data( $posted_data ) {

    $submission = WPCF7_Submission::get_instance();

    $wpcf7      = WPCF7_ContactForm::get_current();


    $formID = $wpcf7->id();

    if ( $submission ) {

        if( $formID == "YOUR-FORM-ID" ) {

            if ( is_user_logged_in() ) {

                $role = sanitize_key( $posted_data['radio-766'][0] );

                if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )

                    return;


                // Getting the WP_User object

                $user = wp_get_current_user();

                // The user exists

                if( $user && $user->exists() ) {

                    // Check if user already has that role or not

                    if ( !in_array( $role, (array) $user->roles ) ) {

                        // Remove all the previous roles from the user and add this one

                        // This will also reset all the caps and set them for the new role

                        $user->set_role( $role );

                    }

                }

            }

        }

    }

}

如果您只想向用戶添加新角色并保留現(xiàn)有角色,則不要在set_role下面使用:


// Add a new role to the user while retaining the previous ones

$user->add_role( $role );


查看完整回答
反對(duì) 回復(fù) 2022-06-11
?
holdtom

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊

至于以編程方式設(shè)置用戶角色,您只需獲取用戶對(duì)象并使用 set_role() 函數(shù)將其角色更改為您想要的任何角色,只要該角色已定義。試試這種方式


function tm_cf7_roles_posted_data( $posted_data ) {


    // Stop if user is not logged in.

    if ( ! is_user_logged_in() )

        return;


    ob_start();


    $role = sanitize_key( $_POST['radio-766'] );

    if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )

        return;


    $user = new WP_User( get_current_user_id() );

    $index = key( $user->roles );

    $user_role = $user->set_role($role);



    $output = ob_get_contents();

    ob_end_clean();

    return $output;

add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );


查看完整回答
反對(duì) 回復(fù) 2022-06-11
  • 2 回答
  • 0 關(guān)注
  • 145 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)