Give Out Wordpress post_content? - wordpress

My problem is that I want give out post_content, but the output is with raw shortcodes and HTML.
I need an output with the correct translating.
A link should be a link and not a href, but only normal text with links.
When a media file is inside the post, I want to ignore this.
I don't want a big preview for my posts - only a simple one.
My output code:
$html .= '<p>'.string_limit_words($numpost->post_content, 20).'</p>';

You need to pass $numpost->post_content through the the_content filter, like so:
apply_filters( 'the_content', $numpost->post_content )
Further reading: https://developer.wordpress.org/reference/hooks/the_content/

Related

Wordpress filter get_the_content()

I'm writing a plugin that requires adding to the content; adding a filter to the_content() is straightforward enough, but the theme I'm testing in uses get_the_content() to build the page.
There's an indication that it should be possible in the Wordpress codex but I don't understand the example:
If you use plugins that filter content (add_filter('the_content')), then this will not apply the filters, unless you call it this way (using apply_filters):
apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file ))
Can anyone help / explain?
Thanks,
David.
What they're saying is you'd have to add code to the place that get_the_content() is called. From your description, that's in the theme - you'd have to change the theme's get_the_content() call as described.
The reason for that is that there are no filters in the get_the_content() function that you can hook into with your plugin (have a look at the source - there are no calls to the apply_filters() function in get_the_content(), except one for the "more" link text).
Late to the party, but here it goes:
In your functions.php you can add something like this:
function do_filter_stuff($content) {
//Whatever you want to do with your content
}
add_filter( 'the_content', 'do_filter_stuff' );
Next, in your page.php (or whichever template you want to use it in) you can call the raw content with the same filter as following:
<?php echo do_filter_stuff(get_the_content()); ?>

Wordpress: Display the content custom page

I want to display the content of a specific page using the theme files.
I tried this :
$import_page = get_page_by_title(page-title-here);
echo "$import_page->post_content";
It works except that it doesnt display the formatting of the page. only text and the images, It avoids the line breaks, paragraph formatting ect.
Thank you for helping me to get this sorted.
You need to apply the_content filter to post_content before printing it.
Like so
echo apply_filters('the_content', $import_page->post_content);
You can find out more about the_content filter.

Paragraph tags and get_page_by_title()

I'm using the $page = get_page_by_title('$name'); to get the contest of a page to display. However, when I use echo $page->post_content; the paragraphs are not wrapped in p tags as is usually the case when you use the_content() inside the wordpress loop. Is there a way to fix this or a better method to get the content that will have the tags?
The value of post_content is the same as the unformatted input the user put in the post editor. In order to get the formatted output you have to apply the appropriate filter.
echo apply_filters('the_content', $page->post_content);
That should apply the appropriate output filter to the post_content field and give you the same value you would normally get from the_content().

Wordpress widget/plugin - content based on text of post(s)/page(s) visible?

I'm new to the Wordpress plugin and widget APIs, but I'm sure this is possible.
I'd like to make a plugin or widget that would create links to other posts or external sites based on certain keywords/tags in the content of the given page/post.
Is this possible?
For example, if a term is in all-caps, link to the Wiktionary definition; inside a <news>..</news> pair, go to Google's news search; etc.
This is definitely possible. Don't bother looking into the widget api for this. Look at the filter api. WordPress has an api that allows you to filter content before it's sent to the browser. In this case, you'd do something like this:
function my_super_awesome_content_filterer( $content ){
$content = preg_replace( '#([A-Z]+)#', '$1', $content );
}
add_filter( 'the_content', 'my_super_awesome_content_filterer' );
Read more about filters here:
http://codex.wordpress.org/Plugin_API#Filters

Possible to add a read more link in an RSS Feed?

This may be a weird or stupid question, but I have the following code (http://pastebin.com/PTFtqkvs) and I want to place a simple "read more" link after the description which links to the the article in the rSS feed - however whatever I do isn't working. Is it even possible to add this option and still conform to the rSS guidelines? This is built using a WP system to show Posts in a certain category.
Any help would be greatly appreciated.
You can hook onto feed specific hooks to add that to your feed content. Something like this in your theme's functions.php would work:
function my_super_awesome_feed_linker( $content ){
$extra = "<a href='" . get_permalink() . "'>Read More...</a>";
return $content . $extra;
}
add_filter( 'the_excerpt_rss', 'my_super_awesome_feed_linker' );
This will add a 'read more' link to all your feeds, though.
In order for this to work, you need to use a normal WordPress loop and the function the_excerpt_rss() instead of what you do in your code, echo $post->post_excerpt;. I've modified your pastebin here:
http://pastebin.com/6Y8pewhW
Also, just a word of advice, this won't really work as a template. WordPress has already sent headers by the time you've gotten to the page's template file. So you'll need to find a way to get those headers sent correctly, or to override them. The two easiest ways would be to filter the header content or to query the posts at 'wp_loaded' before headers are sent.

Resources