Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Regex/function not working in pagetemplate? WordPress

  • SOLVED

Ok, this is really strange. I have put in this function in functions.php:

function parse_yturl($url) {
$pattern = '#^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:.+)?$#x';
preg_match($pattern, $url, $matches);
return (isset($matches[1])) ? $matches[1] : false;
}


But when I try using it in a pagetemplate, *nothing* happens. I mean nothing. It just turns out blank. I have tried <?php echo parse_yturl($videocode); ?> - and if I use it in a seperate file, it works fine. If I use it in my template, nothing. The theme is a heavily modified twenty ten.

<?php query_posts( array(
'post_type' => 'page',
'meta_key' => 'ecpt_forsidevideo',
'meta_value' => '',
'meta_compare' => '!=' ) );

if ( have_posts() ) while ( have_posts() ) : the_post();
$videocode = get_post_meta($post->ID, 'ecpt_videocode', true);
$videoid = parse_yturl($videocode);
?>
<div class="forsidevideo">
<div class="videothumbnail"><?php echo parse_yturl($videocode); //this is only here to see right away if it works or not ?>
<a href="http://www.youtube.com/watch?v=<?php echo $videoid;?>" rel="prettyPhoto" title="<?php the_title(); ?>"><img src="http://img.youtube.com/vi/<?php echo $videoid;?>/0.jpg" alt="YouTube" width="200" height="150" /></a>
<a class="playbutton" href="http://www.youtube.com/watch?v=<?php echo $videoid;?>" rel="prettyPhoto" title="<?php the_title(); ?>"><img src="<?php bloginfo('stylesheet_directory');?>/images/playbutton.png" alt="Vis video" width="40" height="40" /></a>
</div>
<a href="<?php the_permalink();?>"><h3 class="entry-title"><?php the_title(); ?></h3></a>

<?php the_excerpt(); ?>
</div>
<?php endwhile; // end of the loop. ?>


Anyone got any ideas why its not working?

Answers (3)

2012-12-01

Dbranes answers:

Hi, this code

function parse_yturl($url) {
$pattern = '#^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:.+)?$#x';
preg_match($pattern, $url, $matches);
return (isset($matches[1])) ? $matches[1] : false;
}

//$url="http://www.youtube.com/watch?v=abcde123456";
$url="https://www.youtube.com/watch?v=7N5OhNplEd4&feature=g-all";

echo parse_yturl($url);


gives 7N5OhNplEd4 so the function seems to work alright on youtube urls.

You can try this code instead (it has debug values and will show you the number of posts found and the videocode/videoid values)

<?php

// debug:
$bDebug=true; // true or false

function parse_yturl($url) {
$pattern = '#^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:.+)?$#x';
preg_match($pattern, $url, $matches);
return (isset($matches[1])) ? $matches[1] : false;
}
$args = array(
'post_type' => 'page',
'meta_key' => 'ecpt_forsidevideo',
'meta_value' => '',
'meta_compare' => '!='
);
$myquery = new WP_Query( $args );

if($bDebug){
echo "<pre>";
echo "found_posts: ". $myquery->found_posts;
echo "</pre>";
}

while ($myquery->have_posts()) : $myquery->the_post();
$videocode = get_post_meta(get_the_ID(), 'ecpt_videocode', true);
$videoid = parse_yturl($videocode);

if($bDebug){
echo "<pre>";
echo "videocode: " . $videocode;
echo "videoid: " . $videoid;
echo "</pre>";
}
?>
<div class="forsidevideo">
<div class="videothumbnail"><?php echo parse_yturl($videocode); //this is only here to see right away if it works or not ?>
<a href="http://www.youtube.com/watch?v=<?php echo $videoid;?>" rel="prettyPhoto" title="<?php the_title(); ?>"><img src="http://img.youtube.com/vi/<?php echo $videoid;?>/0.jpg" alt="YouTube" width="200" height="150" /></a>
<a class="playbutton" href="http://www.youtube.com/watch?v=<?php echo $videoid;?>" rel="prettyPhoto" title="<?php the_title(); ?>"><img src="<?php bloginfo('stylesheet_directory');?>/images/playbutton.png" alt="Vis video" width="40" height="40" /></a>
</div>
<a href="<?php the_permalink();?>"><h3 class="entry-title"><?php the_title(); ?></h3></a>
<?php the_excerpt(); ?>
</div>
<?php endwhile; // end of the loop. ?>


Update: I replaced

$videocode = get_post_meta($post->ID, 'ecpt_videocode', true);


with

$videocode = get_post_meta(get_the_ID(), 'ecpt_videocode', true);


in the above code.


Torstein Opperud comments:

Hi Dbranes,
yes, as I said, I have tried the function in a seperate file, and then it works fine.

Echoing $videocode works fine, and the query works fine - all pages show up just like expected, no problems there. The only thing that does *not* work is using

parse_yturl($videocode);


in the pagetemplate.


Dbranes comments:

try to replace

$videocode = get_post_meta(get_the_ID(), 'ecpt_videocode',true);


with

$customs = get_post_custom(get_the_ID());
$videocode = (isset($customs[ecpt_forsidevideo][0]))?$customs[ecpt_forsidevideo][0]:"";


(this works for me in a TwentyTen theme in a page template)


Torstein Opperud comments:

You mixed up a couple of my meta fields there Dbranes, I use "ecpt_forsidevideo" to choose if the video should be shown or not, while "ecpt_videocode" is the field with the code.

Anyways - this code:

<?php // debug:

