Auto enable embed youtube video with get_post() function - php

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.

Related

How to strip shortcode in Wordpress HTML

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

Overriding Excerpt in WordPress with Advanced Custom Field

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.

Replace Shortcodes with HTML in Wordpress

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

Preventing WordPress excerpts from hiding videos

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

Disable Wordpress short tag

I want to modify the layout for a Wordpress short tag. The tag I want to modify is the tag which will break a single post into multiple pages.
In my theme I need to disable that functionality and wrap each section in a div.
I know it's possible to add filters to modify short tags but clearly i'm doing something wrong. The function below doesn't seem to replace the short tag and i'm still getting a paginated list.
Can anyone suggest a solution to replace the short tags?
add_filter( 'the_content', 'reformat_lists' );
function reformat_lists($content){
$f = '<!--nextpage-->';
$r = '<div id="cmn-list">';
str_replace($f,$r,$content);
return $content;
}
It's likely that your post query is calling setup_postdata, so that <!--nextpage--> has already been replaced before you get the chance, so you might have to use another filter or figure out what Wordpress is inserting. You could get at the_content before setup_postdata if you used get_posts instead of WP_query. Ideally, you could find a filter on the_content that occurs before this, but not before DB write, but I can't seem to find anything to do job.
Its not beautiful, in that it is destructive (replaces the tag before saving to the database) rather than just prior to printing, but this might work for you:
function reformat_lists($content){
$f = '<!--nextpage-->';
$r = '<div id="cmn-list">';
$content = str_ireplace($f,$r,$content); //don't forget to pass your replaced string to $content
return $content;
}
add_filter( 'content_save_pre', 'reformat_lists');
EDIT: Even better, if you get the global $post, you CAN grab the unfiltered content. Try the below - I added a </div> onto the end of the content to close the one we're inserting so it wont break your layout. Grabbing the global $post might not work in all contexts, so I leave it up to test in your setup.
function reformat_lists($content){
global $post;
$content = $post->post_content;
$f = '<!--nextpage-->';
$r = '<div id="cmn-list">';
$content = str_ireplace($f,$r,$content) . '</div>';
return $content;
}
add_filter( 'the_content', 'reformat_lists', 1);

Categories