Get youtube thumbnail simplepie - youtube

So I'm trying to get a thumbnail of a youtube video in simple pie, my problem is that the get_thumbnail() function doesn't seem to be pulling it because the get_enclosure function seems to be returning no values.
Is there something that must be done to initialize the simplepie object to get the enclosure properly?

Not all feeds support/use RSS enclosures it isn't part of the RSS standard at least not the original RSS standard. It is part of something called MediaRSS. None the less this can be done. Another problem is Google has been changing GData API which is what actually makes the RSS feeds for YouTube or it did, you might want to use this API instead which produces Atom feeds. You probably want to look at some documentation.
You have to use additional code beyond SimplePie to create a thumbnail for some feeds, I used something called simple_html_dom and another script called thumbnail.php to make thumbnails as necessary. Your life is better if your have a feed like Flickr which supports MediaRSS but if you gotta force the creation of a thumbnail, I used this code:
if ($enclosure = $item->get_enclosure())
{
// Check to see if we have a thumbnail. We need it because this is going to display an image.
if ($thumb = $enclosure->get_thumbnail())
{
// Add each item: item title, linked back to the original posting, with a tooltip containing the description.
$html .= '<li class="' . $item_classname . '">';
$html .= '<a href="' . $item->get_permalink() . '" title="' . $title_attr . '">';
$html .= '<img src="' . $thumb . '" alt="' . $item->get_title() . '" border="0" />';
$html .= '</a>';
$html .= '</li>' . "\n";
}
}
else
{
// There are feeds that don't use enclosures that none the less are desireable to dsipaly wide as they contain primarily images
// Dakka Dakka and some YouTube feeds fall into this category, not sure what is up with Chest of Colors...
$htmlDOM = new simple_html_dom();
$htmlDOM->load($item->get_content());
$image = $htmlDOM->find('img', 0);
$link = $htmlDOM->find('a', 0);
// Add each item: item title, linked back to the original posting, with a tooltip containing the description.
$html .= '<li class="' . $item_classname . '">';
$html .= '<a href="' . $link->href . '" title="' . $title_attr . '">';
// Sometimes I'm not getting thumbnails, so I'm going to try to make them on the fly using this tutorial:
// http://www.webgeekly.com/tutorials/php/how-to-create-an-image-thumbnail-on-the-fly-using-php/
$html .= '<img src="thumbnail.php?file=' . $image->src . '&maxw=100&maxh=150" alt="' . $item->get_title() . '" border="0" />';
$html .= '</a>';
$html .= '</li>' . "\n";
}
The formatting seems a bit odd, but that is ripped right from my code which is running here. You're still best off not making a lot of thumbnails from a feed that doesn't support them.

Related

Plotly - prevent <a> Tag from open new tab

The x label of my plotly diagram contains a link to the selected data point:
let str = '<a href="' + url + '"target=' + '_self' + '>' + cycleStart[1] + '</a>';
But the link is always opened in a new tab. I know that the target attribute causes this problem. But as you can see my simple solution is not working. I already searched
target="_blank"
in the plotly.min js file and changed it into
target="_self"
Also this does not work for me.
So how can I prevent this Tag from opening a new tab? I want the url to open in the same window.

How to print get method in javascript

I want to print mod value in javascript document.write
for example my url is http://sitename.com/test.js?mod=57
now I want print 57 in document.write
document.write('<img src="http://utools.com/tools/mousse/mousse'+mod+'.png" />');

Pull in previous and next post featured images into single.php