$bDebug=true; // true or false



function parse_yturl($url) {

$pattern = '#^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:.+)?$#x';

preg_match($pattern, $url, $matches);

return (isset($matches[1])) ? $matches[1] : false;

}

$args = array(

'post_type' => 'page',

'meta_key' => 'ecpt_forsidevideo',

'meta_value' => '',

'meta_compare' => '!='

);

$myquery = new WP_Query( $args );



if($bDebug){

echo "<pre>";

echo "found_posts: ". $myquery->found_posts;

echo "</pre>";

}



while ($myquery->have_posts()) : $myquery->the_post();

$customs = get_post_custom(get_the_ID());

$videocode = (isset($customs[ecpt_videocode][0]))?$customs[ecpt_videocode][0]:"";



$videoid = parse_yturl($videocode);



if($bDebug){

echo "<pre>";

echo "videocode: " . $videocode. "<br>";

echo "videoid: " . $videoid;

echo "</pre>";

}

?>

<div class="forsidevideo">

<div class="videothumbnail"><?php echo parse_yturl($videocode); //this is only here to see right away if it works or not ?>

<a href="http://www.youtube.com/watch?v=<?php echo $videoid;?>" rel="prettyPhoto" title="<?php the_title(); ?>"><img src="http://img.youtube.com/vi/<?php echo $videoid;?>/0.jpg" alt="YouTube" width="200" height="150" /></a>

<a class="playbutton" href="http://www.youtube.com/watch?v=<?php echo $videoid;?>" rel="prettyPhoto" title="<?php the_title(); ?>"><img src="<?php bloginfo('stylesheet_directory');?>/images/playbutton.png" alt="Vis video" width="40" height="40" /></a>

</div>

<a href="<?php the_permalink();?>"><h3 class="entry-title"><?php the_title(); ?></h3></a>

<?php the_excerpt(); ?>

</div>

<?php endwhile; // end of the loop. ?>


gives these results:

found_posts: 2

videocode: http://www.youtube.com/watch?feature=player_embedded&v=9sPpmsute8c
videoid:

videocode: https://www.youtube.com/watch?feature=player_detailpage&v=tkaLTx5qk14
videoid:


Dbranes comments:

ok sorry I mixed up the fields value when I was copying your code ;-)

I'm wondering if you got extra space in your 'ecpt_videocode' custom field value?

If I run this code in my page template I get this

found_posts: 1
videocode: http://www.youtube.com/watch?feature=player_embedded&v=9sPpmsute8c
videoid: 9sPpmsute8c
id: 220


but if I add an extra space in front of the ecpt_videocode custom field value, I get this
videocode: http://www.youtube.com/watch?feature=player_embedded&v=9sPpmsute8c
videoid:
id: 220


So you could try to replace

$videoid = parse_yturl($videocode);


with

$videoid = parse_yturl(trim($videocode));



Torstein Opperud comments:

Argh, this is strange. Still no luck here (but the trim part was a good idea anyways) probably means theres something else in my theme that f**ks up this. Ah, well, I guess I'll just have to start testing to find out that part then :/ I think I'll just declare you the winner on this one, dbranes :)


Dbranes comments:

We must be able to find this bug ;-)

Here is a debug version of the parse_yturl() function

function parse_yturl($url) {

$pattern = '#^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:.+)?$#x';
preg_match($pattern, $url, $matches);

echo "<pre style='padding:5px;border:1px solid #333;background:#888;'>";
echo "<strong>debug info:</strong>";
echo "<br/>";
echo "url: ". $url;
echo "<br/>";
echo "matches: ";
var_dump($matches);
echo "</pre>";

return (isset($matches[1])) ? $matches[1] : false;
}


this gives me:

debug info:
url: http://www.youtube.com/watch?feature=player_embedded&v=9sPpmsute8c
matches: array(2) {
[0]=>
string(66) "http://www.youtube.com/watch?feature=player_embedded&v=9sPpmsute8c"
[1]=>
string(11) "9sPpmsute8c"
}


2012-12-01

Gabriel Reguly answers:

Hi Torstein,

I wonder what is the content of $videocode?

Regards,
Gabriel


Torstein Opperud comments:

$videocode would be some variant of a youtube url. I have tried just using the function (at the top of my post) in a seperate file, and then it works, giving the video id of ... well - all the different varieties of youtube urls I have thrown at it, although I havent tried all, of course.

Basicly - I use a custom meta field to enter a youtube url. Then the function with the regex is supposed to find the id of the video in that url. By some reason it works when I do it in a seperate file, and if I echo $videocode - that part also works, but when I put everything together, it does not work anymore.


Gabriel Reguly comments:

Hi Torstein,

Perhaps I was not clear enough, I was interested in the actual value you get here


$videocode = get_post_meta($post->ID, 'ecpt_videocode', true);


Dbranes has an interesting code, that will show the $videocode contents, which I suspect is where the trouble lies in.

Regards,
Gabriel

2012-12-01

Arnav Joy answers:

what do you mean by "separate" file? is it header.php or page.php or single.php?


Torstein Opperud comments:

just any file - test.php for instance, totally seperate from wordpress.

like this:

<?php
function parse_yturl($url) {

$pattern = '#^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:.+)?$#x';

preg_match($pattern, $url, $matches);

return (isset($matches[1])) ? $matches[1] : false;

}



$url="https://www.youtube.com/watch?v=7N5OhNplEd4&feature=g-all";



echo parse_yturl($url);?>


will give 7N5OhNplEd4 - which means the function itself works fine.