I have a custom theme where when I upload an image with caption, the caption shortcode is shown on the page: [caption id="attachment_109"].... Using either of WP's default themes, I see no issue. On inspection, WP uses the_content(), link, so I need to do some stripping. I get my posts with get_page_by_path():
<?php
$post = get_page_by_path( $current_page->post_name, OBJECT, 'service' );
// I assume this would work
$content = the_content($post->post_content);
//Blank page:
echo $content;
Echoing $post->post_content shows the caption shortcode as mentioned above. How to get rid of if it? BTW, I need the caption values.
You can get the post content like this
$post = get_post(123); //pass the id of the post
$content = $post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
or
$content=apply_filters('the_content', get_post_field('post_content', 123)); //123 is the post id
after that just strip the shortcode also you can check if the post is having the shortcode in it or not by has_shortcode
Related
I'm having trouble changing the posts display in my WordPress website. So far, the posts can display a title and text content, and I would like to display tags, categories and an image. I added the following code in functions.php and it actually displays the p tags, but around the content, and not in it. Also, it is displayed on all pages of my website, while I just want to add these HTML tags inside the posts.
So,
How can I put the tags inside the_content() and only display the custom HTML in posts?
I hope my question is clear, I starting learning PHP a few days ago, sorry!
Thank you so much in advance for you help!
The code :
// Creating a custom function that appends HTML to the content
function bts_add_html_to_content( $content ) {
$html_segment = '<p>Text to be added.</p>';
$content = $html_segment . $content . $html_segment;
return $content;
}
add_filter( 'the_content', 'bts_add_html_to_content', 99);
Not totally sure what you want for the "in it" part but I can help with conditionally applying your code to posts only... See below:
// Creating a custom function that appends HTML to the content
function bts_add_html_to_content( $content ) {
// if not a post then return the $content as is
if ('post' !== get_post_type()) {
return $content;
}
// must be a post if we get here so lets do something with it
$html_segment = '<p>Text to be added.</p>';
$content = $html_segment . $content . $html_segment;
return $content;
}
add_filter( 'the_content', 'bts_add_html_to_content', 99);
I'm new to WordPress and we have a form that people can fill out to submit posts onto our site.
We used Advanced Custom Fields plugin to fill out all the necessary information we needed. We have on field that is story_description.
I would like to make that story_description to use all the excerpt filters (length, etc) and save it under the post_excerpt in the wp_posts table.
How would I go about having that save in that table with the custom length and the other specific in the excerpt that are defined? I'm new to all the filters and actions.
Is there a way to just overwrite is and treat is like a normal excerpt when the post is saved?
I appreciate any responses.
Your first need to add the following to allow excerpt generation outside of the loop (this go in your theme functions.php):
function rw_trim_excerpt( $text='' )
{
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
return wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
add_filter('wp_trim_excerpt', 'rw_trim_excerpt');
This function comes from jlengstorf answer.
Then when you insert your post do the following:
wp_insert_post(array(
// ... other stuffs ...
post_excerpt => apply_filters('get_the_excerpt', $story_description)
));
This will filter your $story_description string with all the excerpt WP functions.
I'm working on a WordPress site that has been using a plugin to grab amazon product images using a shortcode.
You simply insert the asin of a book into a shortcode, like this:
[amazon template=image&asin=B00FYY53B8]
When the post loads, the shortcode is converted into the actual image HTML using the URL from amazon.... so the example above would return
http://ecx.images-amazon.com/images/I/71kIifYTccL._SL1500_.jpg
I'm using another custom plugin to build out the content for posts, and so far am just using this shortcode as part of the post content. I would like to do some other things, such as set the featured image, etc. and need that URL.
I've tried
echo do_shortcode( '[amazon template=image&asin=B00FYY53B8]' );
But it doesn't return anything. Is there a way to "execute" shortcodes in a plugin and save the output?
Ultimately, I would also like to scale this functionality so I can replace other/old shortcodes with their HTML output, and save it back to the post, essentially eliminating the use of some shortcodes in posts.
I have a demo for add a short code in wordpress. I hope it help.
add_action('plugins_loaded','MOD_load');
function MOD_load() {
short_enable_form();
}
function short_enable_form(){
if(!function_exists('add_shortcode')) {
return;
}
add_shortcode('form_mod' , array(&$this, 'out_put_form'));
}
public function out_put_form(){
return 'HTML TEXT';
}
You can include this code in functions.php. And call this function in content-singular.php or the similar file in your theme.
function updatesc_database( $post ) {
if ( empty($post) ) global $post;
if ( empty($post) || ! isset($post->post_content) ) return false;
$content = $post->post_content;
$shortc1="amazon template=image&asin";
$shortc2="B00FYY53B8]"; //insert the number variable here
$shortwh=$shortc1.$shortc2;
$replace = do_shortcode($shortwh);
$content = str_replace( $shortwh, $replace, $content );
return $content;
}
I'm using WordPress on my website and I want to limit the posts length on the front page with the_excerpt(); to about 100 words. However if I do another post that has a video will hide the video and I don't want that.
However if I use the_content(); and the <!--more--> tag in the post the Read More link has #more-## after it and causes it to be almost at the bottom of the page.
You can create your own function instead of excerpt
function get_new_excerpt(){
$excerpt = get_the_content();
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$new_excerpt = substr($excerpt, 0, 20);
return $new_excerpt;
}
Place this in functions.php
Now instead of the_excerpt, use
echo get_new_excerpt();
I use get_post() function to get a specific post content. However, I cannot make the auto embed video runs.
Here is the code
<?php
$post_id = 110;
$queried_post = get_post($post_id);
$content = $queried_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
I want it auto detect the youtube link and enable the embed video.
The sample content is
Check out this cool video:
http://www.youtube.com/watch?v=nTDNLUzjkpg
That was a cool video.
http://codex.wordpress.org/Function_Reference/get_post
http://codex.wordpress.org/Embeds
I haven't tried this myself, but this is what I can tell you:
The oEmbed functionality is applied in wordpress by a filter.
The get_posts() function suppresses filters by default and I guess get_post() does the same, although I was not able to verify this in the documentation.
Perhaps you can use query_posts() (which doesn't suppress filters) or get_posts() with suppress_filters=>false to test this.