2011-06-24 176 views
0

每次用戶訪問wordpress單一頁面頁面時,都會執行腳本update_comments.php。它的工作方式應該是這樣的,但用戶在腳本執行完畢之前不會看到wordpress的帖子或其他任何內容。我只是想在用戶沒有注意到一件事情的情況下啓動腳本。 ignore_user_abort(true)在update_comments.php中打開。加載頁面後運行php腳本

<?php 
global $post; 
$theid= $post->ID; 
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 
preg_match('/http(.*):\/\/(.*)\/(.*)/', 
    $url, $matches4); 
    $url = trim($matches4[2]); 
preg_match('/(.*)\/(.*)/', 
    $url, $matches5); 
    $url = trim($matches5[1]); 
$url = 'http://'.$url.'/update_comments.php?theid='.$theid; 
$myresult = file_get_contents($url); 
?> 
+0

也許這個問題更適合[WordPress的答案](http://wordpress.stackexchange.com)? – 2011-06-24 10:40:33

+0

@Keoki Errr爲什麼會這樣?這適用於產生輸出的所有php頁面,而不僅僅是wordpress。 – natli

回答

1

一種方法是使用JavaScript回調:

<script src="jsCommentsCallback.php?id=<?php echo $post->ID; ?>" type="text/javascript" defer="defer" /> 

加載JavaScript在頁面的底部,有PHP文件(即jsCommentsCallback.php)打你$url,使用$_GET['id']。如果您需要對update_comments.php的內容進行操作,可以將其打包成JSON格式,然後將其發送回瀏覽器以插入頁面或其他內容。