有两件事。首先,不要自己加载WordPress。使用WordPress主页URL,而不是直接链接到插件文件:
<a href="<?php echo home_url( \'?workshop_id=X\' ) ?>">Download iCal</a>
。。。然后拦截请求:
function wpse_183197_send_ical() {
if ( ! empty( $_GET[\'workshop_id\'] ) && ! is_admin() ) {
// Your code
exit;
}
}
add_action( \'init\', \'wpse_183197_send_ical\' );
至于代码本身,由于您只在一个帖子上操作,我们可以使其更加精简:
if ( ! $id = ( int ) $_GET[\'workshop_id\'] )
return; // Invalid parameter
if ( ! $post = get_post( $id ) )
return; // Invalid post
if ( ! $title = get_field( \'workshop\', $post->ID /* You can pass a post ID as a second arg to ACF get_field */ ) )
return; // No workshop
if ( $post->post_status !== \'publish\' )
return; // Might want to protect access to drafts and the like
// Calendar headers
echo "SUMMARY:$title\\n";