简体   繁体   English

Wordpress中的基本php'if then'URL链接

[英]basic php 'if then' URL link in wordpress

I'm trying to write code that will display a URL link if there one present from a form sumbmission; 我正在尝试编写代码,如果表单提交中存在一个URL链接,它将显示URL链接;

If > [a link exists]

then [display the text 'more info' with the href link wrapped around it]

I've confused myself mixing wordpress and php, and can't quite get it. 我混淆了自己的WordPress和PHP的混合,并不能完全得到它。 Any help would be great. 任何帮助都会很棒。

This question isn't very specific, but the pseudo-code I can offer is this: 这个问题不是很具体,但是我可以提供的伪代码是这样的:

<?php if (isset($_GET['url'])): ?>
    <a href="<?php echo $_GET['url']; ?>">Read more</a>
<?php endif; ?>

Are you looking to do something like this to the comments displayed on a post? 您是否要对帖子中显示的评论执行类似的操作?

Comment: "I like https://www.google.com/ " becomes "I like more info ". 评论:“我喜欢https://www.google.com/ ”变为“我喜欢更多信息 ”。

If that's the case, perhaps adding a filter to functions.php to search for and replace URL might do the trick: 在这种情况下,也许在functions.php .php中添加一个过滤器以搜索和替换URL可能会成功:

// define the get_comment_text callback 
function filter_get_comment_text( $comment_comment_content, $comment, $args ) { 
    // Regular expression to find URL 
    $pattern = '/(https?):\/\/(www\.)?[a-z0-9\.:].*?(?=\s)/i';

    // Replace url with linked "more info"
    $replacement = '<a href="$0">more info</a>';

    // Find matches & replace
    $newcomment = preg_replace($pattern, $replacement, $comment_comment_content);

    // Return the comment
    return $newcomment;
}; 

// add the filter 
add_filter( 'get_comment_text', 'filter_get_comment_text', 10, 3 ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM