簡體   English   中英

更改注冊/丟失密碼“操作鏈接” URL,標題和“修改錯誤”頁面。 主題我的登錄插件驅動的Wordpress網絡多站點

[英]Change Register/Lost Password “action links” URLs, titles & Modify Error pages. Theme My Login plugin driven Wordpress network multisite

我已經為我的客戶建立了一個龐大的網絡多站點,該站點每月接收1000個新用戶,並且已經有5個克隆網絡站點,並且還在不斷增加。它有一個靜態主頁,其中主題-我的登錄插件在自定義的Divi子代上運行主題。

在自定義的登錄頁面上,登錄本身運行良好,並且在下面的兩個“操作鏈接”中分別是“注冊”和“丟失密碼”。 我有兩個外部自定義頁面可鏈接到兩個鏈接。

因此,我編輯了子主題的functions.php文件,該文件中已經包含一些工作過濾器,用於處理外部身份驗證,視頻(popcorn.js),動態版權並從管理欄隱藏Wordpress徽標。

我想將默認的“注冊/丟失密碼”“操作鏈接”更改為不同的URL,更改其鏈接標題並修改“錯誤”頁面,以便“丟失密碼?”。 鏈接將指向與“丟失的密碼”“操作鏈接”相同的URL。

下面是上面要求的更改之前的Functions.php文件:

<?php

/*
PHP script content in functions.php of Child Theme in Wordpress Network 
Multisite.

Functions to add new externally authenticated users as wordpress users 
at subscriber level via email field using HTTP POST. 

Usernames expected in the format: user@DOMAIN.com or user@role.DOMAIN.com 
(also all variations of .com.au, .co.nz, etc.) 

New Wordpress Network MultiSite Subfolders are assigned to their own groups of users. 

On login their WordPress profile is automatically created in the MultiSite 
Subfolder for that user; based on the DOMAIN portion of username/email.
Accordingly, Wordpress Network Subfolder names match the DOMAIN exactly. 

eg: "http://wpsite.com/DOMAIN/Home" is the landing page for the user: 
    "user@DOMAIN.com" or "user@DOMAIN.com.au" and so on.

The Logic flow is below annotated and step by step along with the script:

1.Do the external check: The external login must be successful and the return 
  value must be validated with a "200" response from the external auth' server. 

If response is anything else give an error.

2.Do another check to see if they exist in the WP DB, if not; create them
  first (EVERY user in WordPress has a unique ID, so using this ID to identify a user in this script.) 

If the ID is found, our user is automatically logged in 
and lands on their home page.

If the user does NOT exist, the user should be created automatically on the
relevant subfolder site (see notes ablove)
*/

// PHP code starts here:

// The two lines of filters below are executed just before the invocation of the 
// WordPress authentication process.
add_filter( 'authenticate', 'external_auth', 10, 3 );
add_filter( 'login_redirect', 'ds_login_redirect', 10, 3 );

function external_auth( $user, $username, $password ){

    // Make sure a username and password are present for us to work with
    if($username == '' || $password == '') return;    

    // Try to log into the external service or database with username and password
    $args = array(
        'method' => 'POST',
        'timeout' => 45,
        'redirection' => 5,
        'httpversion' => '1.0',
        'blocking' => true,
        'headers' => array(),
        'body' => array( 'username' => $username, 'password' => $password ),
        'cookies' => array()
        );

    $ext_auth = wp_remote_post("http://IP.OF.EXTERNAL.AUTH:SERVER/api-token-auth/",$args);

    // If external authentication was successful
    if($ext_auth['response']['code'] == 200) {

        $userobj = new WP_User();
        $user = $userobj->get_data_by( 'login', $username ); 
        // Does not return a WP_User object :(
        $user = new WP_User($user->ID); 
        // Attempt to load up the user with that ID

        if( $user->ID == 0 ) {
                // The user does not currently exist in the WordPress user table.

                // If you don't want to add new users to WordPress when they don't already 
                // exist; uncomment the following line and remove the create WP user code
                //$user = new WP_Error( 'denied', __("ERROR: Not a valid user for this system") );

                // Setup minimum required user information and create WP user
                $new_user_id =  wpmu_create_user($username, $password, $username); 
                                // A new user has been created

                                // Match DOMAIN in username/email to WordPress Subfolder and add permission to relevent blog 
                $domain_end = explode('@', $username);
                //var_dump($domain_end);
                $match = explode('.', $domain_end[1]);
                //var_dump($match);


                $domain = 'YOUR_DOMAIN.com';
                foreach ($match as $blog_key){
                    $path = '/'.$blog_key.'/';
                    $blog_id = get_blog_id_from_url ( $domain, $path );
                    if ($blog_id != 0) break;
                }       

                //Specify their role 
                $role = 'subscriber';

                // Give the user access to their blog.
                add_user_to_blog($blog_id, $new_user_id, $role);

                // Load the new user info
                $user = new WP_User ($new_user_id);
        } 

    }else if($ext_auth['response']['code'] == 400){
        $user = new WP_Error( 'denied', __("ERROR: User/pass bad") );
    }

    // Comment below line to fall back to WordPress authentication
    // (in case external service offline for maintenance)
    remove_action('authenticate', 'wp_authenticate_username_password', 20);

    return $user;   
}

