Wordpress (Pods) oEmbed Items - wordpress

I'm using the fantastic Pods plugin to extend Wordpress's basic content types with a few custom ones. I've build an advanced custom type which means I don't get the automatic oEmbed support built into the native page/post types. I've structured it so my custom content type has a pod page using a PHP page template and I have the oEmbed option enabled for my WYISWYG fields that can embed videos.
I found this post which seems to indicate that a basic apply_filter function should automatically handle any embeds but I can't seem to get it to work. I'm a bit new to filters. The code I tried is below:
<?php
// Fetch body field content from $pods object
$mycontent = $pods->field('field_body');
$output = apply_filters('oembed_dataparse', $mycontent);
echo $output;
?>
I tried a variety of different filters such as the_content and others but none seemed to work. I believe it may be a scoping/conflict issue with Pod pages since even writing out entire iFrame embed code into the template won't work but only displays an empty iFrame. The global oembed function does the same, i.e.
$videourl = 'http://www.youtube.com/watch?v=dQw4w9WgXcQ';
$htmlcode = wp_oembed_get($videourl);
echo $htmlcode;
In the context of the page template will output:
<iframe width="500" height="375" frameborder="0" allowfullscreen="" src="http://www.youtube.com/embed/dQw4w9WgXcQ?feature=oembed">
<html>
<head>
</head>
<body>
</body>
</html>
</iframe>

field() gets the value of the field, display() gets the output of the field (ran through any related filters / functions the field is configured to run through).
$mycontent = $pods->field('field_body');
should be
$mycontent = $pods->display('field_body');
For more information, see http://pods.io/docs/field/ vs http://pods.io/docs/display/

Calling apply_filters('oembed_dataparse', $mycontent) is incorrect since this meant to add functionality for processing other data types (photo, video etc) not catered for by default. What you want to do is mimic how WordPress does the embedding. I haven't tested the code below, but it seems to me the way to go about triggering the embed functionality:
global $wp_embed;
$mycontent = $pods->field('field_body');
$output = $wp_embed->autoembed($mycontent);
echo $output;

Related

Get ACF OEmbed URL without returning iFrame using Timber

I have a semi complicated flexible content component that accesses a postObject which includes an oembed ACF field.
In other words Flexible Content -> PostObject -> OEmbed
But I'd like to extract the raw OEmbed link to put into its own "A tag" that will send people to the video source.
I've looked at these issues:
https://github.com/timber/timber/issues/1211
https://github.com/timber/timber/issues/1423
which are similar, but don't seem to be working in my case.
Is there a way to
A) Pull out the link in the Twig template or
B) Extend Timber to return just the link somewhere in the PostObject?
I would suggest extracting the link in you php and then adding it to context:
// Load value.
$iframe = get_field('oembed');
// Use preg_match to find iframe src.
preg_match('/src="(.+?)"/', $iframe, $matches);
$src = $matches[1];
// Add to timber/twig context
$context['iframe_src'] = $src

Wordpress output using custom url

I've been using WP_Route to create some custom urls as:
domain.com/players/playerID
If I use wp_send_json with a query result it works fine, but I would like to print that result.
If I do an :
get_header();
echo "<h1>".$player->name."</h1>";
get_footer();
I get:
NAME
OOPS! THAT PAGE CAN’T BE FOUND
any clues?
How could I force WP to don't run content hooks/whatever ?
PS: Wp_die() adds another header and body tag after the "good content".
<body id="error-page">
<p></p>
</body>
If you are using Anthony Budd's WP_Route, then don't. It's not a finished product (it doesn't work). It's surprising that the author has pushed this class without even testing it. Other users have made pull requests, presumably fixing some of the bugs and glaring omissions, but it seems that the author isn't responding so consider the project dead. There are forks, however, that might be in working condition.
You should probably just exit; in your handler.
When you call the wp_die() function it typically outputs a full HTML document, including the <html>, <head> and <body> tags as well as some CSS and an error message, and this is probably not what you want in your handler.
As the WP_Route class is written, your handler is executed on the WordPress init hook.

Embed shortcodes and oEmbed not working on Wordpress

