I don't want it to display in the_content. With the_content, it adds a p tag, and i don't want that. I want it display in woocommerce_share hook. How do I do that?
when I try add_action or add_filter code , it doesn't work. I just want to display it in woocommerce product page and my custom post type "Journals"
My custom post type is a blog post. When the user click the post, in the post I want the social icons to show up.
add_filter( 'IS_THERE_A_CUSTOM_FILTER_I_CAN_USE_WITHOUT_THE_P_TAG', 'crunchify_social_sharing_buttons');
add_action( 'woocommerce_share', 'crunchify_social_sharing_buttons');
functions.php
function crunchify_social_sharing_buttons($content) {
if(is_singular() || is_home()){
// Get current page URL
$crunchifyURL = get_permalink();
// Get current page title
$crunchifyTitle = str_replace( ' ', '%20', get_the_title());
// Get Post Thumbnail for pinterest
$crunchifyThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
// Construct sharing URL without using any script
$twitterURL = 'https://twitter.com/intent/tweet?text='.$crunchifyTitle.'&url='.$crunchifyURL.'&via=Crunchify';
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$crunchifyURL;
$googleURL = 'https://plus.google.com/share?url='.$crunchifyURL;
$bufferURL = 'https://bufferapp.com/add?url='.$crunchifyURL.'&text='.$crunchifyTitle;
// Based on popular demand added Pinterest too
$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$crunchifyURL.'&media='.$crunchifyThumbnail[0].'&description='.$crunchifyTitle;
// Add sharing button at the end of page/page content
$content .= '<div class="crunchify-social">';
$content .= '<h5>SHARE ON</h5> <a class="crunchify-link crunchify-twitter" href="'. $twitterURL .'" target="_blank">Twitter</a>';
$content .= '<a class="crunchify-link crunchify-facebook" href="'.$facebookURL.'" target="_blank">Facebook</a>';
$content .= '<a class="crunchify-link crunchify-googleplus" href="'.$googleURL.'" target="_blank">Google+</a>';
$content .= '<a class="crunchify-link crunchify-buffer" href="'.$bufferURL.'" target="_blank">Buffer</a>';
$content .= '<a class="crunchify-link crunchify-pinterest" href="'.$pinterestURL.'" target="_blank">Pin It</a>';
$content .= '</div>';
return $content;
}else{
// if not a post/page then don't include sharing button
return $content;
}
};
add_filter( 'the_content', 'crunchify_social_sharing_buttons');
style.css
.crunchify-link {
padding: 4px 8px 6px 8px;
color: white;
font-size: 12px;
border-radius: 2px;
margin-right: 2px;
cursor: pointer;
-moz-background-clip: padding;
-webkit-background-clip: padding-box;
box-shadow: inset 0 -3px 0 rgba(0,0,0,.2);
-moz-box-shadow: inset 0 -3px 0 rgba(0,0,0,.2);
-webkit-box-shadow: inset 0 -3px 0 rgba(0,0,0,.2);
}
.crunchify-link:hover,.crunchify-link:active {
color: white;
}
.crunchify-twitter {
background: #00aced;
}
.crunchify-twitter:hover,.crunchify-twitter:active {
background: #0084b4;
}
.crunchify-facebook {
background: #3B5997;
}
.crunchify-facebook:hover,.crunchify-facebook:active {
background: #2d4372;
}
.crunchify-googleplus {
background: #D64937;
}
.crunchify-googleplus:hover,.crunchify-googleplus:active {
background: #b53525;
}
.crunchify-buffer {
background: #444;
}
.crunchify-buffer:hover,.crunchify-buffer:active {
background: #222;
}
.crunchify-pinterest {
background: #bd081c;
}
.crunchify-pinterest:hover,.crunchify-pinterest:active {
background: #bd081c;
}
.crunchify-social {
margin: 20px 0px 25px 0px;
-webkit-font-smoothing: antialiased;
font-size: 12px;
}
add_filter( 'IS_THERE_A_CUSTOM_FILTER_I_CAN_USE_WITHOUT_THE_P_TAG', 'crunchify_social_sharing_buttons');
No, there is not. Anything that is added via the the_content filter is ultimately displayed as:
apply_filters( 'the_content', $content );
And the filters that are applied include wpautop(). Your crunchify_social_sharing_buttons function that is filtering the content filter is returning a value. To display the buttons from your custom function on the woocommerce_share hook you would need to echo the value.
add_action( 'woocommerce_share', 'crunchify_social_sharing_buttons');
function crunchify_social_sharing_buttons($content) {
if(is_singular() || is_home()){
// Get current page URL
$crunchifyURL = get_permalink();
// Get current page title
$crunchifyTitle = str_replace( ' ', '%20', get_the_title());
// Get Post Thumbnail for pinterest
$crunchifyThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
// Construct sharing URL without using any script
$twitterURL = 'https://twitter.com/intent/tweet?text='.$crunchifyTitle.'&url='.$crunchifyURL.'&via=Crunchify';
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$crunchifyURL;
$googleURL = 'https://plus.google.com/share?url='.$crunchifyURL;
$bufferURL = 'https://bufferapp.com/add?url='.$crunchifyURL.'&text='.$crunchifyTitle;
// Based on popular demand added Pinterest too
$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$crunchifyURL.'&media='.$crunchifyThumbnail[0].'&description='.$crunchifyTitle;
// Add sharing button at the end of page/page content
$content .= '<div class="crunchify-social">';
$content .= '<h5>SHARE ON</h5> <a class="crunchify-link crunchify-twitter" href="'. $twitterURL .'" target="_blank">Twitter</a>';
$content .= '<a class="crunchify-link crunchify-facebook" href="'.$facebookURL.'" target="_blank">Facebook</a>';
$content .= '<a class="crunchify-link crunchify-googleplus" href="'.$googleURL.'" target="_blank">Google+</a>';
$content .= '<a class="crunchify-link crunchify-buffer" href="'.$bufferURL.'" target="_blank">Buffer</a>';
$content .= '<a class="crunchify-link crunchify-pinterest" href="'.$pinterestURL.'" target="_blank">Pin It</a>';
$content .= '</div>';
echo $content;
}
}
This works, but for my social-bar.php code I remove the $value in add_filter. I'm not sure if that is the right way to do it. Because I don't to print any value. Can someone correct me if this is the right way to do it. Thanks
echo apply_filters( 'social_bar');
FUNCTION.php
function crunchify_social_sharing_buttons($content) {
if(is_singular() || is_home()){
// Get current page URL
$crunchifyURL = get_permalink();
// Get current page title
$crunchifyTitle = str_replace( ' ', '%20', get_the_title());
// Get Post Thumbnail for pinterest
$crunchifyThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
// Construct sharing URL without using any script
$twitterURL = 'https://twitter.com/intent/tweet?text='.$crunchifyTitle.'&url='.$crunchifyURL.'&via=Crunchify';
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$crunchifyURL;
$googleURL = 'https://plus.google.com/share?url='.$crunchifyURL;
$bufferURL = 'https://bufferapp.com/add?url='.$crunchifyURL.'&text='.$crunchifyTitle;
// Based on popular demand added Pinterest too
$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$crunchifyURL.'&media='.$crunchifyThumbnail[0].'&description='.$crunchifyTitle;
// Add sharing button at the end of page/page content
$content .= '<div class="crunchify-social">';
$content .= '<h5>SHARE ON</h5> <a class="crunchify-link crunchify-twitter" href="'. $twitterURL .'" target="_blank">Twitter</a>';
$content .= '<a class="crunchify-link crunchify-facebook" href="'.$facebookURL.'" target="_blank">Facebook</a>';
$content .= '<a class="crunchify-link crunchify-googleplus" href="'.$googleURL.'" target="_blank">Google+</a>';
$content .= '<a class="crunchify-link crunchify-buffer" href="'.$bufferURL.'" target="_blank">Buffer</a>';
$content .= '<a class="crunchify-link crunchify-pinterest" href="'.$pinterestURL.'" target="_blank">Pin It</a>';
$content .= '</div>';
return $content;
}else{
// if not a post/page then don't include sharing button
return $content;
}
};
add_filter( 'the_content', 'crunchify_social_sharing_buttons');
add_filter( 'social_bar', 'crunchify_social_sharing_buttons');
social-bar.php
<!-- socialbar-->
<?php
echo apply_filters( 'social_bar');
?>
<!-- /socialbar-->
Display the social bar where I want
<?php
get_template_part('socialbar');
?>
Related
A couple of months ago, I asked a similar question like this and the answer that where given worked for me. I now have another change I would like to add to my page. I would like that each post I create has its own unique div. My page currently looks like this:
the previous question helped me break the div each 3 post, so what I tried was within the if statement that creates a new dive each 3 div was to add another if which would break each 1 div so that each post has its own div and it still breaks to a new div section each 3, maybe I just complicated everything with my description, but I want to get something like:
Here is my code
CSS:
.column {
display: inline-flex;
border: 5px black;
border-style: solid;
padding: 10px;
background: #ffa500;
}
PHP:
else {
$break = 0;
$nRows = $connection->prepare("SELECT post_id, post_title,
post_author, post_file, post_time
FROM posts
ORDER BY post_id DESC");
$nRows->execute();
if($nRows->rowCount() > 0) {
while ($row = $nRows->fetch()) {
$post_title = str_replace('_', ' ', $row['post_title']);
$post_author = $ed->encrypt_decrypt('decrypt',$row['post_author']);
$post_file = $row['post_file'];
$post_date = $row['post_time'];
// Create a new div each 3 columns
if ($break % 3 === 0) {
echo '<br><div class="column"><br>';
}
$break++;
?>
<!-- Blog Content BEGIN Display-->
<div class="box"><?php
// Display the content
$file_parts = pathinfo($post_file);
if(isset($file_parts['extension'])) {
switch ($file_parts['extension']) {
case "jpg":
if(!empty($post_file)) { ?>
<img src="post/postFiles/<?php echo $post_file;?>"><?php
}
break;
case "mp4":?>
<div class="thumbnail">
<video preload="auto" loop muted>
<source src="post/postFiles/<?php echo $post_file;?>">
</video>
</div>
<!-- Preview video on hover -->
<script>
$(document).ready(function () {
$(".thumbnail").hover(function () {
$(this).children("video")[0].play();
}, function () {
var el = $(this).children("video")[0];
el.pause();
el.currentTime = 0;
});
});
</script><?php
break;
case "": // Handle file extension for files ending in '.'
case NULL: // Handle no file extension
break;
}
}
// Title URL Variable
$urlFetchPostId = '<h2><a href="post/postFetch/fetchByTitle/fetchByPT.php?post_id=';
$urlFetchPostTitle = '&post_title=';
$urlFetchPostAuthor = '&post_author=';
echo $urlFetchPostId . $row['post_id'] . $urlFetchPostAuthor. $row['post_author']. $urlFetchPostTitle . $row['post_title'] . '"' . 'class="link-post-title" style="font-family: Arial">' . " ". $post_title . '</a></h2>';
// Author/User URL Variable
$urlFetchPostUser = '<a href="post/postFetch/fetchByAuthor/fetchByPA.php?post_author=';
echo $urlFetchPostUser . $row['post_author'] . '"' . 'class="link-post-author" style="font-family: Arial">' . " ". strtoupper($post_author) . '</a>';
// Posted Date
echo '<br><p style="font-family: Arial">Posted on ' . $post_date . '</p>';
?>
</div><?php
if ($break % 3 === 0) {
echo '<br></div><br>';
}?>
<!-- Blog Content END Display --><?php
}
} else { ?>
<p style="color: darkgoldenrod" class="mssgAlign"><u>NO RECORDS</u></p><?php
}
$nRows = null;
}
Any help, tip or improvement suggestion is welcomed
You want to use margins. Margins specify a buffer around the outside of your container. As opposed to padding, which specifies buffer inside the container. Add this to your css
.column {
display: inline-flex;
border: 5px black;
border-style: solid;
padding: 10px;
background: #ffa500;
margin-left: 20px;
margin-right: 20px;
}
I'm using bootstrap and Advanced Custom Fields plugin. I created a field of image, but image is not responsive. Can anybody help and have any suggestions how can I edit this code to make image responsive on small screens.
Link to my website: http://vizionstar.co.uk/global_listings/
And here is my code which I'm using to get the image from front-page.php:
if(get_field('testimonial_content')) {
echo '<div class="info-box-container">';
$image = get_field('Image');
$size = 'full';
if( $image ) {
echo wp_get_attachment_image( $image, $size );
}
echo '<h3>' . get_field('testimonial_title') . '</h3>';
echo '<h1>' . get_field('testimonial_title2') . '</h1>';
echo '<h1>' . get_field('testimonial_title3') . '</h1>';
the_field('testimonial_content');
echo '</div>';
}
The image is on testimonials section "WHAT THE INDUSTRY SAYS ABOUT US".
The easiest way is to add this to your style.css:
.info-box img {
max-width: 100%;
height: auto;
}
Try adding this line to your CSS file
img.attachment-full.size-full {
width: 100%;
height: 100%;
}
I'm pretty new to PHP and am really learning as I do, no doubt wrongly at times. I've put together a script that reads a CSV containing orders for an e-commerce site and uses the data to send a completion email to the customer.
My problem is that if a customer orders several items in one order they are sent an email for each of those items rather than all the order items with the same order ID being contained in one email.
Its taken me several hours to to get to this point as I've literally been learning as I go but try as I might I'm banging my head against a brick wall with this loop issue and how to go about it. Any help, guidance or direction pointing would be much appreciated!
///////////// RUN THE CSV IMPORT ////////////
if(!file_exists($csvfile)) {
file_put_contents($debug, $date.$m5.PHP_EOL, FILE_APPEND);
exit;
}
$file = fopen($csvfile,"r");
if(!$file) {
file_put_contents($debug, $date.$m6.PHP_EOL, FILE_APPEND);
exit;
}
$size = filesize($csvfile);
if(!$size) {
file_put_contents($debug, $date.$m7.PHP_EOL, FILE_APPEND);
exit;
}
$csvcontent = fread($file,$size);
fclose($file);
$lines = 0;
$queries = "";
$linearray = array();
foreach(explode($lineseparator,$csvcontent) as $line) {
if($lines == 0){ $lines++; continue; }
if (empty($line)) { break; }
$lines++;
$line = trim($line," \t");
$line = str_replace("\r","",$line);
$line = str_replace("\"","",$line);
$linearray = explode($fieldseparator,$line);
$linemysql = implode("','",$linearray);
// Set The Variables
$order_id = $linearray[0];
$order_total = $linearray[1];
$payment_method = $linearray[2];
$shipping_cost = $linearray[3];
$total_quantity = $linearray[6];
$billing_first_name = $linearray[6];
$billing_last_name = $linearray[7];
$billing_company = $linearray[8];
$billing_street1 = $linearray[9];
$billing_street2 = $linearray[10];
$billing_city = $linearray[11];
$billing_zip = $linearray[12];
$billing_state = $linearray[13];
$billing_country = $linearray[14];
$billing_phone = $linearray[15];
$billing_email = $linearray[16];
$shipping_first_name = $linearray[17];
$shipping_last_name = $linearray[18];
$shipping_company = $linearray[19];
$shipping_street1 = $linearray[20];
$shipping_street2 = $linearray[21];
$shipping_city = $linearray[22];
$shipping_zip = $linearray[23];
$shipping_state = $linearray[24];
$shipping_country = $linearray[25];
$sku = $linearray[26];
$product_name = $linearray[27];
$order_line_quantity = $linearray[28];
$order_line_total_price = $linearray[29];
$ship_provider = $linearray[31];
$ship_tracking = $linearray[32];
$order_status = 'wc-completed';
$ship_date = date("Y-m-d");
$ship_sent = "on";
// Use MySQLi for regular price
$mysqli = new MySQLi($hostname, $username, $password, $database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit;
}
$price_id = $mysqli->query("SELECT `post_id` FROM `dk_postmeta` WHERE `meta_value` LIKE '$sku'")->fetch_row()[0];
$regular_price = $mysqli->query("SELECT `meta_value` FROM `dk_postmeta` WHERE `post_id` =$price_id AND `meta_key` LIKE '_regular_price'")->fetch_row()[0];
// CLOSE CONNECTION
mysqli_close($mysqli);
$message = '<!DOCTYPE HTML>'.
'<head>'.
'<meta http-equiv="content-type" content="text/html">'.
'<meta charset="UTF-8">'.
'<title>mysite order complete</title>'.
'<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,300&subset=latin,latin-ext" rel="stylesheet" type="text/css">'.
'<style>
body{font-family: "Open Sans",Arial,Helvetica,sans-serif;font-weight: 400;font-size:16px;line-height:26px;color:#212121;}
a {color: #7E1414;text-decoration: none;outline: 0px none !important;}
.clear{clear:both;}
.wrapper {margin:0 auto;}
.header {background-color: #151515;color: #FFF;width: auto;height: 70px;border: 1px solid #3C3C3C;padding:0 15px;margin-bottom:20px;}
a.logo {font-size: 38px;color: #FFF;line-height: 38px;letter-spacing: 1px;padding: 15px 0px 0px;}
.logo {float: left;}
.logo-small {font-family: Open Sans;text-transform: uppercase;font-size: 12px;margin: 0px;padding: 0px;}
.alert {font-size: 30px;color: #FFF;line-height: 38px;letter-spacing: 1px;padding: 15px 0px 0px;float:right}
.container {width:100%;padding:0}
.inner {margin:0 auto; height:100%;}
.my-account h2 {font-size: 22px;line-height: 30px;border-bottom: 1px solid #ABABAB;padding-bottom: 10px;margin: 0px 0px 20px;font-weight:normal;}
.order-box {padding:10px 20px;border:1px solid #ababab;}
.row {border-bottom:1px solid #E3E3E3;padding:10px 0;}
.row-totals {padding:10px 0 2px;}
.red{color:#7E1414;}
.product{width:48%;float:left;}
.product img {max-width:90px;}
.product .img-box{float:left;max-width:90px;border: 1px solid #CFCFCF;border-radius:3px;}
.product p {float:left;padding:0 0 0 10px;margin:0}
.price{width:20%;float:left;}
.quantity{width:10%;float:left;text-align:right;}
.total{width:20%;float:right;text-align:right;}
.totals-box {margin-top:0px;float:right;width:48%}
.totals-inner {padding:10px 20px 15px;border:1px solid #ababab;margin:20px 0;}
.alert-inner,.customer-inner {padding:0 20px;border:1px solid #ababab;margin:0 0 20px;}
.sub-left{float:left;width:40%;color:#7E1414;}
.sub-right{float:right;width:40%;text-align:right;}
.customer-inner {padding:10px 20px 0;margin:0px;}
.black {color:#212121 !important;}
.billing-box {}
.bbox-left {width:48%;float:left;}
.bbox-right {width:48%;float:right;}
.bbox-right .totals-inner,.bbox-left .totals-inner{padding:10px 20px}
.billing-box h3 {font-size: 18px;line-height: 30px;border-bottom: 1px solid #ABABAB;padding-bottom: 10px;margin: 0px 0px 20px;font-weight:normal;}
.billing-box p {padding:0;margin:0;}
.footer{background-color: #151515;color: #FFF;width: auto;border: 1px solid #3C3C3C;padding:0 15px;margin-bottom:0px;}
.footer p {font-size:20px;line-height:30px;padding:0;text-align:center;}
</style>'.
'</head>'.
'<body>'.
'<div class="wrapper">'.
'<div class="header">'.
'<div class="logo">'.
'<a class="logo" href="https://example.com">mysite <span class="logo-small">small logo</span></a>'.
'</div>'.
'<div class="alert">'.
'Order Shipped'.
'</div>'.
'</div>'.
'<div class="container">'.
'<div class="inner">'.
'<div class="alert-inner">'.
'<p>Dear '.$billing_first_name.','.
'<p>We just wanted to let you know that your recent order with mysite has been completed and shipped on '.$ship_date.'. You can track your order at the EMS website by using their tracking facility and your tracking number '.$ship_tracking.'.</p>'.
'<p>For your reference your order details are below. Thank you for shopping with mysite!</p>'.
'</div>'.
'<div class="my-account">'.
'<div class="order-box">'.
'<h2>Order Details</h2>'.
'<div class="row red">'.
'<div class="product">Product</div>'.
'<div class="price">Price</div>'.
'<div class="quantity">Quantity</div>'.
'<div class="total">Total</div>'.
'<div class="clear"></div>'.
'</div>'.
// NEED TO LOOP THIS SECTION WITH DATA FROM OTHER ROWS THAT MATCH $order_id
'<div class="row">'.
'<div class="product">'.
'<div class="img-box">'.
'<img src="https://example.com/wp-content/uploads/2015/04/'.$sku.'-1-135x180.jpg">'.
'</div>'.
'<p class="item">'.$product_name.'</p>'.
'</div>'.
'<div class="price">USD '.$regular_price.'</div>'.
'<div class="quantity">'.$order_line_quantity.'</div>'.
'<div class="total">USD '.$order_line_total_price.'</div>'.
'<div class="clear"></div>'.
'</div>'.
// END THE LOOP
'</div>'.
'<div class="totals-box">'.
'<div class="totals-inner">'.
'<div class="row row-totals">'.
'<div class="sub-left">SubTotal</div>'.
'<div class="sub-right">USD '.$order_line_total_price.'</div>'.
'<div class="clear"></div>'.
'</div>'.
'<div class="row row-totals">'.
'<div class="sub-left">Shipping</div>'.
'<div class="sub-right">USD '.$shipping_cost.'</div>'.
'<div class="clear"></div>'.
'</div>'.
'<div class="row row-totals">'.
'<div class="sub-left">Payment Method</div>'.
'<div class="sub-right">'.$payment_method.'</div>'.
'<div class="clear"></div>'.
'</div>'.
'<div class="row row-totals">'.
'<div class="sub-left">Total</div>'.
'<div class="sub-right">USD '.$order_total.'</div>'.
'<div class="clear"></div>'.
'</div>'.
'</div>'.
'</div>'.
'<div class="clear"></div>'.
'<div class="customer-inner">'.
'<h2>Customer details</h2>'.
'<p><spcan class="black">Telephone:</span> '.$billing_phone.'</p>'.
'<p>Email: '.$billing_email.'</p>'.
'</div>'.
'<div class="billing-box">'.
'<div class="bbox-left">'.
'<div class="totals-inner">'.
'<h3>Billing Details</h3>'.
'<p>'.$billing_first_name.' '.$billing_last_name.'</p>'.
'<p>'.$billing_company.'</p>'.
'<p>'.$billing_street1.'</p>'.
'<p>'.$billing_street2.'</p>'.
'<p>'.$billing_city.'</p>'.
'<p>'.$billing_zip.'</p>'.
'<p>'.$billing_state.'</p>'.
'<p>'.$billing_country.'</p>'.
'<div class="clear"></div>'.
'</div>'.
'</div>'.
'<div class="bbox-right">'.
'<div class="totals-inner">'.
'<h3>Shipping Details</h3>'.
'<p>'.$shipping_first_name.' '.$shipping_last_name.'</p>'.
'<p>'.$shipping_company.'</p>'.
'<p>'.$shipping_street1.'</p>'.
'<p>'.$shipping_street2.'</p>'.
'<p>'.$shipping_city.'</p>'.
'<p>'.$shipping_zip.'</p>'.
'<p>'.$shipping_state.'</p>'.
'<p>'.$shipping_country.'</p>'.
'<div class="clear"></div>'.
'</div>'.
'</div>'.
'<div class="clear"></div>'.
'</div>'.
'</div>'.
'<div class="footer">'.
'<p>Thank you for your custom!</p>'.
'</div>'.
'</div>'.
'</body>';
/*EMAIL TEMPLATE ENDS*/
$to = $billing_email; // give to email address
$subject = 'Your Order Is Complete'; //change subject of email
$from = 'orders#example.com'; // give from email address
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "CC: \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type:text/html;charset=utf-8\r\n";
// Sending mail
if(mail($to, $subject, $message, $headers))
{
echo 'HTML email sent successfully!';
}
else
{
echo 'Problem sending HTML email!';
}
I have a little problem in my Shortcode which I am creating for WordPress. The foreach loop is not displaying multiple posts each time. I'm not sure why is it doing this because if I var_dump the $post variable it shows that both the posts are available to that variable, can someone help me out please?
CODE:
function notes_shortcode($atts) {
global $post;
$atts = shortcode_atts( array( 'category' => $args["category"]), $atts, 'notes' );
$args = array( 'category_name' => $atts["category"]);
$posts = get_posts( $args );
$date = get_the_date( 'd', $post->ID );
$month = get_the_date( 'M', $post->ID );
foreach( $posts as $post ) {
setup_postdata($post);
$imgURL = getpostImage( $post->ID );
$title = get_the_title( $post->ID );
$content = substr(get_the_content() , 0, 125);
$post = '<div class="animated fadeInUp" data-animation="fadeInUp" data-delay="200" style="opacity: 0;">';
$post .= '<div class="col-md-4 bloglist">';
$post .= '<div class="post-content">';
$post .= '<div class="post-image">';
$post .= '<div class="flexslider blog-slider">';
$post .= '<div class="overlay" style="opacity: 0;"></div>';
$post .= '<div class="flex-viewport" style="overflow: hidden; position: relative;">';
$post .= '<ul class="slides" style="width: 800%; -webkit-transition: 0s; transition: 0s; -webkit-transform: translate3d(-350px, 0px, 0px);">';
$post .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$post .= '<li class="flex-active-slide" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$post .= '<li style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$post .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"></li>';
$post .= '</ul></div></div></div>';
$post .= '<div class="date-box"><span class="day">' . $date . '</span>';
$post .= '<span class="month">' . $month . '</span> </div>';
$post .= '<div class="post-text">';
$post .= '<h3>' . $title . '</h3>';
$post .= '<p> ' . $content . '<br>';
$post .= ' Read More</p></div></div></div></div>';
return $post;
}
}
add_shortcode( 'notes', 'notes_shortcode' );
function getpostImage($postid) {
if (has_post_thumbnail($post->ID)){
$imgArray = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID), 'thumbnail' );
$imgURL = $imgArray[0];
return $imgURL;
}
}
Thanks ..
Okay, I solved the problem. I used the WordPress Filters to hook the $buff variable and returned it outside the loop, below is the solution if anyone needs it.
function notes_shortcode($atts) {
global $post;
global $buf;
$atts = shortcode_atts( array( 'category' => $args["category"], 'posts_per_page' => $args["posts_per_page"]), $atts, 'notes' );
$args = array( 'category_name' => $atts["category"], 'posts_per_page' => $atts["posts_per_page"] );
$posts = get_posts( $args );
$date = get_the_date( 'd', $post->ID );
$month = get_the_date( 'M', $post->ID );
$buf = '';
$postHolder = array();
foreach( $posts as $post ) {
setup_postdata($post);
$imgURL = getpostImage( $post->ID );
$title = get_the_title( $post->ID );
$content = substr(get_the_content() , 0, 125);
$buf .= '<div class="animated fadeInUp" data-animation="fadeInUp" data-delay="200" style="opacity: 0;">';
$buf .= '<div class="col-md-4 bloglist">';
$buf .= '<div class="post-content">';
$buf .= '<div class="post-image">';
$buf .= '<div class="flexslider blog-slider">';
$buf .= '<div class="overlay" style="opacity: 0;"></div>';
$buf .= '<div class="flex-viewport" style="overflow: hidden; position: relative; max-width: 350px; max-height: 175px; padding-bottom: 15px; margin-bottom: 15px;">';
$buf .= '<ul class="slides" style="width: 800%; -webkit-transition: 0s; transition: 0s; -webkit-transform: translate3d(-350px, 0px, 0px);">';
$buf .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$buf .= '<li class="flex-active-slide" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$buf .= '<li style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$buf .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"></li>';
$buf .= '</ul></div></div></div>';
$buf .= '<div class="date-box"><span class="day">' . $date . '</span>';
$buf .= '<span class="month">' . $month . '</span> </div>';
$buf .= '<div class="post-text">';
$buf .= '<h3>' . $title . '</h3>';
$buf .= '<p> ' . $content . '<br>';
$buf .= ' Read More</p></div></div></div>';
$buf .= apply_filters( 'post_class', '</div>', $atts );
}
$buf .= apply_filters( 'post_class', '', $atts );
return $buf;
}
add_shortcode( 'notes', 'notes_shortcode' );
function getpostImage($postid) {
if (has_post_thumbnail($post->ID)){
$imgArray = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID), 'thumbnail' );
$imgURL = $imgArray[0];
return $imgURL;
}
}
Thankyou all for the help ..
You are returning the function, that means that your are making the function end. What you can do is send the array and then loop it later.Use the code below
function notes_shortcode($atts) {
global $post;
$atts = shortcode_atts( array( 'category' => $args["category"]), $atts, 'notes' );
$args = array( 'category_name' => $atts["category"]);
$posts = get_posts( $args );
$date = get_the_date( 'd', $post->ID );
$month = get_the_date( 'M', $post->ID );
$array = array();
foreach( $posts as $post ) {
setup_postdata($post);
$imgURL = getpostImage( $post->ID );
$title = get_the_title( $post->ID );
$content = substr(get_the_content() , 0, 125);
$post = '<div class="animated fadeInUp" data-animation="fadeInUp" data-delay="200" style="opacity: 0;">';
$post .= '<div class="col-md-4 bloglist">';
$post .= '<div class="post-content">';
$post .= '<div class="post-image">';
$post .= '<div class="flexslider blog-slider">';
$post .= '<div class="overlay" style="opacity: 0;"></div>';
$post .= '<div class="flex-viewport" style="overflow: hidden; position: relative;">';
$post .= '<ul class="slides" style="width: 800%; -webkit-transition: 0s; transition: 0s; -webkit-transform: translate3d(-350px, 0px, 0px);">';
$post .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$post .= '<li class="flex-active-slide" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$post .= '<li style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"> </li>';
$post .= '<li class="clone" aria-hidden="true" style="width: 350px; float: left; display: block;"> <img src="' . $imgURL . '" alt="" draggable="false"></li>';
$post .= '</ul></div></div></div>';
$post .= '<div class="date-box"><span class="day">' . $date . '</span>';
$post .= '<span class="month">' . $month . '</span> </div>';
$post .= '<div class="post-text">';
$post .= '<h3>' . $title . '</h3>';
$post .= '<p> ' . $content . '<br>';
$post .= ' Read More</p></div></div></div></div>';
$array[] = $post;
}
return $array;
}
add_shortcode( 'notes', 'notes_shortcode' );
function getpostImage($postid) {
if (has_post_thumbnail($post->ID)){
$imgArray = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID), 'thumbnail' );
$imgURL = $imgArray[0];
return $imgURL;
}
}
Hope this helps you
I am trying to develop a plugin that send the customer a "gift receipt" upon completing an order. I have everything working perfectly except I am unable to strip the price from the emails. When I try to edit the email-order-items.php template and remove the price column then the emails come in blank.
Specifically this is for downloadable products so the download links no longer show when I make any edits to the email-order-items file. And I would only want it to strip the price from the gift receipt emails not the other emails.
What I have done:
in my plugin I call on a email template "customer-gift-receipt.php" which is pretty much the same as "customer-processing-order.php" that comes package with Woocommerce.
In the file there is this line that brings in the email-order-items template and shows the links and price
<?php echo $order->email_order_items_table( $order->is_download_permitted(),
true, ($order->status=='processing') ? true : false ); ?>
This is the email-order-items template.
No matter what I do I cannot seem to get those prices stripped from just the customer-gift-receipt.php emails. Specifically it is this line in the above template:
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">
<?php echo $order->get_formatted_line_subtotal( $item ); ?>
</td>
I tried creating a duplicate of the email-order-items template and removing that line then calling it in my plugin and that did not work. I also tried copying the email-order-items template inside of the customer-gift-receipt.php file in the appropriate section and that failed as well. I had to define $items = $order->get_items(); when I tried copying email-order-items directly into the customer-gift-receipt template for it to somewhat work.
So can anyone suggest a way for me to strip the prices out of my customer-gift-receipt templates?
I have checked through these links:
Class WC_Order
Class WC_Email_Customer_Processing_Order
UPDATE:
I just found this link which should help me bring in email_order_items_table outside of the class: https://gist.github.com/mikejolley/1965842
When I try to add the above code in my customer-email-receipt template and place an order I get this error:
Fatal error: Class 'order_item_meta' not found in
.../.../.../.../woocommerce/emails/customer-gift-receipt.php on line 41"
Disable e-mail order items table from e-mail template and copy the function as a custom function into your theme.
<tbody>
<?php //echo $order->email_order_items_table( $order->is_download_permitted(), true, ($order->status=='processing') ? true : false );
echo custom_order_table($order);
?>
</tbody>
I had to remove download and variations which causes *Fatal Error with Class 'order_item_meta'* error. So your custom function will look like:
function custom_order_table($order,$price = false) {
foreach($order->get_items() as $item) :
$_product = $order->get_product_from_item( $item );
$file = $sku = $variation = $image = '';
if ($show_image) :
$src = wp_get_attachment_image_src( get_post_thumbnail_id( $_product->id ), 'thumbnail');
$image = apply_filters('woocommerce_order_product_image', '<img src="'.$src[0].'" alt="Product Image" height="'.$image_size[1].'" width="'.$image_size[0].'" style="vertical-align:middle; margin-right: 10px;" />', $_product);
endif;
if ($show_sku && $_product->get_sku()) :
$sku = ' (#' . $_product->get_sku() . ')';
endif;
$return .= '<tr>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">'. $image . apply_filters('woocommerce_order_product_title', $item['name'], $_product) . $sku . $file . $variation . '</td>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">'.$item['qty'].'</td>';
if ($price):
$return .= '<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">';
if ( $order->display_cart_ex_tax || !$order->prices_include_tax ) :
$ex_tax_label = ( $order->prices_include_tax ) ? 1 : 0;
$return .= woocommerce_price( $order->get_line_subtotal( $item ), array('ex_tax_label' => $ex_tax_label ));
else :
$return .= woocommerce_price( $order->get_line_subtotal( $item, true ) );
endif;
$return .= '</td>';
endif;
$return .= '</tr>';
// Show any purchase notes
if ($show_purchase_note) :
if ($purchase_note = get_post_meta( $_product->id, '_purchase_note', true)) :
$return .= '<tr><td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee;">' . apply_filters('the_content', $purchase_note) . '</td></tr>';
endif;
endif;
endforeach;
echo $return;
}