2016-12-08 45 views
0

我有一個包含在WordPress網站描述中的電子郵件地址。我想把它作爲一個鏈接。製作一段電子郵件,文本鏈接爲

下面是代碼:

function maker_site_description() { 
    $class = 'site-description'; 
    if (! get_theme_mod('display_blogdescription', true)) { 
     $class .= ' screen-reader-text'; 
    } 
    printf('<p class="%s">%s</p>', esc_attr($class), esc_html(get_bloginfo('description'))); 
} 
endif; 

我怎樣才能使檢測的電子郵件地址中,並使其鏈接到一個mailto?

+0

你指的是電子郵件中嵌入的說明文字?有更多的文字不只是電子郵件?例如*「歡迎來到我的網站!聯繫我,給我發電子郵件[email protected]。玩得開心!」* –

回答

0

鑑於您的代碼,無論如何,電子郵件將被剝離(由於esc_html),所以我們必須將其解決。

然後,在我們將字符串傳遞到您的printf之前,我們必須首先使用正則表達式來解析它以查找電子郵件,並用鏈接替換它。

請參見下面的修改功能:

function maker_site_description() { 
    $class = 'site-description'; 
    if (! get_theme_mod('display_blogdescription', true)) { 
     $class .= ' screen-reader-text'; 
    } 

    // First we have to load the description into a variable 
    $description = get_bloginfo('description'); 

    // This is a regular express that will find email addresses 
    $pattern = '/[a-z\d._%+-][email protected][a-z\d.-]+\.[a-z]{2,4}\b/i'; 

    // Search the description for emails, and assign to $matches variable 
    preg_match($pattern, $description, $matches); 

    // Only make changes if a match has been found 
    if (! empty($matches[0])) { 
     $email = $matches[0]; 
     // Build the "mailto" link 
     $link = '<a href="mailto:' . $email . '">' . $email . '</a>'; 

     // Replace the email with the link in the description 
     $description = str_ireplace($email, $link, $description); 
    } 

    // NOW we can print, but we have to remove the esc_html 
    printf('<p class="%s">%s</p>', esc_attr($class), $description); 
} 
相關問題