0

我正在尝试从 WordPress 安装中的数据创建一个 vcard(我假设 WP 部分无关紧要)。我正在使用类似 URL 参数调用该文件,/inc/vcard.php?vcard=664并且到目前为止具有以下代码:

<?php ob_start();

define( 'WP_USE_THEMES', false );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php' );

/**
 * Loads the WordPress environment and template.
 *
 * @package WordPress
 */
if ( !isset($wp_did_header) ) {
    $wp_did_header = true;
    require_once( dirname(__FILE__) . '/wp-load.php' );
    wp();
    require_once( ABSPATH . WPINC . '/template-loader.php' );
}

if (isset($_GET['vcard'])) {
    $attorney_id = $_GET['vcard'];

    $post = get_post($attorney_id);
    $slug = $post->post_name;

    if (!$post || get_post_type() !== 'attorneys') {
        echo 'Invalid attorney';
        return;
    }

    function field_check($field) {
        echo get_field($field) ? get_field($field) : '';
    }

    ?>

    BEGIN:VCARD
    VERSION:3.0
    REV:<?php echo get_the_date(); ?>
    FN:<?php field_check('first_name'); ?> <?php field_check('middle_initial'); ?> <?php field_check('last_name'); ?>
    N:<?php field_check('last_name'); ?>;<?php field_check('first_name'); ?> <?php field_check('middle_initial'); ?>

    TITLE:<?php field_check('position'); ?>

    ORG: Odin, Feldman &amp; Pittleman P.C.

    ADR;WORK:;;1775 Wiehle Avenue, Suite 400;Reston;Virginia;20190;United States of America

    URL:<?php field_check('blog_url'); ?>

    TEL;TYPE=WORK;VOICE:<?php field_check('phone_number'); ?>

    TEL;TYPE=WORK;Fax:<?php field_check('fax_number'); ?>

    EMAIL;TYPE=internet,pref:<?php field_check('email'); ?>

    URL;TYPE=WORK:<?php the_permalink(); ?>

    TZ:-0400
    END:VCARD

    <?php header('Content-Type: text/x-vcard; charset=utf-8');header('Content-Disposition: attachment; filename="' . $slug . '.vcf"'); exit();
}
else {
    echo 'Invalid input.';
}
$output = ob_get_contents();
ob_end_clean();
echo $output;

但似乎输出缓冲工作不正常,或者我仍然没有连接如何一起使用两者(标题和输出缓冲)。任何帮助将不胜感激。谢谢!

快速说明一下,我在 WordPress 之外调用此函数,这就是为什么我在输出缓冲区启动后“包含”WordPress。

更新

