I am trying to separate out the different parts of my wordpress posts (at this point specifically the blockquote) so I can put them in different divs so I can put them on different parts of the page.
I have been able to remove the blockqoute and post the content without it, but couldn't put it back in
<div1>
<?php add_filter( 'the_content', 'rm_quotes' );
function rm_quotes($content) {
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
return $content;
}
?>
</div1>
<div2>
<!-- This is where I want to put the <blockquote> -->
</div2>
Do a preg_match and put it into a variable called $quotes and then remove it from content with preg_replace. Now you can do whatever you want with the blockquotes that were removed.
example code:
preg_match('/<blockquote>([\s\S]+?)</blockquote>/', $content, $quotes);
var_dump($quotes);
I wanted to remove the images from a WordPress post to give me more control of how I could layout the design for the front page of a website. After doing some messing around and failing, I finally found a great post which had an amazing little piece of code that solved my problem.
<?php
$content = get_the_content();
$postOutput = preg_replace('/<img[^>]+./','', $post->post_content);
echo $postOutput;
?>
But some time i have link above images like:
<img src="PATH_IMAGES">
So, How can remove it?
Change above code by following code which remove link if exist into your content.
$content = get_the_content();
$postOutput = preg_replace('/<img[^>]+./','', $post->post_content);
$postContent = preg_replace("/<a[^>]+\>/i", "", $postOutput);
echo $postContent;
I am trying to pull content from one page into another. I could be going about this the wrong way, but I am just trying to get the general method of how it is done. This is what I have so far trying to get the data I need. I am not getting the permalink or the image. The other issue is when applying the substr method to the $content variable it actually cuts the html tags off to making it to where anything after it gets nested into whatever html happens to be in the content. I appreciate any tips you may have. Here is the function I created to try and pull the content in it's initial stage.
function show_post($path) {
$post = get_page_by_path($path);
$title = apply_filters('the_title', $post->post_title);
$image = apply_filters('featured_image', $post->the_post_thumbnail);
$content = apply_filters('the_content', $post->post_content);
$link = apply_filters('the_permalink', $post->get_permalink);
echo '<h3>'.$title.'</h3>';
echo substr($content, 0, 300) . '...';
}
$path is the relative url of the website to where I am pulling content so lets say my url i want to grab data is www.test.com/about/bio I would make the path be about/bio.
I appreciate your help in this.
I am calling in content in Wordpress via the below code. Eseentially, I am dividing the content of the post into three sections; 1. Before the tag, 2. After the tag and 3. Post gallery. The code I have so far works perfectly to get the content, however I am having an issue as all formatting tags (p in particular) are being stripped. Is there a way to retain these?
Thanks
<?php
// Fetch post content
$content = get_post_field( 'post_content', get_the_ID() );
// Get content parts
$content_parts = get_extended( $content );
?>
<p>
<?php echo $content_parts['main']; // Output content before <!--more--> ?>
</p>
<p class="read-more">
<?php echo strip_shortcodes($content_parts['extended']); // Output content after <!--more--> ?>
</p>
<button>Read More</button>
<?php $gallery = get_post_gallery_images( $post ); ?>
When you pull the post content using get_post_field, the autop filter is not applied:
http://codex.wordpress.org/Function_Reference/wpautop
You can apply all of the content filters yourself by adding this line after you set $content:
$content = apply_filters('the_content', $content);
Looking for a php function (non-jQuery or wpautop modification) approach to remove <p></p> from within wordpress.
I tried this but it does not work:
function cleanup_shortcode_fix($content) {
$array = array (
'<p>[' => '[',
']</p>' => ']',
']<br />' => ']',
']<br>' => ']'
);
$content = strtr($content, $array);
return $content;
}
add_filter('the_content', 'cleanup_shortcode_fix');
Try inserting this code in your functions.php file:
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop', 99 );
add_filter( 'the_content', 'shortcode_unautop', 100 );
add_filter('the_content', 'cleanup_shortcode_fix', 10);
I found that it works if you specify 10 as the priority; no other number will work.
This is an old question, but I solved this today and thought I'd share.
In my case, I basically want to remove all the poorly formatted <p> and <br> tags, but then you want to add them back in correctly so that the text in the shortcode gets formatted correctly.
/*
* Half column shortcode
*/
function custom_shortcode_half_column( $atts, $content = '') {
$content = custom_filter_shortcode_text($content);
return '<div class="half-column">'. $content .'</div>';
}
add_shortcode( 'half-column', 'custom_shortcode_half_column' );
/*
* Utility function to deal with the way WordPress auto formats text in a shortcode.
*/
function custom_filter_shortcode_text($text = '') {
// Replace all the poorly formatted P tags that WP adds by default.
$tags = array("<p>", "</p>");
$text = str_replace($tags, "\n", $text);
// Remove any BR tags
$tags = array("<br>", "<br/>", "<br />");
$text = str_replace($tags, "", $text);
// Add back in the P and BR tags again, remove empty ones
return apply_filters('the_content', $text);
}
This really should be the default way WordPress parses the shortcode $content parameter in my opinion.
maybe a regex could work:
$string=preg_replace_('/<p>\s*</p>/', '', $string);
That should replace any <p></p> with nothing or just whitespaces in it to nothing, thus removing them.
When applying regex to HTML code it's a good idea to remove the \r\n of the HTML first, since they stop the regex from working.
You should increase the priority of the filter.
This should work
add_filter('the_content', 'cleanup_shortcode_fix', 1);
You can remove Tag enter
<?php echo $post->post_content; ?>
instead of the_content()
What you need is a mix of jquery and php... that is the only working way
that i found to be working really well. i have tutorial on my site but
in order to keep stuff inhouse here goes
The jQuery:
include this in some JS file you already enqueue
jQuery(function($){
$('div#removep > p').filter(function() {
return $.trim($(this).text()) === '' && $(this).children().length == 0
})
.remove()
})
A shortcode you can later use:
in your functions.php or included file
function sght_removep( $atts, $content = null ) {return '<div id="removep">'.do_shortcode($content).'</div>';}
add_shortcode('removep', 'sght_removep');
Now you can wrap specific stuff like this:
[removep]
Some text i write directly in wordpress wysiwyg
<p></p> <-- this would get removed
[/removep]
This solution requires some know how but it works!
Hope this helps...