how to skip images with certain class in wordpress function - php

I have the following function in my theme's function page. basically what it does is look for any image in the post page and add some spans with css to dynamically create a pinterest button.
function insert_pinterest($content) {
global $post;
$posturl = urlencode(get_permalink()); //Get the post URL
$pinspan = '<span class="pinterest-button">';
$pinurlNew = '<a href="#" onclick="window.open("http://pinterest.com/pin/create/button/?url='.$posturl.'&media=';
$pindescription = '&description='.urlencode(get_the_title());
$options = '","Pinterest","scrollbars=no,menubar=no,width=600,height=380,resizable=yes,toolbar=no,location=no,status=no';
$pinfinish = '");return false;" class="pin-it"></a>';
$pinend = '</span>';
$pattern = '/<img(.*?)src="(.*?).(bmp|gif|jpeg|jpg|png)"(.*?) \/>/i';
$replacement = $pinspan.$pinurlNew.'$2.$3'.$pindescription.$options.$pinfinish.'<img$1src="$2.$3" $4 />'.$pinend;
$content = preg_replace( $pattern, $replacement, $content );
//Fix the link problem
$newpattern = '/<a(.*?)><span class="pinterest-button"><a(.*?)><\/a><img(.*?)\/><\/span><\/a>/i';
$replacement = '<span class="pinterest-button"><a$2></a><a$1><img$3\/></a></span>';
$content = preg_replace( $newpattern, $replacement, $content );
return $content;
}
add_filter( 'the_content', 'insert_pinterest' );
it does everything just fine. but is there a way to have it skip over an image with a certain class name in it like "noPin" ?

