How to style a shortcode based on acf - html

I want to combine the output of an acf field from my page with my shortcode. The text should be underlined with the color set via an acf field.
I tried to call the field color and set the text-decoration via an inline style. But this is not working. Any ideas what I am doing wrong?
function quote_func($atts, $content = null){
$color = get_field('color');
$output = '<div>';
$output .= '<span style="text-decoration-color:' . echo the_field('color'); . '">' . $content . '</span>';
$output .= '</div>';
return $output;
}
add_shortcode( 'quote', 'quote_func' );

You should echo the variable you set in the beginning of your function.
function quote_func($atts, $content = null){
$color = get_field('color');
$output = '<div>';
$output .= '<span style="text-decoration-color:' . $color . '">' . $content . '</span>';
$output .= '</div>';
return $output;
}
add_shortcode( 'quote', 'quote_func' );

get_field('color') isn't enough to get the value if you're not inside a post, you need a second parameter. You are in a shortcode then you need to use:
get_field('color', $postId);
To get the id of the post from within shortcode you can use:
global $post;
$postId = $post->ID;
If you use the same color for each post you may have the option page and in that case you need to use:
get_field('color', 'option');

Related

Wordpress wpdb not inserting/updating with HTML data

So simply put, I have my PHP code generating a bunch of HTML. I then want to update a database entry like so
$wpdb->update("wp_table",
array( 'content' => $html ),
array( 'id' => 1 ),
array( '%s' ),
array( '%d' )
);
This simply does not work. Nothing in the database changes.
To debug, I placed the exact same code right above with one minor change, and it DOES WORK:
$wpdb->update("wp_sn_cached_popular_display",
array( 'content' => "hello" ),
array( 'id' => 1 ),
array( '%s' ),
array( '%d' )
);
(I swapped out the $html for a direct string.)
I can't even begin to comprehend why, and I've played around with it a lot. I've even done this and it DOES WORK too:
$string = "hello";
$wpdb->update("wp_sn_cached_popular_display",
array( 'content' => $string),
array( 'id' => 1 ),
array( '%s' ),
array( '%d' )
);
It's just the one $html variable is causing this function to not run, or something?
Compiling the $html variable is a bunch of stuff like this:
$html .= '<li>';
$html .= '<div class="upcoming-left">';
$html .= '<time datetime="' . $date . '" class="icon">';
$html .= '<em>' . $date_day.'</em>';
$html .= '<strong>' . $date_month . '</strong>';
$html .= '<span>' . $date_num . '</span>';
$html .= '</time>';
$html .= '</div>';
$html .= '<div class="upcoming-right">';
$html .= '<span class="upcoming-title">' . $upcoming_title . '</span>';
$html .= '<span class="upcoming-desc">' . $upcoming_desc . '</span>';
$html .= '<div class="clear"></div>';
$html .= '</div>';
$html .= '</li>';
//...
Any ideas why this is?
Bonus fun time: This appears to work on my local xampp insall, not the live site.
This could be an issue with table datatypes
https://www.w3schools.com/sql/sql_datatypes.asp
depending on what the table is set to it may not support the string length of your html variable or certain chars in it.
It looks like you concat your .html var multiple times. It's a pretty long string in the end. to test this try to save a very long manually entered string. try with special chars. look at the table structure. This is just some ideas and where my thoughts go.

itunes search store api

I want to get data from the API iTunes Store to my PHP file. In the search form you can fill the artist and title of the song. I want to show only the data from the song. This is my code:
$title = $_POST["title"];
$artist = $_POST["artist"];
$query_string = urlencode($title);
$query_string2 = urlencode($artist);
$json = file_get_contents('https://itunes.apple.com/search?term=$query_string&term=$query_string2'); // this WILL do an http request for you
$data = json_decode($json);
echo "<div class='container' id='movies'>";
echo "<div class='col-md-4' id='movie'>";
echo "<img src='" .$data->artworkUrl60. "' width='200'>";
echo "<h4>" .$data->artistName. " - " .$data->trackName. "</h4>";
i get this error: Notice: Undefined property: stdClass::$artworkUrl60 in....
Whats wrong with my code?
Because your $data Object doesn't contain an attribute named $artworkUrl60.
Your HTTP query doesn't not work correctly, you should use double quote instead of single quote.
// For limit you can add 'limit' parameter
$json = file_get_contents("https://itunes.apple.com/search?term=$query_string&term=$query_string2&limit=20");
// OR concatenation
// $json = file_get_contents('https://itunes.apple.com/search?term='.$query_string.'&term='.$query_string2);
$data = json_decode($json);
if (0 == $data->resultCount) {
echo 'No result found ! ';
}
else {
foreach ($data->results as $row){
echo "<div class='container' id='movies'>";
echo "<div class='col-md-4' id='movie'>";
echo "<img src='" .$row->artworkUrl60. "' width='200'>";
echo "<h4>" .$row->artistName. " - " .$row->trackName. "</h4>";
}
}
https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/#searchexamples

Read txt file into HTML table

