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...
Related
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.
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;
}
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.
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
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);
:)