[英]In Wordpress function.php's hook, how to retrieve the short URL of the current post correctly?
在Wordpress function.php
,我有以下代碼來獲取當前帖子的簡短URL:
add_action('wpcf7_before_send_mail', 'save_cf7_data');
function save_cf7_data($cf)
{
$post_url = 'https://' . $_SERVER[ 'SERVER_NAME' ] . $_SERVER[ 'REQUEST_URI' ];
$post_id = url_to_postid( $post_url );
$post_shorturl = wp_get_shortlink($post_id);
}
過去幾個月來,它一直都能完美運行,沒有任何問題。 但是,聯系表7插件或Wordpress中必須進行一些更改, $post_url
開始返回url,如下所示:
https://example.com/wp-json/contact-form-7/v1/contact-forms/108/feedback
在這種情況下,如何正確檢索當前帖子的簡短URL?
試試這個代碼:
function save_cf7_data($cf) {
$submission = WPCF7_Submission::get_instance();
$cf_url = $submission->get_meta( 'url' );
$cf_postid = url_to_postid( $cf_url );
$cf_shorturl = wp_get_shortlink($cf_postid);
}
add_action('wpcf7_before_send_mail', 'save_cf7_data');
請參閱CF7的源代碼中的文件mail.php。
看看特殊郵件標簽 ,也特別是[_post_url]
。
我將使用get_permalink()來檢索頁面模板中的發布網址。 functions.php
可能在設置全局$post
之前加載,但不太確定。
編輯:
哦,如果您查看獲得的$post_url
,它實際上是WordPress Rest API的URL。 因此,我認為該表單插件曾經用於提交到原始頁面,但現在選擇使用WordPress Rest API。
但是,您的解決方案首先不是正確的方法。 由於這是表單提交,因此應該轉儲$_REQUEST
。 我敢打賭那里有表格的張貼網址信息。 如果默認情況下未提交,則可以在插件表單中添加帖子網址(很可能是隱藏的)字段。
然后這樣嘗試
add_action('wpcf7_before_send_mail', 'save_cf7_data');
function save_cf7_data($cf)
{
global $wp;
$post_url = home_url(add_query_arg(array(),$wp->request));
$post_id = url_to_postid( $post_url );
$post_shorturl = wp_get_shortlink($post_id);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.