having issues with this simple code trying to get a value - wordpress

am just confused why such a small difference can make a big problem.
here is the working code with just digits entered into it.
$user_info = get_userdata(70);
echo 'Username: ' . $user_info->user_login . "\n";
this code shows username: sarah
and now same but this time with code inside of it
$number = the_author_meta('ID');
echo $number; // echos 70
$user_info = get_userdata($number);
echo 'Username: ' . $user_info->user_login . "\n";
and with the code below it just shows userame: thats it.

MAke sure you read your code. At this line:
$user_info = get_userdata($number);
You miss a r in the variable $number

Here's the problem:
Replace "the_author_meta" with "get_the_author_meta"
the_author_meta just immediately outputs to the screen the information you're trying to retrieve. That's why it outputs '70'.. If you get rid of the echo in front of it, it'd STILL output that 70.
get_the_author_meta actually returns the value and assigns it to a variable, like you're expecting.

$aaaaaa = $author_id=$post->post_author;
$user_info = get_userdata($aaaaaa);
echo 'Username: ' . $user_info->user_login . "\n";
echo 'User roles: ' . implode(', ', $user_info->roles) . "\n";
echo 'User ID: ' . $user_info->ID . "\n";
echo get_user_option( 'rc_banned', $user_info->ID);

Related

How display the spell out of an amount with currency details in WooCommerce

I am trying to spell out WooCommerce order total amount, in my invoice.php template file can reach order total amount.
First I tried:
$total = $order->get_total();
<?php echo ( $total ); ?> -
<?php
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $f->format($total); ?>
The order total displayed is 225.00 and the spell out display is: two hundred twenty-five
Edit:
I found the following solution:
<?php $number = $order->get_total() ;
$formatter = new NumberFormatter('tr', NumberFormatter::SPELLOUT);
$formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, "%financial");
echo $formatter->format($number); ?>
But the result shows like that: two hundred twenty-five
The desired display should be: two hundred twenty-five turkish liras , zero penny.
How can i do this ?
The NumberFormatter SPELLOUT constant doesn't handle the decimals.
You can use the following custom function to display a float number amount spell out like (with the currency details):
function wc_spellout_amount( $amount, $country_code = 'tr' ) {
$formatter = new NumberFormatter($country_code, NumberFormatter::SPELLOUT);
$formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, "%financial");
$amounts = explode('.', (string) $amount); // Separating decimals from amount
$output = $formatter->format($amounts[0]);
$output .= ' ' . _n('turkish lira', 'turkish liras', $amounts[0], 'woocommerce');
$output .= ', ' . $formatter->format($amounts[1]);
$output .= ' ' . _n('penny', 'pennies', ( $amounts[1] > 0 ? $amounts[1] : 1 ), 'woocommerce');
return $output;
}
Code goes in functions.php file of the active child theme (or active theme).
Usage: Then you will use it as follows in your code:
echo wc_spellout_amount( $order->get_total() );
Tested and works.

Wordpress: Stripping Images of <p> tag and adding <div> IF image has no caption

I've seen some variations of this question around, but I've as of yet been unable to tweak them into what I'm looking for.
In a Wordpress post, if an image is uploaded with a caption the image will be output wrapped in a div (with class wp-caption). Perfect.
However, if there is no caption the image is wrapped in a paragraph tag. I'd like to replace this with a div.
So if an image is uploaded without a caption, it is wrapped in a div as well — something like class="wp-nocaption".
My first try was to modify the media.php file. I changed this section:
if ( 1 > (int) $width || empty($caption) )
return $content;
if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
return '<div ' . $id . 'class="wp-caption ' . esc_attr($align)
. '" style="width: ' . (10 + (int) $width) . 'px">'
. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
to this:
if ( 1 > (int) $width || empty($caption) )
return '<div ' . $id . 'class="wp-nocaption ' . esc_attr($align)
. '" style="width: ' . ($width) . 'px">'
. do_shortcode( $content ) . '</div>';
if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
return '<div ' . $id . 'class="wp-caption ' . esc_attr($align)
. '" style="width: ' . ($width) . 'px">'
. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
Which I hoped would do the same thing for a non-captioned image as a captioned one, except the div would have a different class.
But that didn't work. Does anyone have any idea why?
I also found some articles suggesting using a preg_replace function.
http://css-tricks.com/snippets/wordpress/remove-paragraph-tags-from-around-images/
https://wordpress.stackexchange.com/questions/7090/stop-wordpress-wrapping-images-in-a-p-tag
I tweaked their functions, attempting to replace paragraph tags that wrap an image wrapped in a link with div tags.
function filter_ptags_on_images($content) {
return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU',
'/<div class="wp-nocaption">\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/div>/iU',
$content);
}
add_filter('the_content', 'filter_ptags_on_images');
But this doesn't work. I've been reading a lot but still can't wrap my head around the php concept of "backreference". I know the replace section of my function is wrong, but I'm not sure how to fix it.
If you have any suggestions as to what I'm doing wrong on either way would be very much appreciated!
Thank you so much!!

wp_trim_words not working on anything but ordinary strings

