无需打印任何内容即可获得WordPress登录功能

时间:2011-08-29 作者:Jonathan

我想提供一个带有“文件传输”标题的动态生成的PDF

header(\'Content-Description: File Transfer\');
但是,在生成文档之前,我需要验证用户的登录名和凭据。

if(is_user_logged_in()){
     $currentUser = wp_get_current_user();
     $userLogin = $currentUser->user_login;
     if(userAllowedAccess($requestID, $userLogin)){
         header(\'Content-Description: File Transfer\') 
         ....... file content
     }
     else{
         //Do nothing... get off my lawn
     }
}

我应该把它放在哪里,这样我就不会从我的wordpress页面中获得任何标题或输出?我需要加载足够的API来获取我的is\\u user\\u logged\\u in()和wp\\u get\\u current\\u user()函数,但不想加载打印到页面的任何内容。。。

建议?

谢谢-J

2 个回复
SO网友:chrisguitarguy

我想你是指Content-Disposition 标题。

我最近为一位客户做了一件非常类似的事情。我选择的武器:自定义重写规则。

首先,添加重写:

<?php
add_action( \'init\', \'wpse27232_add_rewrite\' );
/**
 * Adds the rewrite rule for the download.
 * 
 * @uses add_rewrite_rule
 */
function wpse27232_add_rewrite()
{
    add_rewrite_rule(
        \'^download/?$\',
        \'index.php?file_download=true\',
        \'top\'
    );
}
当有人来访时yoursite.com/download 他们会抓住这次重写。请注意index.php?file_download=true 一点WordPress不知道file_download 应为可识别的查询变量。让我们通过过滤来识别query_vars.

<?php
add_filter( \'query_vars\', \'wpse27232_query_vars\' );
/**
 * Filter our query vars so WordPress recognizes \'file_download\'
 */
function wpse27232_query_vars( $vars )
{
    $vars[] = \'file_download\';
    return $vars;
}
现在有趣的部分是:抓住file_download 查询变量并发送文件。

<?php
add_action( \'template_redirect\', \'wpse27232_catch_file_dl\' );
/**
 * Catches when the file_download query variable is present.  Sends the content
 * header, the file, and then exits.
 */
function wpse27232_catch_file_dl()
{
    // No query var?  bail.
    if( ! get_query_var( \'file_download\' ) ) return;
    
    // change this, obviously. Should be a path to the pdf file
    // I wrote this as a plugin, hence `plugin_dir_path`
    $f = plugin_dir_path( __FILE__ ) . \'your-file.pdf\';
    if( file_exists( $f ) && is_user_logged_in() )
    {
        // Do your additional checks and setup here
        
        // Send the headers
        header(\'Content-Type: application/pdf\');
        header(\'Content-Disposition: attachment; filename=\' . basename( $f ) );
        header( \'Content-Length: \' . filesize($f));
        // You may want to make sure the content buffer is clear here

        // read the pdf output
        readfile($f);
        exit();
    }
    else
    {
        global $wp_query;
        $wp_query->is_404 = true;
    }
}
终于得到了这一点plugin 要工作,请使用激活挂钩刷新重写规则。

<?php
register_activation_hook( __FILE__, \'wpse27232_activation\' );
function wpse27232_activation()
{
    wpse27232_add_rewrite();
    flush_rewrite_rules();
}

SO网友:Rarst

有多种方法可以做到这一点:

页面模板Ajax操作加载core早期挂钩的自定义处理程序(例如template_redirect)AJAX in Plugins 在法典中。

结束

相关推荐

清空使用设置API创建的选项的最佳方法是什么?

我正在尝试清空使用设置API创建的选项,但失败。update_option ( \'my_option\', \'\' ); 似乎什么都不做delete_option ( \'my_option\' ); 破坏整个选项,导致其他问题。我只想清空这些值并重置它。我真的不知道如何正确地实现这一点。有人能帮忙吗?50分悬赏悬赏!<?php //Create the menus add_action( \'admin_menu\', \'tccl_menu\' ); function