Adding a Dynamic Date as a Wordpress Function - wordpress

I want to add a shortcode function in my Wordpress site that shows the date one week from now, two weeks from now and 3 days from now. I'd like to be able to add the shortcode [addoneweek] on pages where I need them.
Here is the code that I have, but it isn't working. What am I doing wrong?
function addoneweek( $aow )
{
return(<?php $now = new DateTime();
echo $now->add(new DateInterval('P1W'))->format('m-d-Y');
?>)
}
add_shortcode( 'arttime', 'addoneweek');

Try below code in your current functions.php
add_shortcode('arttime', 'arttime');
function arttime() {
$Today = date('d:m:y');
// add 3 days to date
$html = Date('d:m:y', strtotime("+3 days")) . '<br/>';
// add 7 days to date
$html .= Date('d:m:y', strtotime("+7 days")) . '<br/>';
// add 14 days to date
$html .= Date('d:m:y', strtotime("+14 days")) . '<br/>';
return $html;
}

Firstly, I would suggest reading over the add_shortcode codex entry. It will explain how to use attributes/arguments.
Secondly, you are returning on your third line, which means that the echo on the fourth line is not going to run.
Thirdly, you also have a random opening <?php tag after your return, which you then try to close on your fifth line.
Fourth, and this is not really a big problem, but you should actually return your end result, instead of echo'ing it. echo will still output your request, but you're then essentially running an echo inside and echo when the shortcode runs.
This is what your function should look like:
function addoneweek()
{
$now = new DateTime();
return $now->add(new DateInterval('P1W'))->format('m-d-Y');
}
add_shortcode( 'arttime', 'addoneweek');

Related

How to retrieve the goal name in Analytics using APIs

I'm trying to retrieve the name of my Analytics goals through the Google API using PHP. I tried this code but I don't understand why it creates a loop and does not return any value to me.
Anyone have any ideas?
Thanks so much
$goals = $analytics->management_goals->listManagementGoals('123456', 'UA-123456-1', '~all');
foreach ($goals->getItems() as $key) {
$html = <<<HTML
<pre>
Goal id = {$key->getId()}
Goal name = {$key->getName()}
HTML;
$html .= '</pre>';
print $html;
}

Additional order email based on custom customer field

I need to send a copy of the order email to a second email adress defined by a custom field.
I have installed the iconic custom account field plugin and created a custom field on the user (I works and I can edit it in admin).
https://iconicwp.com/blog/the-ultimate-guide-to-adding-custom-woocommerce-user-account-fields/
It's named 'iconic-register-email'
I have also implemented this https://wordpress.stackexchange.com/questions/92020/adding-a-second-email-address-to-a-completed-order-in-woocommerce solution to send an extra email. It works with a hard-coded email.
Now I need to combine the solutions and I just can't get it to work. I have appended the send email code at the bottom of the iconic plugin code.
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);
function mycustom_headers_filter_function( $headers, $object ) {
$sae = 'a-custom-email#gmail.com';
if ($object == 'customer_completed_order') {
$headers .= 'BCC: Name <' . $sae . '>' . "\r\n";
}
return $headers;
}
This code works, but I need to use iconic-register-email field.
Need to fetch the registred email adress on a user, so when that user makes an order an order copy is sent to that adress defined by iconic-register-email.
My knowledge about coding is very limited, please help. Last thing to add before I can go live.
Managed to solve it. Not sure if it's the most elegant solution, but it works.
As it's predefined functions and arrays, this is how I did it
function mycustom_headers_filter_function( $headers, $object ) {
$sae = iconic_get_userdata( get_current_user_id(), 'iconic-register-email' );
if ($object == 'new_order') {
$headers .= 'BCC: <' . $sae . '>' . "\r\n";
}
return $headers;
}

Edit site_url with filter

