登录下载谷歌驱动器文件[关闭]

问题描述 投票:0回答:0

我正在尝试添加一项功能,当用户点击任何下载链接(以“drive.google.com”开头或包括“drive.google.com”)时,它应该将他重定向到登录页面。

意味着,如果用户登录,他只能从 WordPress 网站下载 Google 驱动器文件(或任何以 drive.google.com 开头的链接)。我正在使用终极会员插件进行用户注册。

我试过下面给出的 php 代码,但对我没有用。

 <?php
// Add filter to modify links in content
add_filter( 'the_content', 'dgf_modify_links_in_content' );
function dgf_modify_links_in_content( $content ) {
    // Find all links in content
preg_match_all( '/href="(.*?)"/i', $content, $matches );
// Loop through links and modify Google Drive links
foreach ( $matches[1] as $url ) {
    // Check if URL starts with drive.google.com
    if ( strpos( $url, 'https://drive.google.com/' ) === 0 ) {
        // Modify link if user is not logged in
        if ( !is_user_logged_in() ) {
            $url = wp_login_url( $url );
        }
        // Generate download link with nonce URL
        $download_link = wp_nonce_url( $url, 'dgf_download' );
        // Replace original link with download link
        $content = str_replace( 'href="' . $url . '"', 'href="' . $download_link . '"', $content );
    }
}
// Return modified content
return $content;
}

// Handle download link clicks
add_action( 'init', 'dgf_handle_download_link_click' );
function dgf_handle_download_link_click() {
// Check if download link was clicked
if ( isset( $_GET['dgf_download'] ) && wp_verify_nonce( $_GET['dgf_download'], 'dgf_download' ) ) {
    // Get Google Drive file URL
    $url = $_GET['dgf_download'];
    // Download file
    wp_redirect( $url );
    exit;
}
}
?>

这是我在代码片段插件中使用的 php 代码,如果用户在单击以 drive.google.com 开头的链接时未登录,则将用户重定向到登录页面

但它仍然允许注销用户下载文件。

我应该使用任何其他代码,还是我犯了什么错误?

在此之前,我使用了以下代码和简码。

<?php
// Add filter to modify links in content
add_filter( 'the_content', 'dgf_modify_links_in_content' );
function dgf_modify_links_in_content( $content ) {
// Find all links in content
preg_match_all( '/href="(.*?)"/i', $content, $matches );
// Loop through links and modify Google Drive links
foreach ( $matches[1] as $url ) {
    // Check if URL starts with drive.google.com
    if ( strpos( $url, 'https://drive.google.com/' ) === 0 ) {
        // Generate download link shortcode
        $download_link_shortcode = '[download_link url="' . $url . '"]Download File[/download_link]';
        // Replace original link with download link shortcode
        $content = str_replace( 'href="' . $url . '"', $download_link_shortcode, $content );
    }
}
// Return modified content
return $content;
}

// Add shortcode for displaying download link
add_shortcode( 'download_link', 'dgf_download_link_shortcode' );
function dgf_download_link_shortcode( $atts ) {
// Get URL attribute from shortcode
extract( shortcode_atts( array(
    'url' => ''
), $atts ) );
// Check if URL starts with drive.google.com
if ( strpos( $url, 'https://drive.google.com/' ) === 0 ) {
    // Check if user is logged in
    if ( is_user_logged_in() ) {
        // Generate download link
        $download_link = wp_nonce_url( $url, 'dgf_download' );
        // Return download link
        return '<a href="' . $download_link . '">Download File</a>';
    } else {
        // Redirect to login page
        wp_redirect( wp_login_url() );
        exit;
    }
} else {
    // URL is not a Google Drive file, return original URL
    return $url;
}
}

// Handle download link clicks
add_action( 'init', 'dgf_handle_download_link_click' );
function dgf_handle_download_link_click() {
// Check if download link was clicked
if ( isset( $_GET['dgf_download'] ) && wp_verify_nonce( $_GET['dgf_download'], 'dgf_download' ) ) {
    // Get Google Drive file URL
    $url = $_GET['dgf_download'];
    // Download file
    wp_redirect( $url );
    exit;
}
}
?>

使用简码: [download_link url="https://drive.google.com/your-file-id"]下载文件[/download_link]

但是,它不允许任何用户在没有登录的情况下打开帖子。 那么有没有其他方法可以实现这个功能呢

php html wordpress google-drive-api ultimate-member
© www.soinside.com 2019 - 2024. All rights reserved.