My previous and next posts are set on an infinite loop so there is always a previous and next post. I.E. for the newest article the "next" post would be go to the oldest post. And if we're viewing the oldest post, the "previous" post would go to the newest post.
I need to pull in the featured images of the next and previous posts to be the background for the pagination navigation.
I've got the images pulling in when there truly is a next or previous post. However, when the infinite loop is necessary, i.e. next being the oldest post, or previous being the newest post, my code breaks down.
My code is below. I believe the issue is that I'm not actually capturing the ID of what I need in $first_id and $last_id. I've tried using get_queried_object_id() $first->post->ID and various combinations to get the post ID of what is being pulled in since .previous-title is working correctly.
<div class="post_nav">
<?php if( get_adjacent_post(false, '', true) ) :
$previous_post = get_previous_post(); ?>
<div class="previous-img"><?php echo get_the_post_thumbnail( $previous_post->ID );?></div>
<p class="previous-title"><?php previous_post_link('Previous Story<br> %link','%title');?</p>
<?php else:
$first = new WP_Query('posts_per_page=1&order=DESC'); $first->the_post();
$first_id = $first->ID; ?>
<div class="previous-img"><?php echo get_the_post_thumbnail( $first_id );?></div>
<?php echo '<p class="previous-title">Previous Story<br>' . the_title() . '</p>';
wp_reset_query(); ?>
<?php endif; ?>
<?php if( get_adjacent_post(false, '', false) ) :
$next_post = get_next_post(); ?>
<div class="next-img"><?php echo get_the_post_thumbnail( $next_post->ID ); ?></div>
<p class="next-title"><?php next_post_link('Next Story<br> %link','%title');?></p>
<?php else:
$last = new WP_Query('posts_per_page=1&order=ASC'); $last->the_post();
$last_id = $last->ID; ?>
<div class="next-img"><?php echo get_the_post_thumbnail( $last_id );?></div>
<?php echo '<p class="next-title">Next Story<br>' . the_title() . '</p>';
wp_reset_query(); ?>
<?php endif; ?>
You're right, $first and $last are not posts, but queries. You could either use get_the_ID() or grab the ID directly.
$last_id = get_the_ID();
( since you've made it the active loop )
or
$last_id = $last->posts[0]->ID;
which means you wouldn't need to set up the loop for $last, in which case you'd also need to get the permalink and title via functions taking the ID as a parameter. This is a minor optimization, if you get confused here go with the first option.
Also, your next-title and previous-title in the else paths seem to be switched with each other. Or perhaps the entire code blocks are.

Escape Html Text in ZF2

I have a text like this:
<p><strong>Lorem</strong> ipsur </p>
To see a text without html tags in Zend Framework 2 how can I do?
I tried to put:
<?php echo $this->escapehtml($array['text']); ?>
I read that for security is not good to do:
<?php echo $array['descrizione']; ?>
Im not sure if i understand the question, since i dont know the contents of your arrays.
basicaly if you want to scape the contents of a variable, let's say, $input, you have to call, as you mentioned
$this>escapeHtml($input)
Actually, the PhpRenderer includes a selection of helpers you can use for this purpose: EscapeHtml, EscapeHtmlAttr, EscapeJs, EscapeCss, and EscapeUrl.
You can read about this here
Also, if you want more control, you can use Zend\Escaper, that in 2 lines of code allow you to escape html, like this
$escaper = new Zend\Escaper\Escaper('utf-8');
$output = $escaper->escapeHtml($input);
or escape attributes, like this
$escaper = new Zend\Escaper\Escaper('utf-8');
$output = $escaper->escapeHtmlAttr($input);
I recommend you read the 3 links, they are very short and would give you a better undestanding of what you are doing.
At this moment there is no option in escapeHtml to allow certain tags. What you have to do in these specific cases is to modify the returned value:
<?php
$str = $this->escapehtml($array['text']); ?>
$str = preg_replace(
array('#href="(.*)"#', '#<(/?(?:pre|a|b|br|em|u|ul|li|ol|p|strong)(\shref=".*")?/?)>#' ),
array( 'href="\1"', '<\1>' ),
$str
);
echo $str;
You can add/remove tags from the part pre|a|b|br|em|u|ul|li|ol|p|strong to your needs. Also this code will allow anchor tags with href only.

How to add the language selector in the ckeditor tool

I've integrated the ckeditor gem for rails in the activeadmin gem. So far it is working good but I want to include the language selector which I've seen in the demo of the ckeditor. But I've not found any related article except this. The demo of that selector isthis. Thanks in advance.
Download CKEditor and check the following sample: samples\uilanguages.html
Basically, CKEditor demo just shows a custom select element, with the list of languages taken from samples/assets/uilanguages/languages.js
The most relevant lines from this sample are:
<script src="assets/uilanguages/languages.js"></script>
<script>
document.write( '<select disabled="disabled" id="languages" onchange="createEditor( this.value );">' );
// Get the language list from the _languages.js file.
for ( var i = 0 ; i < window.CKEDITOR_LANGS.length ; i++ ) {
document.write(
'<option value="' + window.CKEDITOR_LANGS[i].code + '">' +
window.CKEDITOR_LANGS[i].name +
'</option>' );
}
document.write( '</select>' );
</script>
Keep in mind that by default CKEditor auto detects the browser language and loads the correct translation. So, providing a select combo to a user may so that he could select the language may not have a lot of sense.

Resources