Show favicon only for a first link from that host - php

I wrote a simple wordpress plugin that shows a favicon next to a link in a wordpress page/post content.
add_filter( 'the_content', 'favicon_content_filter', 20 );
/**
* Add a icon to the beginning of every post page.
*/
function favicon_content_filter( $content ) {
$content = preg_replace('/<a href="(.+)">/', '<img src="http://www.google.com/s2/favicons?domain=$1" /> <a href="$1">', $content);
return $content;
}
Now, the client wants to have this favicon displayed next to a link ONLY for the first time when that particular link shows up on a page.
I'm stuck with this for a few hours already. Obviously, I'm not that good with regex. Any help would be highly appreciated

You can use strpos function:
/**
* Add a icon to the beginning of every post page.
*/
function favicon_content_filter( $content ) {
if (strpos($content, "http://www.google.com/s2/favicons") === false) {
$content = preg_replace('~<a href="([^"]+)">~', '<img src="http://www.google.com/s2/favicons?domain=$1" /> <a href="$1">', $content);
}
return $content;
}
This will replace $content only if /s2/favicons isn't there in it already.

Related

oembed_result not running on my Wordpress theme

I implemented the following Wordpress filter in function.php file of my Wordpress theme:
function change_oembed($html, $url, $args){
$video_pattern = "#(\W|^)(youtube)(\W|$)#";
$notification = '<div class="cookieconsent-optout-marketing">Please
accept marketing-cookies
to watch this video.
</div>';
if(preg_match($video_pattern, $url)){
$matches = null;
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches);
$gdpr_youtube = '<iframe data-src="https://www.youtube.com/embed/' .
$matches[1] . '" data-cookieconsent="marketing" frameborder="0"
allowfullscreen></iframe>';
$new_html = '<div class="clearfix"></div>' . $notification . $gdpr_youtube;
} else {
$new_html = $html;
}
return $new_html;
}
add_filter( 'oembed_result', array($this, 'change_oembed'), 999, 3 );
I can see the result of this filter in the post. I only see the youtube.com video URL text. I tried clearing the cache withint wordpress and the browser (using FastCache plugin), I've tried putting the code outside the if-else statement but nothing works.
update: I also tried video_embed_html instead of oembed_result but it still doesn't work.
Update 2: The function is not being called. I just put some question marks just to see if I see them in code, but I don't. So the change_oembed function isn't called at all.
I checked that the regex works, and it does works for that YouTube URL, so it's not the regex issue.

Apply php function to shortcode

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 filter: `content_save_pre` hook fails to replace content only with `preg_replace` function

I need to replace code block fencing within post_content before save. The post content is written in markdown locally, pushed to github and then Wordpress.
I need the markdown fencing ```js <some code> ``` to be replaced with: [js] <some code> [/js] before saving to Wordpress.
See my working repl: https://repl.it/KDz2/1 My function works perfectly fine outside of Wordpress.
Wordpress is invoking the function, but for some reason the replace is failing. I know this because I can get a simple str_replace to work just fine within Wordpress.
Issue;
preg_replace is failing to return the replaced content within Wordpress filter. No errors thrown. Why is this failing?
For reference, my functions.php file includes:
add_filter( 'content_save_pre', 'markdown_code_highlight_fence');
function markdown_code_highlight_fence( $content ) {
$newContent = preg_replace('/^ *(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/m', '
[${2}]
$3
[\\\${2}]
', $content);
return $newContent;
}
Also tried this
function markdown_code_highlight_fence( $content ) {
$newContent = preg_replace_callback('/^ *(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/m', function($match){
$lang = $match[2] == '' ? 'js' : $match[2];
return '
['.$lang.']'
.' '.
$match[3]
.' '.
'[\\'.$lang.']'; }, $content);
return $newContent;
}
Not sure why preg_replace isn't working within Wordpress. If anyone can help shed some light, please do.
In the interim, I have a working solution as follows:
add_filter( 'content_save_pre', 'markdown_code_highlight_fence_replace', 1, 1);
function markdown_code_highlight_fence_replace( $content ) {
preg_match_all('/`{3,}(\S+)?/', $content, $matches);
foreach ($matches[1] as $key=>$match) {
if($match === '') continue;
$content = preg_replace('/`{3,}/', '[/'.$match.']', $content, 2);
$content = str_replace('[/'.$match.']'.$match, '['.$match.']', $content);
}
return $content;
}

get_attachment from functions.php

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...

Removing iframe tag from feed output

I need to remove an iframe tag that is causing my feed not to work. Here is the url of the validator VALIDATOR. The url of the feed is Natural Nigerian Feed. Please could someone tell me what to do. It's been very frustrating. Here is the code I have written to handle it but it is not working
function rss_noiframe($content) {
// filter out iframe here
$content = preg_replace( '/<iframe(.*)/iframe>/is', '', $content );
return $content;
}
add_filter('the_excerpt_rss', 'rss_noiframe');
add_filter('the_content_feed', 'rss_noiframe');
add_filter('the_content_rss', 'rss_noiframe');
I put this code inside the template's function.php but to no avail
this should replace all iframes (upper and lower case)
function rss_noiframe($content) {
// filter out iframe here
$content = preg_replace( '#<iframe[^>]*?>.*?</iframe>#siu', '', $content );
return $content;
}
Try this, it worked for me -
$string = preg_replace('/<iframe.*?\/iframe>/i','', $originalString);
:)

Categories