I´m a bit out of my field here and I´m confused about this. Using wp_trim_field doesn't work for me except for regular strings.
This does not work, it returns the whole text:
<?php
$field = the_field('project_description');
$trimmedfield = wp_trim_words( $field, $num_words = 1, $more = '… ' );
echo '<p>' . $trimmedfield . '</p>';
?>
This however does work:
<?php
$field = 'this text does get trimmed';
$trimmedfield = wp_trim_words( $field, $num_words = 1, $more = '… ' );
echo '<p>' . $trimmedfield . '</p>';
?>
Echoing out the $field instead does echo out the text that I am trying to trim, but the trimming aint working. Any ideas as to why?
edit - I also tried this, same thing happens:
<?php
$length = 1;
$text = the_field('project_description');
$words = explode(' ', $text);
array_splice($words, $length);
$text = implode(' ', $words);
echo $text;
?>
use var_dump($field); wp_trim_words( $field,....) $field must be string type ...check if this is or not to test the datatype, if its not im sure you know what to do then.
Use typecast if its not.
You'll need to change the $field variable to this: $field = get_field('project_description');
the_field(); outputs the content, while get_field(); retrieves it. In order to pass it through a function, you'll need to retrieve it.
ACF documentation page that answers this question: https://www.advancedcustomfields.com/resources/displaying-custom-field-values-in-your-theme/

Google Analytics API: problems extracting some data

How do I use this API? I can extract some data, but not all of it... I'm a bit lost and I can't find any examples in the documentation. I'm using gapi.class.php.
I have code such as:
$ga = new gapi('user','pwd');
$ga->requestReportData('id',array('browser'),array('pageviews','visits', 'timeOnSite'));
var_dump($ga);
foreach($ga->getResults() as $result)
{
print_r($result);
echo '<strong>'.$result.'</strong><br />';
echo 'Pageviews: ' . $result->getPageviews() . ' ';
echo 'Visits: ' . $result->getVisits() . '<br />';
echo 'Time On site: ' . $result->getTimeOnSite() . '<br />';
}
echo '<p>Total pageviews: ' . $ga->getPageviews() . ' total visits: ' . $ga->getVisits() . '</p>';
The above is working, but then I also want to get other data such as goals... I see this: http://code.google.com/intl/es-ES/apis/analytics/docs/gdata/dimsmets/dimsmets.html
but I'm not really sure how to call each function, or property... I'm really lost, any examples would be appreciated!
GAPI uses magic get methods. You put in the dimensions and metrics that you want and get them using the magic get methods.
For example:
$ga = new gapi('user','pwd');
$ga->requestReportData('id',array('browser'),array('pageviews','visits', 'timeOnSite'));
foreach($ga->getResults() as $result)
{
print_r($result);
echo '<strong>'.$result.'</strong><br />';
echo 'Pageviews: ' . $result->getPageviews() . ' ';
echo 'Visits: ' . $result->getVisits() . '<br />';
echo 'Time On site: ' . $result->getTimeOnSite() . '<br />';
}
echo '<p>Total pageviews: ' . $ga->getPageviews() . ' total visits: ' . $ga->getVisits() . '</p>';
array('browser') is the dimension and array('pageviews','visits', 'timeOnSite') are the metrics. $result->getPageviews() is the magic get method for the pageviews metric.
So, refer to the list and put in the dimensions and metrics you want then return them using the magic get methods of getYourdimension or getYourmetric. Note that get is lowercase and the dimension or metric begins with a capital letter.
See the documentation for more information:
Access metrics and dimensions using magic get methods
With GAPI, when data is returned from Google it is automatically converted into a native PHP object, with an interface to allow the 'get' the value of any dimesion or metric.
For example, if you request the metric 'uniquePageviews' and the dimesion 'pagePath' you can do the following:
foreach($ga->getResults() as $result)
{
echo $result->getUniquePageviews();
echo $result->getPagePath();
}

insert php variable at the end of url string with an iFrame

Here is what i've done so far but i just keep getting an error
<?php echo (($flag_show_product_info_model == 1 and $products_model !='') ? '<li>' . TEXT_PRODUCT_MODEL . $products_model . '</li>' : '') . "\n"; ?>
<iframe src="http://www.madisonb2b.co.uk/stockenquiry.aspx?id=B8FxKDnJ%2bIdaPT1Nw5wo4r87qHuHcCQIPZzeUE%2fI36LIFOM%2bayBi2RSXHzIJS5Hj97JNSyYL80Q%3d&code=<?php {$product_model} ?>"</iframe>
Any ideas?
Thanks,
Scott
<iframe src="http://www.madisonb2b.co.uk/stockenquiry.aspx?id=B8FxKDnJ%2bIdaPT1Nw5wo4r87qHuHcCQIPZzeUE%2fI36LIFOM%2bayBi2RSXHzIJS5Hj97JNSyYL80Q%3d&code=<?php echo $product_model ?>"</iframe>
(will work if $product_model is defined by the time you echo it, of course.
Did you want to say
echo $product_model
instead of
{$product_model}
?

Resources