function ds_login_redirect( $redirect_to, $request_redirect_to, $user )
{
    if ($user->ID != 0) {
        $user_info = get_userdata($user->ID);
        if ($user_info->primary_blog) {
            $primary_url = get_blogaddress_by_id($user_info->primary_blog) . 'index/';
            if ($primary_url) {
                //echo $primary_url; die();
                wp_redirect($primary_url);
                die();
            }
        }
    }

    return $redirect_to;
}

/* Include popcorn.js --------------------- */

function theme_name_scripts() {
    wp_enqueue_script( 'popcorn', get_template_directory_uri() . '/js/popcorn.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

/* Dynamic Copyright---------------------WPN-10-03-2016--*/

function dynamic_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = 'publish'
");
$output = '';
if($copyright_dates) {
$copyright = $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}

/* Remove WP Logo -------------------------- */


function annointed_admin_bar_remove() {
        global $wp_admin_bar;

        /* Remove their stuff */
        $wp_admin_bar->remove_menu('wp-logo');
}

add_action('wp_before_admin_bar_render', 'annointed_admin_bar_remove', 0);

if ( ! function_exists( 'get_custom_header' ) ) {
    // compatibility with versions of WordPress prior to 3.4.
    add_custom_background();
} else {
    add_theme_support( 'custom-background', apply_filters( 'et_custom_background_args', array() ) );
}

?>

經過大量測試和更改之后,這是我在/ *上方添加的最終工作代碼/ *包括popcorn.js --------------------- * / of functions.php:

/* Filter to redirect register and lost password pages-------------WPN-09-03-2016--*/

function tml_action_url( $url, $action, $instance ) {
    if ( 'register' == $action )
        $url = 'https://EXTERNAL-REGISTRATION-PAGE/';
    elseif ( 'lostpassword' == $action )
        $url = 'https://EXTERNAL-PASSWORD-RESET-PAGE';
    return $url;
}
add_filter( 'tml_action_url', 'tml_action_url', 10, 3 );

/* Filter to change titles of links to above--------------------WPN-09-03-2016--*/

function tml_title( $title, $action ) {
    if ( is_user_logged_in() ) {
        $user = wp_get_current_user;
        if ( 'profile' == $action )
            $title = 'Your Profile';
        else
            $title = sprintf( 'Welcome, %s', $user->display_name );
    } else {
        switch ( $action ) {
            case 'register' :
                $title = 'Not a member? Register';
                break;
            case 'lostpassword':
                $title = 'Forgot your password? Reset it';
                break;
            case 'retrievepassword':
            case 'resetpass':
            case 'rp':
            case 'login':
            default:
                $title = 'Sign In';
        }
    }
    return $title;
}
add_filter( 'tml_title', 'tml_title', 11, 2 );

/* Filter to change link in user error message------------------WPN-10-03-2016--*/

function login_error_message($error){
    //check if that's the error you are looking for
    $pos = strpos($error, 'incorrect');
    if (is_int($pos)) {
        //its the right error so you can overwrite it
        $error = "<strong>ERROR</strong>: Invalid username/password. <a href= https://EXTERNAL-PASSWORD-RESET-PAGE/>Lost Password?</a>";
    }
    else $error = "<strong>ERROR</strong>: Invalid username/password. <a href= https://EXTERNAL-PASSWORD-RESET-PAGE/>Lost Password?</a>";
    return $error;
}
add_filter('login_errors','login_error_message');

對於Igor Yavych表示由衷的感謝,他在昨晚無眠的早晨很早的時候,在我在一個菜鳥錯誤的時候為我提供了幫助,而他正在嘗試代碼以最終獲得工作結果! (請參見此處: 有關if和else if語句的functions.php中的Wordpress PHP問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM