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
Related
I am using a simple shortcode to add line space or separator. I added this shortcode in my post meta field where we can add custom description but this shortcode is not extract on frontend
This is the text in my editor of that post meta
Have you been looking for a better, easier way to enjoy your favorite [linespace gap="30"] Introducing the beautifully simple, versatile and discreet don’t let its size fool you.
where [linespace gap="30"] is that shortcode
This is the output on frontend
Have you been looking for a better, easier way to enjoy your favorite [linespace gap="30"] Introducing the beautifully simple, versatile and discreet don’t let its size fool you.
this is my code for the shortcode function
function linespace_gap_shortcode($atts,$content = null){
$arg_s = shortcode_atts(
array(
'gap'=>'30',
), $atts, 'linespace' );
//getting the values from shortcode
$gap = $arg_s['gap'];
ob_start();
?>
<div class="clearfix separator-<?php echo $gap; ?>"></div>
<?php
return ob_get_clean();
}
add_shortcode('linespace','linespace_gap_shortcode');
In my mind you need to add a do_shortcode() inside your template around your meta value.
Example :
$my_meta = do_shortcode(get_post_meta( 0, 'meta_key', true ));
NB : think about esc_html() inside your shortcode function or force $gap to be an integer for better security .
I'm trying to run a do_shortcode in functions.php with no luck
I'm using Types plugin http://wp-types.com/ for creating custom post types and custom fields.
What I'm trying do is adding a custom column in admin for view all custom posts that displays a thumbnail from a custom field.
This is what I got so far, but it seems that the shortcode doesn't work inside functions.php
// add a column for custom post type (products)
add_filter('manage_product_posts_columns', 'add_thumbnail_column');
add_action('manage_product_posts_custom_column', 'add_thumbnail_content', 10, 2);
function add_thumbnail_column($defaults)
{
$newSlice = array('thumbnail' => 'Image preview');
$counter = 2;
$array_head = array_slice($defaults,0,$counter);
$array_tail = array_slice($defaults,$counter);
$defaults = array_merge($array_head, $newSlice);
$defaults = array_merge($defaults, $array_tail);
return $defaults;
}
function add_thumbnail_content($column_name, $post_ID)
{
// this one works when putting into post content
echo do_shortcode('[types field="square-picture" id="' . $post_ID . '" size="thumbnail"]' . '[/types]');
}
Can anyone help please?
In the Wordpress notes for the function it says
"If there are no shortcode tags defined, then the content will be
returned without any filtering. This might cause issues if a plugin is
disabled as its shortcode will still show up in the post or content."
Types may be conditionally declaring their short code only when you are on the frontend. What may be happening is that, in the admin, the short code is not defined and you are simply getting a false return. While on the frontend, the shortcode is defined and you get the results you intended.
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) );
}?>
I am looking for a wordpress plugin that will allow me to add a paragraph to the sidebar that is specific to the blog post. I would need to be able to add that text when creating the post. Is there something out there like that? I have been unsuccessful in searches.
Thanks
This can be easily solved using Custom Fields, the Text Widget and a Shortcode.
This bit of code goes in the theme's functions.php or, preferable, within a custom plugin.
1) Enable shortcodes for the Text Widget
add_filter( 'widget_text', 'do_shortcode' );
2) Define the shortcode, read comments for details
add_shortcode( 'mytext', 'so_13735174_custom_text_widget' );
function so_13735174_custom_text_widget( $atts, $content = null )
{
global $post;
// $post contains lots of info
// Using $post->ID many more can be retrieved
// Here, we are getting the Custom Field named "text"
$html = get_post_meta( $post->ID, 'text', true );
// Custom Field not set or empty, print nothing
if( !isset( $html ) || '' == $html )
return '';
// Print Custom Field
return $html;
}
3) Add a Text Widget in the desired sidebar.
Leave the title empty and put the Shortcode in the content: [mytext].
4) Now each page or post with a Custom Field named text will have its value printed in the Widget.
5) The $html can get fancy and multiple Custom Fields can be used.
This isn't something that I've ever personally done, but try this.
Summary: You will add the a paragraph using a custom field, then display it in a widget.
Details:
First, make sure custom fields are enabled. Edit a post, then click
the "screen options" at the top right of the page. If "Custom
Fields" isn't checked, check it. You should now see a custom field
area below the post editor.
Come up with a name for your custom field. Perhaps
"extra_paragraph". Now put that in the "name" field in the custom
field area.
Write your paragraph in the "value" field the custom field area.
Install the Custom Field Widget plugin, set it to display this
new "extra_paragraph" field. (widget appears to be untested with newer versions of Wordpress so cross your fingers!)
Now when you write or edit posts you should see this "extra_paragraph" field as an option in the "name" dropdown.
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.