在这两个建议之后(将标题设置在缓冲区之外并将其移动exit();到底部,没有任何区别:

<?php ob_start();

define( 'WP_USE_THEMES', false );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php' );

/**
 * Loads the WordPress environment and template.
 *
 * @package WordPress
 */
if ( !isset($wp_did_header) ) {
    $wp_did_header = true;
    require_once( dirname(__FILE__) . '/wp-load.php' );
    wp();
    require_once( ABSPATH . WPINC . '/template-loader.php' );
}

if (isset($_GET['vcard'])) {
    $attorney_id = $_GET['vcard'];

    $post = get_post($attorney_id);
    $slug = $post->post_name;

    if (!$post || get_post_type() !== 'attorneys') {
        echo 'Invalid attorney';
        return;
    }

    function field_check($field) {
        echo get_field($field) ? get_field($field) : '';
    }

    ?>

    BEGIN:VCARD
    VERSION:3.0
    REV:<?php echo get_the_date(); ?>
    FN:<?php field_check('first_name'); ?> <?php field_check('middle_initial'); ?> <?php field_check('last_name'); ?>
    N:<?php field_check('last_name'); ?>;<?php field_check('first_name'); ?> <?php field_check('middle_initial'); ?>

    TITLE:<?php field_check('position'); ?>

    ORG: Odin, Feldman &amp; Pittleman P.C.

    ADR;WORK:;;1775 Wiehle Avenue, Suite 400;Reston;Virginia;20190;United States of America

    URL:<?php field_check('blog_url'); ?>

    TEL;TYPE=WORK;VOICE:<?php field_check('phone_number'); ?>

    TEL;TYPE=WORK;Fax:<?php field_check('fax_number'); ?>

    EMAIL;TYPE=internet,pref:<?php field_check('email'); ?>

    URL;TYPE=WORK:<?php the_permalink(); ?>

    TZ:-0400
    END:VCARD

    <?php
}
else {
    echo 'Invalid input.';
}
$output = ob_get_contents();
ob_end_clean();
header('Content-Type: text/x-vcard; charset=utf-8');header('Content-Disposition: attachment; filename="' . $slug . '.vcf"');
echo $output;
exit();

更新#2

我已经对代码进行了更多修改,以确保任何地方都没有杂散线:

<?php ob_start();

$slug = 'vcard';

define( 'WP_USE_THEMES', false );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php' );

/**
 * Loads the WordPress environment and template.
 *
 * @package WordPress
 */
if ( !isset($wp_did_header) ) {
    $wp_did_header = true;
    require_once( dirname(__FILE__) . '/wp-load.php' );
    wp();
    require_once( ABSPATH . WPINC . '/template-loader.php' );
}

if (isset($_GET['vcard'])) {
    $attorney_id = $_GET['vcard'];

    $post = get_post($attorney_id);
    $slug = $post->post_name;

    if (!$post || get_post_type() !== 'attorneys') {
        return 'Invalid attorney';
    }

    function field_check($field) {
        echo get_field($field) ? get_field($field) : '';
    }

?>
BEGIN:VCARD
VERSION:3.0
REV:<?php echo get_the_date(); ?>

FN:<?php field_check('first_name'); ?> <?php field_check('middle_initial'); ?> <?php field_check('last_name'); ?>

N:<?php field_check('last_name'); ?>;<?php field_check('first_name'); ?> <?php field_check('middle_initial'); ?>

TITLE:<?php field_check('position'); ?>

ORG: Odin, Feldman &amp; Pittleman P.C.
ADR;WORK:;;1775 Wiehle Avenue, Suite 400;Reston;Virginia;20190;United States of America
URL:<?php field_check('blog_url'); ?>

TEL;TYPE=WORK;VOICE:<?php field_check('phone_number'); ?>

TEL;TYPE=WORK;Fax:<?php field_check('fax_number'); ?>

EMAIL;TYPE=internet,pref:<?php field_check('email'); ?>

URL;TYPE=WORK:<?php the_permalink(); ?>

TZ:-0400
END:VCARD<?php
}
else {
    return 'Invalid input.';
}
$output = ob_get_contents();
header('Content-Type: text/x-vcard');
header('Content-Disposition: attachment; filename='.$slug.'.vcf');
return $output;
exit();

但仍然没有得到任何地方。我确实注意到没有“标头已发送”消息,但在尝试加载时“找不到网页”。如果我取出标头调用,这是我得到的一些示例输入:

BEGIN:VCARD
VERSION:3.0
REV:November 4, 2013
FN:Sample Name
N:Adams;Robert C.
TITLE:Shareholder
ORG: Sample Company
ADR;WORK:;;Street;City;State;12345;United States of America
URL:
TEL;TYPE=WORK;VOICE:(555) 123-4567
TEL;TYPE=WORK;Fax:(555) 123-4567
EMAIL;TYPE=internet,pref:sample@test.com
URL;TYPE=WORK:http://www.google.com
TZ:-0400
END:VCARD

现在这是在源视图中,但在浏览器中它都在一行上——在将标题呈现为 vcard 时这有关系吗?谢谢!

4

1 回答 1

0

删除 exit() 以便您的代码在发送输出之前不会停止。

于 2013-11-07T13:09:09.533 回答