Please help I am using MYBB forum and I want to add rel="nofollow" to all the outgoing links but not internal.
This code works but it ads nofollow to all the links how to make it check if the link is internal and if not add nofollow
function nofollowlink_changelink($message){
$search = '<a';
$replace = '<a rel="nofollow"';
$message = str_replace($search , $replace , $message);
return $message;
}
I wan't this code not to add nofollow to $mybb->settings['bburl']
Also here you have some more code to play with.
function nofollowexternal_changelink($message) {
global $mybb;
$message = preg_replace_callback("/<a href=\"(.+)\" (.+)>(.+)<\\/a>/", "replace_external", $message);
return $message;
}
function replace_external($groups) {
global $mybb;
$url = str_replace(array(".", "/"), array("\\.", "\\/"), $mybb->settings['bburl']);
$urls = preg_replace("/^http/","https", $url, 1);
if (preg_match("/$url/", $groups[1]) || preg_match("/$urls/", $groups[1])) {
return $groups[0];
}
else {
return "".$groups[3]."";
}
}
This code doesn't work if URLs are post in this format: URL, URL, URL, URL it only adds no follow to the last one.
Please help
Related
i'm trying to do something in PHP
I'm trying to get the link of an image -> store it to my DB, but I'd like the user to be able to store text before it, and after it, I've gotten my hands on a similar function for links, but the image part is missing.
As you can see the turnUrlIntoHyperlink does a regex check over the entire arg passed, turning the text that contains it to the url, so users can post something like
Hey check this cool site "https://stackoverflow.com" its dope!
And the entire argument posting to my database.
However i can't seem to get the same function working for the Convert Image, as it simply won't post and removed text before/after it before when i made the attempt.
How would i do this in a correct way, and can i combine these 2 functions in to 1 function?
function convertImg($string) {
return preg_replace('/((https?):\/\/(\S*)\.(jpg|gif|png)(\?(\S*))?(?=\s|$|\pP))/i', '<img src="$1" />', $string);
}
function turnUrlIntoHyperlink($string){
//The Regular Expression filter
$reg_exUrl = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
// Check if there is a url in the text
if(preg_match_all($reg_exUrl, $string, $url)) {
// Loop through all matches
foreach($url[0] as $newLinks){
if(strstr( $newLinks, ":" ) === false){
$link = 'http://'.$newLinks;
}else{
$link = $newLinks;
}
// Create Search and Replace strings
$search = $newLinks;
$replace = ''.$link.'';
$string = str_replace($search, $replace, $string);
}
}
//Return result
return $string;
}
more explained in detail :
When i post a link like https://google.com/ I'd like it to be a href,
But if i post an image like https://image.shutterstock.com/image-photo/duck-on-white-background-260nw-1037486431.jpg , i'd like it to be a img src,
Currently, i'm storing it in my db and echoing it to a little debug panel,
Do you mean that you want to make an <img> inside <a> element?
Your turnUrlIntoHyperlink function have captured the url successfully, so we can just use explode to get string before and after the link.
$exploded = explode($link, $string);
$string_before = $exploded[0];
$string_after = $exploded[1];
Code example:
<?php
function turnUrlIntoHyperlink($string){
//The Regular Expression filter
$reg_exUrl = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
// Check if there is a url in the text
if(preg_match_all($reg_exUrl, $string, $url)) {
// add http protocol if the url does not already contain it
$newLinks = $url[0][0];
if(strstr( $newLinks, ":" ) === false){
$link = 'http://'.$newLinks;
}else{
$link = $newLinks;
}
$exploded = explode($link, $string);
$string_before = $exploded[0];
$string_after = $exploded[1];
return $string_before.'<img src="'.$link.'">'.$string_after;
}
return $string;
}
echo turnUrlIntoHyperlink('Hey check this cool site https://stackoverflow.com/img/myimage.png its dope!');
Output:
Hey check this cool site <img src="https://stackoverflow.com/img/myimage.png"> its dope!
Edit: the question has been edited
Since an image URL is just another kind of link/URL, your logic should go like this pseudocode:
if link is image and link is url
print <img src=link> tag
else if link is url and link is not image
print <a href=link> tag
else
print link
So you can just write a new function to "merge" those two function:
function convertToImgOrHyperlink($string) {
$result = convertImg($string);
if($result != $string) return $result;
$result = turnUrlIntoHyperlink($string);
if($result != $string) return $result;
return $string;
}
echo convertToImgOrHyperlink('Hey check this cool site https://stackoverflow.com/img/myimage.png its dope!');
echo "\r\n\r\n";
echo convertToImgOrHyperlink('Hey check this cool site https://stackoverflow.com/ its dope!');
echo "\r\n\r\n";
Output:
Hey check this cool site <img src="https://stackoverflow.com/img/myimage.png" /> its dope!
Hey check this cool site https://stackoverflow.com/ its dope!
The basic idea is that since image url is also a link, such check must be done first. Then if it's effective (input and return is different), then do <img> convertion. Otherwise do <a> convertion.
This is my custom function:
add_filter('post_link', 'get_custom_permalink', 10, 4);
function get_custom_permalink($permalink, $post, $leavename = false) {
$options = get_option('my-options');
$custom_field = $options['my_field'];
//$permalink = get_option('permalink_structure');
$url = $options['agg_permalink'];
$url = str_replace("%post%", get_post_permalink($post), $url);
$url = str_replace("%site%", get_site_url(), $url);
$url = str_replace("%home%", get_home_url(), $url);
if (!empty($url) ) {
return $url;
} else {
return $permalink;
}
}
get_post_permalink($post) is allways using Plain url the default wordpress permalink option even if Post name is selected in my permalink settings.
so the url in browser is example.com/post_type=post&p=ID but I want to get the post name when I use %post% in the field I created.
When I use get_permalink or get_the_permalink I get blank wordpress pages. Is there anything else that I can try?
Thankyou.
Use the_permalink() function in loop whenever you want to use link and use /%postname%/ instead of %post% in permalink setting.
I'll start by saying I'm fairly new to coding so I'm probably going about this the wrong way.
Basically I've got the below php function that changes urls to the page title of the url instead of a plain web address. So instead of www.google.com it would appear as Google.
<?php
function get_title($url){
$str = file_get_contents($url);
if(strlen($str)>0){
$str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside <title>
preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
return $title[1];
}
}
?>
This is great but to implement this I have to use the below code.
echo get_title("http://www.google.com/");
However this just works on a predefined URL. What I have set up on my site at the moment is a shortcode in a html widget.
<a href='[rwmb_meta meta_key="link_1"]'>[rwmb_meta meta_key="link_1"]</a>
This shortcode displays a url/link that is input by the user in the backend of Wordpress and displays it on the frontend as a link. However I want to apply the get_title function to the above shortcode so instead of the web address it shows the page title.
Is this possible?
Thanks in advance.
for name of a url from a link you can use parse_url($url, PHP_URL_HOST);
easier way would be to have an array of links for example
$links[] = 'some1 url here';
$links[] = 'some2 url here';
then just loop your $links array with the function.
foreach($links as $link)get_title($link);
https://metabox.io/docs/get-meta-value/
try:
$files = rwmb_meta( 'info' ); // Since 4.8.0
$files = rwmb_meta( 'info', 'type=file' ); // Prior to 4.8.0
if ( !empty( $files ) ) {
foreach ( $files as $file ) {
echo $file['url'];
}
}
WordPress automatically converts a YouTube URL in the content of a page/post to a embedded iframe video.
It respects the start parameter, if present, in the YouTube URL, but it does not respect the end parameter, if present.
I therefore need to locate the WordPress code that handles this automatic YouTube embed functionality so I can, hopefully, hook in my own filter that (using this solution) will take care of the end requirements.
I have searched through the class-wp-embed.php, class-oembed.php and media.php files of the /wp-includes/ directory and, in the latter, thought I had found the code I needed...
apply_filters( 'wp_embed_handler_youtube', $embed, $attr, $url, $rawattr )
...but that filter doesn't seem to get called.
Can anyone point me in the right direction?
I had same problems and not found answer. So here is working solution:
add_filter('embed_oembed_html', 'my_theme_embed_handler_oembed_youtube', 10, 4);
function my_theme_embed_handler_oembed_youtube($html, $url, $attr, $post_ID) {
if (strpos($url, 'youtube.com')!==false) {
/* YOU CAN CHANGE RESULT HTML CODE HERE */
$html = '<div class="youtube-wrap">'.$html.'</div>';
}
return $html;
}
You can customize the youtube url and set various condition.I have implemented it in past.You may get some reference from the below code:
if(strpos($url, "youtube")!==false)
{
if(strpos($url, "<object")===false)
{
if(strpos($url, "<iframe")===false)
{
if(strpos($url, "//youtu.be/")===false)
{
$url_string = parse_url($url, PHP_URL_QUERY);
parse_str($url_string, $args);
$videoId = isset($args['v']) ? $args['v'] : false;
}
else
{
$url_string = explode('/',$url);
$videoId = $url_string[3];
}
}
else
{
$pattern = '!//(?:www.)?youtube.com/embed/([A-Za-z0-9\-_]+)!i';
$result = preg_match($pattern, $url, $matches);
$videoId = $matches[1];
}
}
else
{
preg_match('#<object[^>]+>.+?http://www.youtube.com/v/([A-Za-z0-9\-_]+).+?</object>#s', $url, $matches);
$videoId = $matches[1];
}
$urlfrom = 'youtube';
$video_thumb= '';
}
I'm trying to open an image with a lightbox (colorbox) and when it did, have a button that opens a link to it's attachment-page.
I made the attribute data-att="" were the link to the attachment-page should get.
Here is what i have in functions.php right now:
add_filter('the_content', 'add_colorbox');
function add_colorbox($content) {
global $post;
$att_url = wp_get_attachment_link($id);
$pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
$replacement = '<a$1href=$2$3.$4$5 class="fotos" data-att="'. $att_url .'" title="'.$post->post_title.'"$6>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
So with $att_url = get_attachment_link($id); im trying to get the attachment link and pass it to the data-att="..." with no luck yet...