I am having a weird issue.
Normally when you paste a youtube video the video will embed properly automatically. However when I paste a link into the visual view I can play and see the embedded video in the EDIT view, but on preview or publish it echoes out the link without embedding it as plain text.
So far I have tried disabling all plugins (except Advanced Custom fields)
Using the [embed] shortcode
Changing the youtube links from http to https
What does work is pasting the embed code from Youtube, or writing <iframe> in the text view.
It also works fine to paste and embed when I change theme
For me the issue was I was using
<?php echo get_the_content(); ?>
Instead of
<?php the_content(); ?>
As per the Wordpress Codex:
An important difference from the_content() is that get_the_content()
does not pass the content through the the_content filter. This means
that get_the_content() will not auto-embed videos or expand
shortcodes, among other things.
https://developer.wordpress.org/reference/functions/get_the_content/#:~:text=An%20important%20difference%20from%20the_content()%20is%20that%20get_the_content()%20does%20not%20pass%20the%20content%20through%20the%20the_content%20filter.%20This%20means%20that%20get_the_content()%20will%20not%20auto%2Dembed%20videos%20or%20expand%20shortcodes%2C%20among%20other%20things.
Use the [video] short code instead. It is incorporated in the Wordpress core nowadays. Go to this link for more info: https://codex.wordpress.org/Video_Shortcode
For example:
[video width="640" height="360" src="/wp-content/uploads/files/movie.mp4"]
May this not work in a custom theme, then you want to look at how the content of the post is being processes. You may need to apply the the_content filter to the post content variable like this:
apply_filters('the_content', $post->post_content);

Wordpress: How to pass additional Content to the blog-preview page?

For each blog-post on my wordpress-blog I'd like to have Teaxtarea where i can pass additional content for that post.
In my case that would be an unordered list which contains a quick overview of the content.
That additional content should be displayed in the preview of the post on the blog-preview-page.
My problem:
I am actually not sure on how to best add this additional content and then pass it to the preview.
Do I use wordpress' custom fields for something like this?
I'm gratefull for a push in the right direction.
Thank you,
Nils
If I understand you right, I'd take a look at "custom meta boxes" functionality - it allows you to add any type of additional input into your blog post admin area, and than display its content on front-end however you like.
There's a nice tutorial series on that topic, with example code snippets:
http://wp.tutsplus.com/series/reusable-custom-meta-boxes/
And if you'd like to display the textarea content only in preview mode, you can use appropriate conditional tag in you template file:
http://codex.wordpress.org/Conditional_Tags#A_Preview
The conditional tag is_preview returns true when a single post is viewed in Draft mode. The following will append post meta to the content when a post is being previewed:
function so16799607_preview( $content )
{
if ( ! is_preview() )
return $content;
return $content . get_post_meta( get_the_ID(), 'my_post_meta', true );
}
add_filter( 'the_content', 'so16799607_preview', 10, 1 );
You should check out Advanced Custom Fields. That's a really stable plugin that lets you create custom meta boxes in posts, pages and custom post types. That plugin does exactly what your question states. Need al little PHP to get stuff from your database, but that is as easy as:
<?php the_field(field_name);?>
And the documentation is pretty good. And if you don't like a plugin, it exports the PHP as well.
Anther tool that does the same is Pods Framework. Both powerfull extensions to any WP install in my opinion.
Hope this helps.

Load a page through ajax with Wordpress using different template

After the user clicks a link, I want to load the corresponding page within the currentpage. This ajaxpage should be themed differently than it's non-ajax page.
The pages would be added by the client in different languages. So there is no fixed id's and the client shouldnt add the same article twice (1 for normal and 1 for ajax). This rules manually overwriting templates out.
The first part, ajax loading of content is easy to do, but I don't know how to display content in different ways.
So
domain.com/product/car direct visit shows a product (car) using single-product.php as template which includes a header and footer.
ajax loading domain.com/product/car would show a product (car) but without a header and footer and perhaps some small layout changes.
My initial thought would be setting some template dynamicly (template-product-ajax.php) but Im not sure if this is possible or how.
In short, how can I create a different template/layout/view when loading a page with ajax in wordpress
You would have to pass in a series of variables, including the page/post ID you want to load, what kind of object you're trying to load (either 'page' or 'post') and a boolean to ensure that the standard get_header() function isn't loaded (as you only want the content of the page, not the headers).
Somewhere within the template, you can use that boolean to determine the template's behavior. For example:
<?php
/*Template Name: AJAX Content*/
if(!isset($_POST['bool']))
get_header();
?>
<div id="body">
<div class="page_content">
<?php
if(isset($_POST['bool']))
{
$object = $_POST['ob_type'] == 'page' ? get_page($_POST['PID']) : get_post($_POST['PID']);
echo $object->post_content;
/*echo '<pre>';var_dump($object);echo '</pre>';//Uncomment this section if you want to know what you can access with variable $object*/
{
else
{
//STANDARD LOOP
}
?>
</div>
</div>
This is untested, but it should help get you started if you're already familiar with AJAX calls and sending POST data to a separate page.

Resources