Wrapping a link tag around PHP - php

I have the following PHP and need to make what's outputted (subheader, not thumbnail) hyperlinked to the parent directory
<?php
//$featured_img=get_template_directory_uri().'/img/default.png';
$featured_img='';
if(get_the_post_thumbnail_url()) {
$featured_img=' style="background-image:url('.get_the_post_thumbnail_url().')"';
}
$subheader= '<p class="entry-subheader"> </p>';
if(get_field('subheader') ) {
$subheader= '<p class="entry-subheader">'.get_field('subheader').'</p>';
}
?>
I have no experience in PHP - only self taught in HTML and CSS so struggling as to where to put in the link.

$subheader= '<p class="entry-subheader">'.get_field('subheader').'</p>';
Cd001's answer worked for me

Related

PHP check if file_exists without extension then Ajax a div with appropriate media tag (img or video) based on filepath

First posting here. I know inline php is not preferred but I haven't converted all my scripts to echo json_encoded arrays to work in javascript on the client side...so for now, I have inline php.
I do not know the extension of the user uploaded media because it could be a jpg,mp4,etc and upon upload it goes into a media folder with the user id as an identifier.
When my user first loads the div (and html page), the php script cycles through an array and does a fetch_assoc from sql query to the database each time; It returns the (media_id #) and prints out an li with the respective media displayed next to some other values from the query.
I only know the (media_id) and the file path name without the extension. When the page first loads, everything works great and the file_exists function returns correctly.
THE PROBLEM
When I AJAX the div and do the query again, because the user added a row to the database, the new list prints out with all info, BUT the file_exists function doesn't recognize the exact same paths as before and I don't have an img or video on the page.
I copy/pasted the exact same code from the original div and put it in a file for ajax to re-query and print the new li's.
All variables are the same and when I hard code a test filepath, it prints fine. Maybe there's a caching issue?
THE CODE
<?php
$result=$conn->query($select);
$row=$result->fetch_assoc();
?>
<li>
<?php
if ($row['count']>0) {
echo "<div class='media-container'>";
$pathname = "uploads/".$row["id"]."media1";
$testjpg=$pathname.".jpg";
$testjpeg=$pathname.".jpeg";
$testpng=$pathname.".png";
$testmp4=$pathname.".mp4";
if (file_exists($testjpg)==TRUE || file_exists($testpng)==TRUE || file_exists($testjpeg)==TRUE) {
echo '<img src="'.$pathname.'">';
}if(file_exists($testmp4)==TRUE) {
echo "<video></video>";
}
echo "</div>";
}?>
</li>
I could use some advice on how to fix this and how to print appropriate media tags on unknown media types.
THE OUTPUT
<div class='media-container'>
</div>
DEBUGGING ATTEMPTS
echoing the exact file path of a known image in an <img> tag works fine. putting echo'test'; inside the file_exists case does nothing.
--
Solution (Kind of)
So I've used html's onerror before and I found a workaround, though I'd still like to know why I was getting an error. PSA this uses JQuery but javascript works too:
My Solution
<script>
function img2video(el, src) {
$( el ).replaceWith( '<video class="videoClass"><source src="'+src+'" type="video/mp4"></video>' );
}
</script>
<body>
<img style="width:100%" onerror="img2video(this,'<?php echo$pathname;?>')" src="<?php echo$pathname;?>">
</body>
Alright, so here's the final answer I made to best fit the problem using glob:
Javascript:
function img2video(el,src,place) {
if (place=='type') {
$( el ).replaceWith( '<video controls controlsList="nodownload" disablePictureInPicture style="width:100%;object-fit:contain;" preload="auto"><source src="'+src+'" type="video/mp4"></video>');
}
}
PHP:
<?php for ( $i=1; $i <= $limit; $i++) {
$path ="[DIRECTORY]/".$row["id"]."media".$i;
$path = (!empty(glob($path . '*.{jpg,png,jpeg,avi,mp4}', GLOB_BRACE)[0])) ? glob($path . '*.{jpg,png,jpeg,avi,mp4}', GLOB_BRACE)[0] : false;?>
<div>
<img onerror="img2video(this,'<?php echo$path;?>','type',<?php echo$row["id"];?>,<?php echo$i;?>)" src="<?php echo$path;?>">
</div>
<?php } ?>
I don't know how to mark as duplicate, if someone could help with that. My answer uses Glob_Brace from #Akif Hussain 's response on This Question.

New browser tabs for pdf files from glob-created list?

To list a large number of sheet-music pdfs on a website, I'm using this at the top of my php/html file:
<?php
header('Content-Type: text/html; charset=utf-8');
function getFiles(){
$files=array();
if($dir=opendir('.')){
while($file=readdir($dir)) {
if($file!='.' && $file!='..'){
$files[]=($file);
}
}
closedir($dir);
}
natsort($files); //sort
return $files;
}
?>
... then in the ul bit of the html section I have this:
<? foreach (glob("*.pdf") as $file)
echo "<li name=\"".$file."\">$file</li>";
?>
It works fine, except that when clicked, the pdf replaces the web page content in the browser, and I would prefer it to appear in a separate browser tab.
I've tried inserting "_blank" in various places but it kills the page, so I'm obviously not doing it right (I'm a musician, not a programmer!).
Similar questions on here seem to relate to different set-ups and I don't have the knowledge to apply the answers to my situation.
How can I get my otherwise successful website set-up to trigger a new browser tab for each tune clicked? Thanks.
"I've tried inserting "_blank" in various places" -- the correct place is in the a element: <a href="..." target="_blank"...>
The code of your template should read:
<?php
foreach (glob("*.pdf") as $file) {
echo '<li name="'.$file.'">'.$file.'</li>';
}
?>

WP Thumbail not rendered correctly

I'm trying to add a thumbail to a div using the_post_thumbnail() function.
The code I'm using is the following:
<div class="sc-col sc-s12 sc-m3 sc-card sc-card-basic">
<figure>'.the_post_thumbnail(array('370', '188')).'</figure>
</div>
But when the code is rendered in a browser the code becomes (just took a part of the rendered code):
<img width="370" height="182" src="/wp-content/uploads/2016/05/image.png">
<div class="sc-col sc-s12 sc-m3 sc-card sc-card-basic">
<figure></figure>
</div>
As you can see the image is rendered outside the div and figure. My question is why is it doing this? Because now my whole layout is breaking now and, the problem is that I have to use that function else I would have used an other function. Can I somehow rewrite the function so it only returns the src link using add_filter function?
https://developer.wordpress.org/reference/functions/the_post_thumbnail/ shows that the function the_post_thumbnail is designed to actually output something to the page, not return a string.
So change your code to something more like this:
$html = '<div class="sc-col sc-s12 sc-m3 sc-card sc-card-basic">
<figure>';
echo $html;
the_post_thumbnail(array('370', '188'))
$html = '</figure>
</div>';
echo $html;
This solved it for me:
<figure><?php the_post_thumbnail(array('370', '188')); ?></figure>
As staded by #ChrisLear the_post_thumbail() doens't return a string and there for fails.

Download music from SoundCloud with tag, Getting the tag

I have a website that grabs songs from Soundcloud. My problem is, all of the files that are downloaded have names like aline!
For example, if I download/get file "I walk alone - green day.mp3", after download it looks like bybbydsbaubx.mp3. What to do with it? I just need the song name because all my users are confused.
Here's a sample of my webpage's code.
<?php
include'func.php';
$id=$_GET['id'];
$grab=json_decode(ngegrab('http://api.soundcloud.com/tracks/'.$id.'.json?client_id=c435ec96ae345d5ce32496d339fc291d'));
$duration=format_time($grab->duration/1000);
if(!empty($grab->genre)){
$genre=$grab->genre;
}else{
$genre='Unknown';
}
$name=$grab->title;
$size=format_size(getfilesize(getlinkdl($id)));
if(!empty($name) && !empty($_GET['id']) && !empty($_GET['permalink'])){
$title='Download '.$name.' ('.$size.')';
include'headl.php';
echo'<div class=title align=justify><span xmlns:v="http://rdf.data-vocabulary.org/#"><span typeof="v:Breadcrumb">Home</span> » <span typeof="v:Breadcrumb">'.$genre.'</span> » <span typeof="v:Breadcrumb"><span class="breadcrumb_last" property="v:title">'.$title.'</span></span></span></div><div class="menu"><div class="ml"><b>Download '.$name.'.mp3</b></div><div class="ml">Genre: '.$genre.'</div><div class="ml">Duration: '.$duration.'</div><div class="ml">Size: '.$size.'</div><div class="ml">Bitrate: 128 kbps</div></div>';
$permexp=explode('-',$grab->permalink);
echo'<div class="list">Tag: ';
foreach($permexp as $perma){
echo''.ucfirst($perma).', ';
}
$ttag=clearspace($genre);
$tgrab=json_decode(ngegrab('http://ws.audioscrobbler.com/2.0/?method=tag.getsimilar&tag='.strtolower($ttag).'&api_key=7df2ba528dcd0d495e3db6284ee6e1a3&format=json'));
$tjumlah=count($tgrab->similartags->tag)-1;
if($tjumlah < 4){
$tcount=$tjumlah;
}else{
$tcount='3';
}
if(!empty($tgrab->similartags->tag[0]->name)){
echo'</div><div class="list">Other Genre';
for($i=0;$i<=$tcount;$i++){
echo''.$tgrab->similartags->tag[$i]->name.', ';
}
}
echo'</div><div class=green align=center><img src="/download.gif"><br/><b>Download '.$name.'.mp3</b><br/></div>';
$genrer=str_replace(' ',',',$genre);
$genrer=str_replace('_',',',$genrer);
$genrer=str_replace('-',',',$genrer);
$jsonr=json_decode(ngegrab('http://api.soundcloud.com/tracks.json?genres='.strtolower($genrer).'&limit=8&offset=0&client_id=c435ec96ae345d5ce32496d339fc291d'));
if (!empty($jsonr[0]->title)){
You could just rename the filename. Then on your code, just replace the old filename with the new one you just made.

search for element name using PHP simple HTML dom parser

I'm hoping someone can help me. I'm using PHP Simple HTML DOM Parser (http://simplehtmldom.sourceforge.net/manual.htm) successfully, but I now am trying to find elements based on a certain name. For example, in the fetched HTML, there might be a tags such as:
<p class="mattFacer">Matt Facer</p>
<p class="mattJones">Matt Jones</p>
<p class="daveSmith">DaveS Smith</p>
What I need to do is to read in this HTML and capture any HTML elements which match anything beginning with the word, "matt"
I've tried
$html = str_get_html("http://www.testsite.com");
foreach($html->find('matt*') as $element) {
echo $element;
}
but this doesn't work. It returns nothing.
Is it possible to do this? I basically want to search for any HTML element which contains the word "matt". It could be a span, div or p.
I'm at a dead end here!
$html = str_get_html("http://www.testsite.com");
foreach($html->find('[class*=matt]') as $element) {
echo $element;
}
Let's try that
Maybe this?
foreach(array_merge($html->find('[class*=matt]'),$html->find('[id*=matt]')) as $element) {
echo $element;
}

Categories