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.
Related
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.
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);
:)
I use the following to find all URLĀ“s inside $content
$content = preg_match_all( '/(http[s]?:[^\s]*)/i', $content, $links );
But this will depend on the http:// part in http://www.google.com/some/path .
My questions are :
1 - How can I modify it in order to hit also the links that are start with only www , e.g. www.google.com?
2 - The main aim is to find the links, and replace them with a value that is returned from another function. I tried preg_match_callback() , but it is not working (probably using it wrong ..
$content = preg_replace_callback(
"/(http[s]?:[^\s]*)/i",
"my_callback",
$content);
function my_callback(){
// do a lot of stuff independently of preg_replace
// adding to =.output...
return $output;
}
Now , in my logic (which is probably wrong ) all matches from the $content would be replaced by $output. what am I doing wrong ?
(please no anonymous functions - i am testing on an old server)
EDIT I - after comments , trying to clarify with more details
function o99_simple_parse($content){
$content = preg_replace_callback( '/(http[s]?:[^\s]*)/i', 'o99_simple_callback', $content );
return $content;
}
callback :
function o99_simple_callback($url){
// how to get the URL which is actually the match? and width ??
$url = esc_url_raw( $link );
$url_name = parse_url($url);
$url_name = $description = $url_name['host'];// get rid of http://..
$url = 'http://something' . urlencode($url) . '?w=' . $width ;
return $url; // what i really need to replace
}
To modify the regex you already have to allow URLs that begin with www, you'd simply write this:
/((http[s]?:|www[.])[^\s]*)/i
+ ++++++++
I'm trying to use PHP to trim a YouTube URL down to the video ID. It's working, but it's also adding a lot of whitespace to the result. Does anyone know how I'd be able to fix this? Here's the setup:
Located in Wordpress single.php (finds attached YouTube URL from post meta):
<?php
$vidurl = get_post_meta($post->ID, "_videoembed", true );
$youtube_id = getYouTubeIdFromURL($vidurl);
echo $youtube_id;
?>
Here's what's executing the function:
//Get YT Video ID
function getYouTubeIdFromURL($url)
{
$url_string = parse_url($url, PHP_URL_QUERY);
parse_str($url_string, $args);
return isset($args['v']) ? $args['v'] : false;
}
Like I said, the function works fine, but it produces a bunch of white space. Say the video included in the meta is youtube.com/watch?v=1337&feature=player_embedded it'll return
" 1337 "
I appreciate all help in advance! I've had some really nice people help me on this site before.
Are you sure your get_post_meta function isn't returning a bunch of spaces with it? Can you do a var_dump on $vidurl? When I run the function you gave, the output looks just fine (http://www.ideone.com/LRoTd):
<?php
function getYouTubeIdFromURL($url)
{
$url_string = parse_url($url, PHP_URL_QUERY);
parse_str($url_string, $args);
return isset($args['v']) ? $args['v'] : false;
}
$theurl = "youtube.com/watch?v=1337&feature=player_embedded";
$youtubeid = getYouTubeIdFromURL($theurl);
echo "'$youtubeid'";
?>
Output:
'1337'