I am creating a wordpress theme for a friend and am trying to add some markup before and after an image that has been inserted using the media button on a post.
Here is what I have so far but it's giving me an error. Any suggestions or help would be much appreciated!
add_filter ('the_content', 'wpse264886_content_insert_html', 10, 1);
function wpse264886_content_insert_html ($content) {
$html1 = 'before ... ';
$html2 = 'after ...';
$content = preg_replace('/(<a.*?)><(img.*?)/', '$1>' . $html1 . '<$2>' . $html2, $content);
return $content;
}
Thanks!
Ponte
Related
I have a problem with getting the post content without the images.
I have a code like this:
$html="";
$html .= '<p class="news-text col-xl-12">'. preg_replace('/\s+?(\S+)?$/', '
',
substr(get_post_field('post_content', $post_id) .'', 0, 250)) .'</p>';
I have also tried:
substr(strip_tags( get_the_excerpt() ),0,250));
but I didn't have any luck with it. Could you please help me out?
i think this is ok
$content = get_post_field('post_content', $post_id);
$content = preg_replace("/<img[^>]+\>/i", "", $content);
I have following code
function source2(){
?> <p>hello world</p> <?php
}
//add beore after content
function wpdev_before_after($content) {
$beforecontent = 'This goes before the content. Isn’t that awesome!';
$aftercontent = source2();
$fullcontent = $beforecontent . $content . $aftercontent;
return $fullcontent;
}
add_filter('the_content', 'wpdev_before_after');
"Hello World" suppose appear after content.
However, it shows before the content instead, even before the $beforecontent.
If I insert plain text rather than calling a function, it works fine.
How can I fix this?
Thanks
Here is the modified version of the output to be displayed as per your requirement. You Need to return the string inorder to concatenate it. Unless the string is returned it will not be displayed.
OUTPUT: First Text/ Center Text /Last Text to be displayed
<?php
function source2(){
$string = 'Last Text to be displayed';
return $string;
}
//add beore after content
function wpdev_before_after($content) {
ob_start();
$beforecontent = 'First Text';
$aftercontent = source2();
$fullcontent = $beforecontent.$content.$aftercontent;
return $fullcontent;
}
add_filter('the_content', 'wpdev_before_after');
$word="/ Center Text /";
echo wpdev_before_after($word);
?>
Hope so you need to start the output buffer and clear the output and print it again. Have a try at this .
function source2(){
?> <p>hello world</p> <?php
}
//add beore after content
function wpdev_before_after($content) {
ob_start();
$beforecontent = 'This goes before the content. Isn’t that awesome!';
$aftercontent = source2();
$fullcontent = $beforecontent . $content . $aftercontent;
$fullcontent = ob_get_clean();
return $fullcontent;
}
add_filter('the_content', 'wpdev_before_after');
I'm working on adding social media buttons on all posts. I am using this code:
function wintersong_custom_buttons($content) {
$new_content = 'Button HTML here.';
$content .= $new_content;
return $content;
}
add_filter('the_content', 'wintersong_custom_buttons');
I have tried to add is_single but then the buttons only appear when you view a post.
I want them to appear only...
when the user is viewing all posts
when the user has clicked on a post and is viewing all content of it
How can I do that?
solution:
function wintersong_custom_buttons($content) {
if( ! is_page() ) {
$new_content = 'Button HTML here';
$content .= $new_content;
$content = preg_replace('/\s+/', ' ', trim($content));
}
return $content;
}
add_filter('the_content', 'wintersong_custom_buttons');
Here is what I'd like to accomplish
Goal A: I want to hyperlink each thumbnail in index.php to their post.
Goal B: I want to define a hyperlink for each thumbnail in single.php to an external website.
You may ask why am I using thumbnails for single.php? The reason is because I want this layout:
And so far I understand that there are 3 methods to display images:
Insert image into the editor area along with the text, but the problem is I cannot float the image and text differently because all items within a post are assigned a p tag - am I wrong?
Custom fields should get the job done but it doesn't seem the most efficient way - or am I wrong?
Post Thumbnails should be the easiest way but see my problem below
I have the code to accomplish Goal A and B but they only work separately.
In other words, "Code 1" does not work if "Code 2" is present.
How can I resolve this issue? Or is there a better method accomplish my goal?
Code 1: Link thumbnails to external websites using custom field (single.php)
<?php $name = get_post_meta($post->ID, 'externalurl', true);
if( $name ) { ?>
<?php the_post_thumbnail(); ?>
<?php } else {
the_post_thumbnail();
} ?>
Code 2: Link thumbnails to the post (functions.php)
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
function my_post_image_html( $html, $post_id, $post_image_id ) {
$html = '' . $html . '';
return $html;
}
is_single() function will help you achieve what you need. Try below code in functions.php and remove the additional code from single.php
function my_post_image_html( $html, $post_id, $post_image_id ) {
if ( is_single()) {
$name = get_post_meta($post_id, 'externalurl', true);
if( $name ) {
$html = '' . $html . '';
}
return $html;
}
else
{
$html = '' . $html . '';
return $html;
}
}
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
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);
:)