Add one element after first paragraph - php

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

Related

Wordpress - How to show only two sentences in preview the_excerpt

I need my preview show only two sentences, how can I do it?
add_filter(
'the_excerpt',
function ($excerpt) {
return substr($excerpt,0,strpos($excerpt,'.')+1);
}
);
function excerpt_read_more_link($output) {
global $post;
$output = preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\0', $output);
if(mb_strlen($output) > 300) {
$output = mb_substr($output, 0, 300);
}
return $output;
}
add_filter('the_excerpt', 'excerpt_read_more_link');
I have a code, that make it for first one sentence, but how can I fix it to show two?

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.

Inserting content before <p> tags in PHP

Though this is Wordpress orientated, I think the basis of the PHP is more a Stackoverflow question:
I would like to add a template_part (similar to include()) before the first <p> in a post.
I can't add the template_part before the_content() because inside the the_content() is sometimes a <figure> then <p>:
<figure>....</figure>
// Want to insert here!
<p>.......</p>
<p>.......</p>
<p>.......</p>
<figure>....</figure>
<p>.......</p>
<p>.......</p>
These are the two codes I have tried using, but not sure how to make it go before the first <p>:
Method one:
$after = 1;
$content = apply_filters('the_content', $post->post_content);
if(substr_count($content, '<p>') > $after) {
$contents = explode("</p>", $content); $p_count = 0;
foreach($contents as $content){
echo $content; if($p_count == $after){
get_template_part('path/to/part');
} $p_count++;
}
}
Method two:
$paragraphAfter[1] = get_template_part('path/to/part');
$content = apply_filters( 'the_content', get_the_content() );
$content = explode("</p>", $content);
$count = count($content);
for ($i = 0; $i < $count; $i++ ) {
if ( array_key_exists($i, $paragraphAfter) ) {
echo $paragraphAfter[$i];
}
echo $content[$i] . "</p>";
}
Preferably the more efficient method, either from above or your own :]
A dirty hack could be:
function add_the_string_filter( $content ) {
$string_to_append = 'your string to add';
$content = preg_replace('/<p>/', $string_to_append . '<p>', $content , 1);
return $content;
}
add_filter( 'the_content', 'add_the_string_filter' );
The last parameter of the preg_replace is the limit, which tells to replace only the first occurence.

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

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

Categories