How to use WP oembed script outside the_content - wordpress

I've a custom post type "video" and wanted to show videos like youtube, dailymotion in a specified area using WP default oembed script.
So i'm using a custom field"video url", but the problem is that oembed work in the_content not with custom field. so how can i do this. or any other solution

If the custom field only contains the video URL like http://www.youtube.com/watch?v=dQw4w9WgXcQ, then you can get the oEmbed HTML code with wp_oembed_get:
$videourl = 'http://www.youtube.com/watch?v=dQw4w9WgXcQ';
$htmlcode = wp_oembed_get($videourl);
echo $htmlcode;
If your custom field contains more than just the URL, you could use the the_content filter to do the same thing the the_content-function does:
$content = "<h2>this video is great</h2>\n<p>check it out</p>\n"
. "[embed]http://www.youtube.com/watch?v=dQw4w9WgXcQ[/embed]";
$htmlcode = apply_filters('the_content', $content);
echo $htmlcode;

Here is a complete answer to your question. It is also a cleaner and quicker method, using wp_oembed_get, rather than shortcode. Of course, change video_url to the name of your custom field.
This code checks that the video_url field is not empty, then oEmbeds the video.
<?php if (!((get('video_url', TRUE))=='')) {
echo wp_oembed_get( get('video_url', true) );
}?>

Related

Add WordPress filter to ACF WYSIWYG field

I am currently using the following filter to replace any instance of the string 'magic_click_link' within WordPress' the_content.
function click_link ($b) {
global $post;
$this_post_id = $post->ID;
$op_name = get_field('operator_name');
$namenospace = make_no_space("$op_name");
$tracking_link = '/go/'.$this_post_id.'/';
$click_link = '<a class="claimCTA" id="operator-step1-'.$namenospace.'" href="'.$tracking_link.'" target="_blank" rel="nofollow">Click here to go to '.$op_name.'!</a>';
$b = str_ireplace('magic_click_link',$click_link,$b);
return $b;
}
add_filter( 'the_content', 'click_link');
I have just updated this field to use an ACF WYSIWYG field, but this has caused the filter to stop working.
I believe I need to target something other than the_content inside the filter but I'm not sure what is needed...
The filter the_content automatically runs on any content output using the function the_content(), so by default, this means it only applies to standard WordPress post and page content.
To apply these filters to your custom field content as well, you need to manually call apply_filters() when you output the field's content.
For example, say your custom field is called my_extra_content. With Advanced Custom Fields, you can call:
echo apply_filters("the_content", get_field("my_extra_content"));
This will output the content of your field, while applying all the usual filters against it. Because your click_link() function is hooked to the_content already, it will therefore process your custom field content as you are desiring.

How to output cmb2 wysiwyg by using timber

I am using cmb2 to create the custom post type, and I have a custom post type called legacy_cycle, which contains several wysiwyg fileds.
Than I am using Timber as theme tool to display the input. However, the wysiwyg field could not work properly. It is fine if it only contains text or image, but it only output the following information when I insert a youtube video in the wysiwyg editor under the custom post type, but the output from the native wordpress editor is fine.
" [embed]https://www.youtube.com/watch?v=MS91knuzoOA[/embed]"
I tried to use post.get_filed('my_wysiwig'), but it did not work.
I am wondering how I could output the field correctly? much appreciated!
So, I figured it out through this post..Applying oembed filters to WYSIWYG field
My solution by using timber/twig is to get the data in the single.php, and here is the code:
$post_meta = get_post_meta(get_the_ID(),'my_wysiswg', true);
$post_meta = $wp_embed->autoembed( $post_meta );
$post_meta = $wp_embed->run_shortcode( $post_meta );
$post_meta = do_shortcode( $post_meta );
$post_meta = wpautop( $post_meta );
$post->my_wysiswyg = $post_meta;
Then I can print the video in the single-custom-post-type.twig by using {{post.my_wysiswyg}}
It looks like you just need to process shortcodes in that field. This should convert those into actual YouTube videos:
{{ post.get_field('my_wysiswyg') | shortcodes }}
I use CMB2 extensively with Timber and my usual approach is to extend TimberPost and add in methods for getting meta data. For a wysiwyg field, something like this:
class CustomPost extends TimberPost {
public function my_wysiswg(){
$metadata = get_post_meta($this->ID, 'my_wysiswg', true);
if ($metadata){
return apply_filters('the_content', $metadata);
}
}
}
You can specify which class Timber will use with the second parameter of Timber::get_posts

Custom URL %link next_post_link and previous_post_link Wordpress

I would like to customize the URL link generated by next_post_link() and previous_post_link() (Wordpress post Navigation Functions)
Now returns a URL link like this:
http://wwww.misite.com/notice1 (for example)
I would like returns a custom URL link like this:
http://www.misite.com/shop/notice1
My code is :
$content .= next_post_link('<div class="nav-prev">« %link</div>');
$content .= previous_post_link( '<div class="nav-next">%link »</div>');
You try to retrieve the result of function that don't return anything. See the WordPress Codex for more info about these functions.
If you want to store the link, you can use the get_next_posts_link() function instead (see here). You can also find get_previous_posts_link().

Wordpress: add custom page attribute

I have the NextGEN Gallery plugin on my wordpress site. Normally I would add this short code in my page content to display gallery items:
[ nggallery id=5 template=custom ]
Now I'd like to replace this by adding custom fields in the Page Attributes setting when you are adding/editing a page. The custom fields would be "Gallery ID" and "Template name".
I'm of course using a custom page template. How can I retrieve the page attributes into this page template?
Thanks in advance!
Yes, what Stratboy said. Documentation here. This is the setup that should work for you:
<?php
$gallery_ID = get_post_meta($post->ID, 'Gallery ID', true);
$template_name = get_post_meta($post->ID, 'Template Name', true);
if ($gallery_ID && $template_name){
//echo '$gallery_ID: '.$gallery_ID.'; $template_name: '.$template_name.';';
echo do_shortcode('[nggallery id="'.$gallery_ID.'" template="'.$template_name.'"]');
}
?>
So, first:
have you already implemented the custom page template?
have you already implemented the custom fields?
Anyway, generally, in a template you get your custom fields values using the get_post_meta function in the Loop, like this:
//the last param tell if you want the value returned as a string (true) or an array (false)
get_post_meta($post->ID,'field name',true);
You can echo the returned value or use it for other tasks.
Let me know.

Inserting Wordpress Plugin content to posts

I am trying to learn more about Wordpress and creating plugins. I have seen an existing plugin use a technique where you can add a 'reference' to it within your posts and WP will parse it and replace it with the plugins own content. The example i am referring to is the NextGen gallery which uses the following code
[nextgen id=9]
I have tried searching for how this technique works but trying to find something that you don't know the name of is pretty difficult!
Can anyone point me towards some resources about how to use this feature of WP?
The technique is called shortcodes.
add_shortcode('my-content','my_plugin_shortcode');
function my_plugin_shortcode($atts, $content = null) {
$atts = shortcode_atts($my_default_atts,$atts); // $atts is now an associate array
$my_content = 'This is some content.';
return $my_content;
}
Then, if you have a post with the following content:
Hey, here is some content.
[my-content]
You will get the following output when the post is displayed:
Hey, here is some content. This is
some content.
If you passed a shortcode like [my-content id="9" test="test"], then the $atts variable in the above function would be like the following array declaration
$atts = array('id'=>9, 'test'=>'test');
The $content variable only has content when you use matching shortcodes around some text:
[my-content]This is some test
content.[my-content]

Resources