How to remove iframe when have a post id in wordpress? - php

I have a simple code:
function filter_widgets($content) {
global $post;
if($post->ID = 1210) {
$content = preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '', $content);
}
return $content;
}
add_filter('the_content', 'filter_widgets');
But when run postId=1210 is result can't iframe, how to fix it?

try this
$content = preg_replace("/(<iframe[^<]+<\/iframe>)/", '', $content);

Related

Add one element after first paragraph

I've the following code for adding advertisement before the_content of posts in my WordPress website.
<?php if (!empty($smof_data['ads_entry_top'])) { ?>
<div class="entry-img-300"><?php echo stripslashes($smof_data['ads_entry_top']); ?></div>
<?php } ?>
I want it to be added after the first paragraph, so wrote this function into functions.php:
add_filter( 'the_content', 'insert_after_first', 20 );
function insert_after_first( $content ) {
$content = preg_replace( "/<\/p>/", "</p>" . stripslashes($smof_data['ads_entry_top']) , $content, 1 );
return $content;
}
But it does nothing. What is wrong?
I changed my method and now add some content after first paragraph with below function and filter. I add it here maybe it helps someone:
add_filter('the_content', 'ata_the_content_filter', 10, 1);
function ata_the_content_filter($content)
{
$parags = explode('</p>', $content);
$parags[0] .= '<br>hello';// add whatever you want after first paragraph
$content_new = '';
foreach ($parags as $parag) {
$content_new .= $parag;
}
return $content_new;
}

wordpress custom excerpt length not working

I have this code in my functions.php
function custom_excerpt_length() {
return 15;
}
add_filter('excerpt_length', 'custom_excerpt_length');
but it doesn't work as it gives me the full text and not the 15 words I specified. And the grid I have set up on my website is not right because of this.
my movie grid
Thanks in advance
Also use this code for multiple type of getting excerpt
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
then in your template code you just use..
<?php echo excerpt(25); ?>
Add third parameter and also you have not mention function parameter it will be worked
function custom_excerpt_length( $length ) {
return 15;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
I've faced this problem too, your code is right.
To solve this just go to Wordpress dashboard > posts > all posts then click edit in the post that has the problem, in the editor make sure that the excerpt is empty then click update.
Click here to see the image

Shortcodes function

Here the problem:
I have two div #post-playlist-list and post-playlist-description
I want to use two shortcodes [texte-article] and [playlist-article]
I need a function which get all the content between[texte-article][/texte-article] to put in #post-playlist-list
and another function which get the [playlist-article][/playlist-article] content into #post-playlist-description
I began to learn at preg_replace but the syntax is not that easy to learn.
I you can help me on this, it would be great.
Ok i tried the function i don't have error but it doesn't seems to return anything
Don't i need to escape the second ] ion the Regexp? like "/[texte-article](.*)[/texte-article]/"
Here's the code in my single.php :
<div id="post-playlist-list" class="box">
<?php
$id = get_the_ID();
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
$content = str_replace( ']]>', ']]>', $content );
echo $content;
?>
</div>
<div id="post-playlist-description" >
<?php
echo $content;
?>
</div>
Here's the code in my single.php:
function texte_article_function() {
preg_replace_callback("/\[texte-article\](.*)\[\/texte-article\]/", function($matches) {
//$matches contains an array of all matches
//$matches[0] stores the full pattern match: \[texte-article](.*)\[\/texte-article]
//$matches[1] stores the first subpattern match: (.*)
//Do whatever text parsing you need to do here and return the result.
$content = $matches[0];
return $content;
}, $postdata);
}
function playlist_article_function() {
preg_replace_callback("/\[playlist-article\](.*)\[\/playlist-article\]/", function($matches) {
//$matches contains an array of all matches
//$matches[0] stores the full pattern match: \[texte-article](.*)\[\/texte-article]
//$matches[1] stores the first subpattern match: (.*)
//Do whatever text parsing you need to do here and return the result.
$content = $matches[0];
return $content;
}, $postdata);
}
function register_shortcodes(){
add_shortcode('texte-article', 'texte_article_function');
add_shortcode('playlist-article', 'playlist_article_function');
}
add_action( 'init', 'register_shortcodes');
Thanks for all the answers :) here's the solution, using regex:
functions.php
function texte_article_function($content_texte_article) {
//Get [texte-article] content
$content_texte_article = preg_replace('#.*\[texte-article\](.*)\[\/texte-article\].*#s', '$1', $content_texte_article);
return $content_texte_article;
}
function playlist_article_function($content_playlist_article) {
//get [fap_playlist] content
$content_playlist_article = preg_replace('#.*\[playlist-article\](.*)\[\/playlist-article\].*#s', '$1', $content_playlist_article);
return $content_playlist_article;
}
Callback functions:
<?php
$id = get_the_ID();
$post = get_post($id);
$content_playlist_article = $content;
$btn_play_index_id = $content;
echo playlist_article_function($content_playlist_article);
// echo $content;
?>
<?php
$content_texte_article = $content;
echo texte_article_function($content_texte_article);
?>
If it can help someone.

