Removing iframe tag from feed output - php

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);
:)

Related

Wordpress Plugin Shortcodes - Automatic breaks for content but not for HTML

I am creating a private wordpress shortcode for an accordion, by using a simple plugin, created by myself.
The problem: Wordpress adds breaks and paragraphs to my HTML outcome, which leads to a broken design.
I can remove autobreaks, but then it also does not work for the real content, which has paragraphs. But I dont want to use p-tags in the backend.
How can I stop wordpress from doing autobreaks to my HTML, but still doing it for my wrapped content?
The solution has to work within a plugin - without affecting other plugins/functions.
I can remove any spacing between my shortcodes, so wordpress does not add any breaks. But thats very ugly to read.
This way it works:
[os_accordion][os_spoiler title="Title"]Content with some
breaks.[/os_spoiler][/os_accordion]
This way it does not:
[os_accordion]
[os_spoiler title="Title"]
Content with some
breaks.
[/os_spoiler]
[/os_accordion]
I tried to use a function for removing breaks, but it does not work.
//Helper to remove autops
function cleanup_shortcode_fix($content) {
$array = array('<p>[' => '[', ']</p>' => ']', ']<br />' => ']', ']<br>' => ']');
$content = strtr($content, $array);
return $content;
}
//Outer accordion wrapper
function function_accordion($atts, $content = null){
$html = '<div class="accordion">'.do_shortcode($content).'</div>';
return cleanup_shortcode_fix($html);
}
add_shortcode('os_accordion', 'function_accordion');
//Inner wraps
function function_spoiler($atts, $content = null){
//set default attributes and values
$values = shortcode_atts( array(
'title' => '',
), $atts );
//Output buffer
ob_start();
?>
<div class="toggle"><?php echo esc_attr($values['title']); ?></div>
<div class="content"><?php echo $content; ?></div>
<?php
return ob_get_clean(); //Close buffer and return data
}
add_shortcode('os_spoiler', 'function_spoiler');
Any ideas how I can make the last version work? I'm stuck. :/
This should do it. It's probably just putting p tags after everything.
remove_filter('the_content', 'wpautop');

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;
}

Show favicon only for a first link from that host

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.

How to Remove an HTML tag from XML feed in Wordpress

So I have an xml feed that is for some reason displaying a price tag (I don't even know if this is a valid HTML tag and it is displayed as "price/" in angle brackets of course) under each individual blog post causing the feed not to validate (displaying 65 times). I would like to remove this from the feed and tried adding this piece of code to the functions.php file:
function rss_nopricetag($content) {
$content = preg_replace( '/<price/>/is', '', $content );
return $content;
}
add_filter('the_excerpt_rss', 'rss_nopricetag');
add_filter('the_content_feed', 'rss_nopricetag');
To no avail, I am using the below code to remove the tag from my feed and it is working just fine.
function rss_noiframe($content) {
$content = preg_replace( '/<iframe(.*)\/iframe>/is', '', $content );
return $content;
}
add_filter('the_excerpt_rss', 'rss_noiframe');
add_filter('the_content_feed', 'rss_noiframe');
What could I be doing wrong?
Try escaping the slash in the price regex.
$content = preg_replace( '/<price\/>/is', '', $content);

PHP Matching URLs and using preg_replace_callback()

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

Categories