With WordPress, calling site_url() returns the full site URL (http://www.example.com)
What I'm trying to do is adding something (add-something-here) at the end of the URL with filter.
The result I'm expecting is:
http://www.example.com/add-something-here
Does someone know how to do that with filter?
I tried the following with no success:
function custom_site_url($url) {
return get_site_url('/add-something-here');
}
add_filter('site_url', 'custom_site_url');
The problem is that you are producing a loop with this filter. The function get_site_url is exactly where the filter site_url is being called.
You need:
add_filter( 'site_url', 'custom_site_url' );
function custom_site_url( $url )
{
if( is_admin() ) // you probably don't want this in admin side
return $url;
return $url .'/something';
}
Bear in mind, that this may produce errors for scripts that rely on the real URL.
Untested, but since it's PHP could you just try...
function custom_site_url($url) {
return get_site_url() . '/add-something-here';
}
add_filter('site_url', 'custom_site_url');
Untested, but this is a snippet of what I use often:
$constant = 'add-something-here';
return ''. esc_html( $name ).'';
or you might simply want to get the page ID instead and it's much better.
$page_id = '2014';
return '.esc_html__( 'pagename', 'text-domain' ).'

Wordpress shortcode only returning 1 from feed despite using foreach

I am trying to create a custom Wordpress plugin which utilises shortcodes to output what I want. In this text code I am attempting to read an rss file and spit out just a list of the top 5 feeds.
The $showno is one of the shortcode variables so I can use the following [player show=foo snowno=5]. In the example code $show isn't used.
The code below only shows the most recent item in the feed list. If I change the return to echo then it works as expected except it show at the top of the post instead of where I've entered the shortcode. I searched for an answer to this and the solution offered was simply "use return" which I've done...
Appreciate advice. Thanks
include_once(ABSPATH . WPINC . '/rss.php');
$num_items = $showno;
$feedurl = 'http://feeds.bbci.co.uk/news/rss.xml';
$feed = fetch_rss($feedurl);
$items = array_slice ($feed->items, 0, $num_items);
foreach ($items as $item ) {
$title = $item[title];
$mp3link = $item[link];
$description = $item[description];
return "<li>$title - $description</li>";
}
Shortcodes should be returned not echoed.
In your code, you are breaking the execution of the foreach and returning the first value.
You should build a string variable and after the foreach loop return it, so all your loop will be included, e.g.:
$final_html = '';
foreach( $items as $item )
{
$final_html .= "<li>$title - $description</li>";
}
return $final_html;

How to use the $content variable within a module in Drupal 7, Ubercart 3

I'm running the latest versions of Drupal 7 & Ubercart 3. I'm trying to capture date from the $content variable for use within a module. Specifically I am trying to capture data from a custom product field and display that data inline certain product attributes/options.
The point of this is to create a custom description for each attribute for each product.
It seems the $content variable is not available from uc_attribute.module. Using $content['field_original_size']; returns: undefined variable content . If I use the render function I am returned no errors nor data. Here is what I have so far:
function theme_uc_attribute_option($variables) {
$original_size = render($content['field_original_size']);
if($variables['option'] == 'Original'){
$output = $variables['option'];
$output .= ', ' . $original_size;
if ($variables['price']) {
$output .= ', ' . $variables['price'];
}
}
else{
$output = $variables['option'];
if ($variables['price']) {
$output .= ', ' . $variables['price'];
}
}
return $output;
}
It seems that th easiest way to do this, may be with the token_replace() function, so heres what I am trying now but does not work. There are no errors, but the token does not get replaced.
function theme_uc_attribute_option($variables) {
if($variables['option'] == 'Original'){
$output = $variables['option'];
if ($variables['price']) {
$output .= ', ' . '[node:field-medium]';
$output .= ', ' . $variables['price'];
token_replace($output);
}
}
else{
$output = $variables['option'];
if ($variables['price']) {
$output .= ', ' . $variables['price'];
}
}
return $output;
}
You could print out the $variables, and see if what you need is somewhere in there.
I think it's because you didn't declare your function using a reference to $variables. I believe it should be called as function theme_uc_attribute_option(&$variables) {}. That tells PHP to send a reference to the variable instead of the value of the variable. When the value is sent, any changes to it are local only. If a reference is used, the changes make their way back to the variable. See the PHP Manual for details. I just noticed how old this is.

Resources