Wordpress PHP - need to retain nextpage paginating tag in custom query

Preface: i am not a good coder.
I need to use $post->post_content to get the raw post so that i can use the EXPLODE php command. But when i do use $post->post_content, it filters out the tags that are in my post which need to be retained. here is my script. what am i doing wrong? thanks!
<?php
$content = apply_filters('the_content', $post->post_content);
$contents = array_filter(explode("</p>", $content));
foreach ($contents as $content) {
if (strpos($content, '<img') !== false ) {
echo $content;
echo "</p>after image ad";
} else {
echo $content;
echo "</p>";
}
}
?>
I'm basically trying to insert an Ad after any paragraph that only contains an image.
It seems that when you call:
$content = apply_filters('the_content', $post->post_content);
It applies autop to split the paragraphs, and also applies do_shortcode on all shortcodes.
So you'd better not to call apply_filters here, but call wpautop instead:
See: http://codex.wordpress.org/Function_Reference/wpautop
<?php
$content = wpautop( $post->post_content );
$contents = array_filter(explode("</p>", $content));
$result = '';
foreach ($contents as $content) {
$result .= $content.'</p>';
if (strpos($content, '<img') !== false ) {
$result .= "after image ad";
}
}
$content = apply_filters('the_content', $result);
echo $result;
?>

Automatically removing 'p' tags from the HTML

I have the following code, which supposedly removes all p tags that are wrapped around images. I am literally copying and pasting this into my functions.php. However, it's not working:
function filter_ptags_on_images($content){
return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
add_filter('the_content', 'filter_ptags_on_images');
Do I need to change the function parameter with something more relevant to my theme? I'm new to WordPress so apologies for what might seem like a silly question.
You can disable this feature by adding this code in your functions.php
remove_filter( 'the_content', 'wpautop' ); // Disable auto 'p' in content
remove_filter( 'the_excerpt', 'wpautop' ); // Disable auto 'p' in excerpt
remove_filter()
wpautop
wouldn't this work?
function filter_ptags_on_images($content){
return preg_replace('/\<p\b.*?\>(.+)\<\/p\>/i', '$1', $content);
}
edit:
my bad, sorry I wasn't reading carefully
function filter_ptags_on_images($content){
if (!preg_match_all('/\<p\b.*?\>(.+?)\<\/p\>/is',$content,$ps)) {
return $content;
}
$new_content = $content;
foreach ($ps[0] as &$p) {
if (strpos($p,"<img ")) {
$p_stripped_chunk = preg_replace('/\<p\b.*?\>(.*?\<img\b.+\>.*?)\<\/p\>/is', '$1', $p);
$new_content = str_replace($p,$p_stripped_chunk,$new_content);
}
}
return $new_content;
}
edit:
this is another better version I think:
function filter_ptags_on_images($content){
if (!preg_match_all('/\<p\b.*?\>(.*?)\<\/p\>/is',$content,$ps_with_image)) {
return $content;
}
foreach ($ps_with_image[0] as $match_x => $p) {
if (!stripos($p,'<img')) {
unset($ps_with_image[0][$match_x],$ps_with_image[1][$match_x]);
}
}
return str_replace($ps_with_image[0], $ps_with_image[1], $content);
}
edit:
this is much much better version of this:
function filter_ptags_on_images($content){
if (!preg_match_all('/\<p\b[^\>]*?\>(([^\<]|\<(?!\/p))*?\<img\b.+?\>.*?)\<\/p\>/is',$content,$ps_with_image)) {
return $content;
}
return str_replace($ps_with_image[0], $ps_with_image[1], $content);
}
var context = $("p > img");
for (var i = 0; i < context.length; i++) {
$(context[i]).remove();
}
This will remove all the <p> tags around images along with their content.
filter the P tags from the images and iFrame
function filter_ptags_on_images($content)
{
$content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
return preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content);
}
add_filter('the_content', 'filter_ptags_on_images');

Categories