I have to read a txt file into a HTML table.
There are many fields in it but I only want to read the field that says "value".
Here is my txt file:
one=availability:, timestamp=90754, value=no
two=description:, timestamp=074693, value=not sure
three=Name, timestamp=90761, value=yes
The one, two, three values are my row headers and I want it to display the values underneath.
Is there anyway of doing this using iframe?? PHP is not working for me.
Supposing the value is always the last field and you are reading the file line by line I would use a dirty approach:
$value = explode('value=', $line)[1];
longwinded approach
$headers=[];
$values=[];
$lines = file('./homework.txt', FILE_IGNORE_NEW_LINES);
foreach($lines as $line){
$chunks = explode(',',$line);
foreach($chunks as $index => $chunk){
list($key, $value) = explode('=', trim($chunk));
if(!$index){
$headers[] = $value;
}
if('value' === $key){
$values[] = $value;
}
}
}
echo "<table><thead><tr>";
foreach($headers as $h){
echo "<th>{$h}</th>";
}
echo "</tr></thead><tbody><tr>";
foreach($values as $v){
echo "<td>{$v}</td>";
}
echo "</tr></tbody></table>";
If all rows follow the same pattern you can probably use:
//$textfilestring = "one=availability:, timestamp=90754, value=no
//two=description:, timestamp=074693, value=not sure
//three=Name, timestamp=90761, value=yes";
$textfilestring = file_get_contents("PATH_TO_FILE");
$arraylines = explode("\n", $textfilestring);
for ($i=0;$i<count($arraylines);$i++) {
$arraylines[$i] = explode("value=", $arraylines[$i]);
}
echo "<pre>";
var_dump($arraylines);
echo "</pre>";
echo $arraylines[0][1] . "<br>";
echo $arraylines[1][1] . "<br>";
echo $arraylines[2][1] . "<br>";
$arraylines should be twodimensional with one part beeing
one=availability:, timestamp=90754,
and one beeing
no
Not tested though.

newline in text area in html form doesn't remain once posted

I'm having a textarea in my contact form with php mail function. In php mail i have set the header to html.
But even if the user types like this
Line1....
Line2....
I'm getting in mail like this.
Line1....Line2....
What could be the reason?
Update:
The text area is as simple as this.
<textarea id ="msg" name="message" cols="" rows="5" class="msg">Message</textarea>
Its posted to this script with jquery ajax function
<?php
$sub = "Message Posted";
$email = "some#somewhere.com";
$message = "<b>Message :</b><br/>" . $_REQUEST["msg"];
$message = wordwrap($message, 70);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ifthi#ifthi.com' . "\r\n" .
'Reply-To: '. $_REQUEST["email"] . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send
mail($email, $sub, $message,$headers);
?>
But when getting in email all are in a single line. Even if you write in 2 line and submit.
if you set your mail to HTML you should replace all line breaks with HTML Tags.
I think the PHP function you need is:
string nl2br ( string $string [, bool $is_xhtml = true ] )
<?php
//whatever you want to replace new lines with
$newLineCode = "<br/>";
$message = $_POST['myTextArea'] ; //unadulterad text we got via Post
$modifiedTextAreaText = ereg_replace( "\n", $newLineCode, $message);
echo " Result of Code Snippet " . $modifiedTextAreaText ;
?>

How to convert mysql table data to Excel or CSV format in CakePHP?

I want to convert my sql data to csv files while clicking on a button. The code fragments I found for sql to CSV conversion were in PHP, and I'm trying to convert it to CakePHP since I'm working in CakePHP.
Here is the PHP code I'm tring to convert:
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field']."; ";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j]."; ";
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
SOLUTION
Function in the Controller:
function exporttocsv()
{
$this->set('headers',$this->Result->find('all',array('fields'=>'Result.label')));
$this->set('values',$this->Result->find('all',array('fields'=>'Result.value')));
}
exporttocsv.ctp file:
<?php
foreach($headers as $header):
$csv_output .=$header['Result']['label'].", ";
endforeach;
$csv_output .="\n";
if(!empty($values)){
foreach($values as $value):
$csv_output .=$value['Result']['value'].", ";
endforeach;
$csv_output .="\n";
}
else{
echo "There is no data to export.";
}
$filename = "export_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>
First of all, you don't do queries and output in the same file in Cake. You query the data as usual in the Controller, $this->set() the result to the view, and in the view you do something like this:
foreach ($results as $result) {
echo join(', ', $result['COLUMNS']);
echo "\n";
}
Outputs something like this:
value, varchar(25), NO, , ,
submitter, int(11), NO, , ,
...
Since Cake automatically wraps a layout around your view, you'll have to set the layout to something different, like 'ajax' (which is simply an empty layout).
deceze is correct about outputting the results from the view file. You'll just need to set some headers so that it appears as a file download on the client side. You can simply put these 2 calls in the top of your view:
header("Content-type:application/vnd.ms-excel");
header("Content-disposition:attachment;filename=\"{$filename}\"" );
If you plan on doing csv downloads in more than one place in your application, I'd recommend this helper:
http://bakery.cakephp.org/articles/view/csv-helper-php5
I use it and it works well.