I would use preg_replace_callback to check if a matched image contains noPin.
function skipNoPin($matches){
if ( strpos($matches[0], "noPin") === false){
return $pinspan.$pinurlNew.'$matches[2].$matches[3]'.$pindescription.$options.$pinfinish.'<img$1src="$2.$3" $4 />'.$pinend;
} else {
return $matches[0]
$content = preg_replace_callback(
$pattern,
skipNoPin,
$content );
Another image attribute could conceivably contain noPin, if you are concerned about that edge case, just make the test in the if statement more specific.

You have to exclude the class noPin from the $pattern regexp :
$pattern = '/<img(.*?)src="(.*?).(bmp|gif|jpeg|jpg|png)"(.*?) \/>/i';
Has to become something like
$pattern = '/<img(.*?)src="(.*?).(bmp|gif|jpeg|jpg|png)"(.*?) (?!class="noPin") \/>/i';
Please check the regexp syntax, but the idea is to exclude class="noPin" from the searched pattern. Then your replacement will not be added to these images.

Related

PHP regex bug not respecting linebreak

Okay, so I've got something of a weird edge case bug that I can't seem to squash.
I've got a textarea form input where users can type status updates. I've built a method to parse through this and autolink http-links (except for a few domains where I use the Essence library to do some oEmbed magic).
But in a very specific edge case the autolink complete buggers out.
Specifically, when there's url to a subdirectory, without an ending slash, where immediately after the url the user does a carriage return to a new line and keeps typing.
When this happens the first word on the new line is included in the url being matched.
The function looks like this:
function autolink( $text, $attributes=array() ) {
$regex = "/(http|https)\:\/\/[a-z0-9\-\.]+\.[a-z0-9]{2,99}(\/\S*)?/i";
$urls = array();
if( preg_match_all( $regex, $text, $urls, PREG_PATTERN_ORDER ) ) {
foreach($urls[0] as $url) {
$parsed_url = parse_url($url);
if( in_array( $parsed_url['host'], array( 'youtube.com', 'vimeo.com', 'soundcloud.com', 'www.youtube.com', 'www.vimeo.com', 'www.soundcloud.com' ) ) ) {
$essence = Essence\Essence::instance();
$media = $essence->embed( $url );
$text = str_replace($url, '<div class="embed-container">'.$media->html.'</div>', $text);
} else {
$attrs = '';
foreach( $attributes as $attribute => $value ) {
$attrs .= " {$attribute}=\"{$value}\"";
}
$text = str_replace($url,'<a href="'.$url.'"'.$attrs.'>'.$url.'</a>', $text);
}
}
}
$text = '<pre>'.print_r($urls, true).'</pre>'.$text;
$text = trim( $text );
return $text;
}

Joomla 3 content plugn: Foreach preg_replace $row->fulltext duplicates first string

I am setting up a new content plugin for Joomla 3, that should replace plugin tags with html content. Everything works fine till the moment when i am preg_replace plugin tags in $row->fulltext.
Here is the plugin code
public function onContentPrepare($context, &$row, &$params, $page = 0) {
$pattern = '#\{uni\}(.*){\/uni\}#sU';
preg_match_all($pattern, $row->fulltext, $matches, PREG_PATTERN_ORDER);
foreach($matches[1] as $k=>$uni){
preg_match('/\{uni-title\}(.*)[\{]/Ui', $uni, $unititle);
preg_match('/\{uni-text\}(.*)/si', $uni, $unitext);
$titleID = str_replace(' ', '_', trim($unititle[1]));
$newString = '<span id="'.$titleID.'">'.$unititle[1].'</span><div class="university-info-holder"><div class="university-info"><i class="icon icon-close"></i>'.$unitext[1].'</div></div>';
$row->fulltext = preg_replace($pattern,$newString,$row->fulltext);
}
}
Any ideas, why it duplicates first found match, as many times as foreach goes?
Just to mention, if i do:
echo $unititle[1];
inside foreach, items aren't duplicated, but are rendered as it should be.
There are a few problems with the original code.
It should be using $row->text instead of $row->fulltext. This is because when rendering an article Joomla merges tht introtext and fulltext fields.
It's a mistake to use $pattern for the matching when making the substitution. That's because the $pattern matches all of the items. Instead use the $match[0][$k] to do the replacement. Use str_replace instead of preg_replace because now you are matching the exact string and don't need to do a regex.
Here's the code for the whole thing.
class PlgContentLivefilter extends JPlugin{
public function onContentPrepare($context, &$row, &$params, $page = 0) {
return $renderUniInfo = $this->renderUniInfo($row, $params, $page = 0);
}
private function renderUniInfo(&$row, &$params, $page = 0) {
$pattern = '#\{uni\}(.*){\/uni\}#sU';
preg_match_all($pattern, $row->text, $matches);
foreach($matches[0] as $k=>$uni){
preg_match('/\{uni-title\}(.*)[\{]/Ui', $uni, $unititle);
preg_match('/\{uni-text\}(.*)/si', $uni, $unitext);
print_r($unititle[1]);
$title = $unititle[1];
$text = $unitext[1];
if (preg_match('#(?:http://)?(?:https://)?(?:www\.)?(?:youtube\.com/(?:v/|embed/|watch\?v=)|youtu\.be/)([\w-]+)?#i', $unitext[1], $match)) {
$video_id = $match[1];
$video_string = '<div class="videoWrapper"><iframe src="http://youtube.com/embed/'.$video_id.'?rel=0"></iframe></div>';
$unitext[1] = preg_replace('#(?:http://)?(?:https://)?(?:www\.)?(?:youtube\.com/(?:v/|embed/|watch\?v=)|youtu\.be/)([\w-]+)?#i', $video_string, $unitext[1]);
$text = $unitext[1];
}
$titleID = str_replace(' ', '_', trim($title));
$newString = '<span id="'.$titleID.'">'.$title.'</span><div class="university-info-holder"><div class="university-info"><i class="icon icon-close"></i>'.$text.'</div></div>';
$row->text = str_replace($matches[0][$k],$newString,$row->text);
}
}
}

Match and replace with the captured/matched characters

I currently have a code that finds and replaces urls into complete html links. It works fine but now i need to update it so that if there is image url then it should convert it into a html img tag and display it. Function im using now is...
function auto_link_text($text) {
$pattern = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';
$callback = create_function('$matches', '
$url = array_shift($matches);
$url_parts = parse_url($url);
return sprintf(\'<a rel="nowfollow" target="_blank" href="%s">%s</a>\', $url, $url);
');
return preg_replace_callback($pattern, $callback, $text);
}
Got it from...
How to add anchor tag to a URL from text input
Here is an example of the text i would to it to go through...
asdf
http://google.com/
asfd
http://yahoo.com/logo.jpg
http://www.apple.com/sdfsd.php?page_id=13&id=18210&status=active#1
http://youtube.com/logo.png
like it updated function to output...
asdf
<a rel="nowfollow" target="_blank" href="http://google.com/">http://google.com/</a>
asfd
<img src="http://yahoo.com/logo.jpg" class="example">
<a rel="nowfollow" target="_blank" href="http://www.apple.com/sdfsd.php?page_id=13&id=18210&status=active#1">http://www.apple.com/sdfsd.php?page_id=13&id=18210&status=active#1</a>
<img src="http://youtube.com/logo.png" class="example">
Big thanks in advance!
You can use this for example:
function create_anchor_tag($url, $text = false) {
if ($text===false) $text = $url;
return '<a rel="no-follow" target="_blank" href="' . $url . '">'
. $text . '</a>';
}
function create_image_tag($url) {
return '<img src="' . $url . '"/>';
}
function auto_link_text($text) {
$pattern = '~\b(?:(?:ht|f)tps?://|www\.)\S+(?<=[\PP?])~i';
$callback = function ($m) {
$img_ext = array('jpg', 'jpeg', 'gif', 'png');
$path = parse_url($m[0], PHP_URL_PATH);
$ext = substr(strrchr($path, '.'), 1);
if (in_array(strtolower($ext), $img_ext))
return create_image_tag($m[0]);
return create_anchor_tag($m[0]);
};
return preg_replace_callback($pattern, $callback, $text);
}
I used several functions to make it more clea[rn], but you can easily adapt it as you like.
Here is the nice post about the best suitable regex pattern for valid URL. I picked one from there to group all the URLs.
Online demo
Steps to follow:
simply extract the url.
put a check on the url and based on your own logic substitute the tag as shown in demo.
sample code: (get all the valid urls in groups. get it from index 1)
$re = "/(([A-Za-z]{3,9}:(?:\\/\\/)?(?:[-;:&=\\+\\$,\\w]+#)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\\+\\$,\\w]+#)[A-Za-z0-9.-]+)((?:\\/[\\+~%\\/.\\w-_]*)?\\??(?:[-\\+=&;%#.\\w_]*)#?(?:[\\w]*))?)/";
$str = "...";
preg_match_all($re, $str, $matches);
sample code: (substitute anchor tag (or what ever you want to add))
$re = "/(([A-Za-z]{3,9}:(?:\\/\\/)?(?:[-;:&=\\+\\$,\\w]+#)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\\+\\$,\\w]+#)[A-Za-z0-9.-]+)((?:\\/[\\+~%\\/.\\w-_]*)?\\??(?:[-\\+=&;%#.\\w_]*)#?(?:[\\w]*))?)/";
$str = "...";
$subst = '$1';
$result = preg_replace($re, $subst, $str);

Custom wp_strip_all_tags() WordPress function

I would like to include line breaks on my WordPress excerpts.
To accomplish this, I see that I can change this function:
function wp_strip_all_tags($string, $remove_breaks = false) {
$string = preg_replace( '#<(script|style)[^>]*?>.*?</\\1>#si', '', $string );
$string = strip_tags($string);
if ( $remove_breaks )
$string = preg_replace('/[\r\n\t ]+/', ' ', $string);
return trim( $string );
}
to:
function wp_strip_all_tags_breaks($string, $remove_breaks = false) {
$string = preg_replace( '#<(script|style)[^>]*?>.*?</\\1>#si', '', $string );
$string = strip_tags($string, '<p>');
if ( $remove_breaks )
$string = preg_replace('/[\r\n\t ]+/', ' ', $string);
return trim( $string );
}
What is the best way to modify my theme to switch functions and provide this functionality?
Overriding/overloading any of the WordPress core functions has to be done in the functions.php of your current theme.
First you have to define the new function in the functions.php (the name should be different from the original wpcore function name) and then you have remove the old function and add the new function to the respective hook/filter.
In case of the_excerpt() it should be done like this:
function new_function() {
//code here
}
remove_filter('get_the_excerpt', 'old_function');
add_filter('get_the_excerpt', 'new_function');
Hope that makes sense.
EDIT: Here is a good tutorial on how to edit the_excerpt() formatting.

Regex: Modify img src tags

I try to replace all images in a html document with inline image (data:image).
I've a sample code which does not work:
function data_uri($filename) {
$mime = mime_content_type($filename);
$data = base64_encode(file_get_contents($filename));
return "data:$mime;base64,$data";
}
function img_handler($matches) {
$image_element = $matches[1];
$pattern = '/(src=["\'])([^"\']+)(["\'])/';
$image_element = preg_replace($pattern, create_function(
$matches,
$matches[1] . data_uri($matches[2]) . $matches[3]),
$image_element);
return $image_element;
}
$content = (many) different img tags
$search = '(<img\s+[^>]+>)';
$content = preg_replace_callback($search, 'img_handler', $content);
Could somebody check this code? Thanks!
UPDATE:
(...) Warning file_get_contents() [function.file-get-contents]: Filename cannot be empty (...)
That means the src url is not in the handler :(
UPDATE 2
<?php
function data_uri($filename) {
$mime = mime_content_type($filename);
$data = base64_encode(file_get_contents($filename));
return "data:$mime;base64,$data";
}
function img_handler($matches) {
$image_element = $matches[0];
$pattern = '/(src=["\'])([^"\']+)(["\'])/';
$image_element = preg_replace_callback($pattern, create_function(
$matchess,
$matchess[1] . data_uri($matchess[2]) . $matchess[3]),
$image_element);
return $image_element;
}
$content = '<img class="alignnone" src="http://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Googlelogoi.png/180px-Googlelogoi.png" alt="google" width="580" height="326" title="google" />';
$search = '(<img\s+[^>]+>)';
$content = preg_replace_callback($search, 'img_handler', $content);
echo $content;
?>
I've upload this test file -> http://goo.gl/vWl9B
Your regex is alright. You are using create_function() wrong. And subsequently the inner preg_replace_callback() doesn't work. The call to data_uri() happens before any regex-replacement takes place, hencewhy the undefined filename error.
Use a proper callback function:
$image_element = preg_replace_callback($pattern, "data_uri_callback", $image_element);
Then move your code into there:
function data_uri_callback($matchess) {
return $matchess[1] . data_uri($matchess[2]) . $matchess[3];
}

Categories