How can I display author id on the author page - wordpress

I'm trying to display the author id on the author page by using a shortcode I created but it's currently not working because the shortcode is only outputting the administrator id.
This is what I'm returning in the shortcode:
return get_the_author_meta('ID');

Working code:
function author_id_shortcode(){
ob_start();
// We get the author ID outside loop.
global $post;
$author_id = $post->post_author;
// Now get the author ID inside loop.
$author_id = get_the_author_meta( 'ID' );
$output = get_the_author_meta( 'ID', $author_id );
ob_end_clean();
return $output;
}
add_shortcode( 'author_id', 'author_id_shortcode' );
call the shortcode with [author_id]

Please try the below code on author.php
if (is_author()){
$author = get_queried_object();
$author_id = $author->ID;
echo $author_id;
}

Related

How an i get product category name from it's id in woocommerce?

I want to create a shortcode for my wp+woocommerce site, that will show the name of the current products category. I get the category id from my url with get request - this is a specialty. It will look something like:
function this_product_category_name() {
$cat_id = (int) str_replace('-product_cat', '', $_GET['really_curr_tax']);
// here must be a string to get the $cat_name from $cat_id;
echo $cat_name;
}
add_shortcode( 'this_product_category_name', 'this_product_category_name' );
What can be the solution?
Use get_term_by().
function this_product_category_name() {
$cat_id = (int) str_replace('-product_cat', '', $_GET['really_curr_tax']);
$product_cat = get_term_by( 'id', $cat_id, 'product_cat' );
echo $product_cat->name;
}
add_shortcode( 'this_product_category_name', 'this_product_category_name' );
USEFUL LINKS
get_term_by()
Since you're on the product category page, then you could use get_queried_object, and then from that object you could get the name like so:
$cat = get_queried_object();
echo $cat->name;
//or if you want to get its id
echo $cat->term_id;
//if you want to see the object and what's in it then you could use print_r()
print_r($cat);
Let me know if that's what you're looking for.
So your code would be something like this:
function this_product_category_name()
{
$cat = get_queried_object();
echo $cat->name;
}
add_shortcode( 'this_product_category_name', 'this_product_category_name' );

Display custom Field in loop - Woocommerce category

I have nested categories in my website.
I created a custom Field in Woocommerce category and I tried to Add it in category loop. It shows only the term value of the current category page
add_action( 'woocommerce_after_subcategory_title', 'custom_add_cat_Min_price', 12);
function custom_add_cat_Min_price ($category) {
$prix_min_catt = get_term_meta(get_queried_object_id(), 'prix_min_cat', true);
$terms = get_the_terms( $post->ID, 'prix_min_cat' );
foreach ($terms as $term){
echo '<div class="prixminofcatg">'.$prix_min_catt.'</div>';
}
}
I think the problem is the scope of your function. You've passed the $category to your function, but haven't used it. This gives you the ID of your category:
function custom_add_cat_Min_price ($category) {
$category_id = $category->term_id;
and from there, you should be able to extract the custom fields.
Thanks its working without Foreach
add_action( 'woocommerce_after_subcategory_title', 'custom_addd_cat_Min_price', 29);
function custom_addd_cat_Min_price ($category) {
$category_id = $category->term_id;
$prix_min_cag = get_term_meta($category_id, 'prix_min_cat', true);
$terms = get_term( $category_id, 'prix_min_cat' );
echo '<div class="prixminofcatg">'.$prix_min_cag.'</div>';
}

Get customer custom meta on order

I just want to ask how to display custom post meta that I created on customer order detail page on frontend.
Like for example I want to get the value of this meta receive_newsletter on success order detail page on Woocommerce.
Thanks,
JM
You can try this:
$user_id = get_current_user_id();
$key = 'receive_newsletter';
$single = true;
$meta = get_user_meta( $user_id, $key, $single );
echo $meta;
It is depend on where you want to show on order detail page.
For example this hook: do_action( 'woocommerce_order_details_after_order_table', $order );
You can use:
add_action('woocommerce_order_details_after_order_table','wn_display_receive_newsletter');
function wn_display_receive_newsletter($order){
$oder_id = $order->get_id();
$receive_newsletter = get_post_meta('receive_newsletter',$oder_id,true);
echo $receive_newsletter;
}
If you saved "receive_newsletter" meta to user, you can use this
add_action('woocommerce_order_details_after_order_table','wn_display_receive_newsletter');
function wn_display_receive_newsletter($order){
$user_id= $order->get_user_id();
$receive_newsletter = get_user_meta('receive_newsletter',$user_id,true);
echo $receive_newsletter;
}

Redirect to Wordpress based based on custom field

Some of my wordpress posts have a custom field called a QR shortcode. I want to be able to redirect any URL request when someone goes to domain.com/QRCODEVALUE, and have them directed to the post with the corresponding QR code. Is there any way to do this? I don't even know where to start!
I haven't tested this, but the logic should help you get somewhere. Grab the URL request path, remove the trailing slash if it exists, and query posts where the custom field contains QRCODEVALUE:
<?php
function my_redirect() {
$request = parse_url($_SERVER['REQUEST_URI']);
$path = $request["path"];
$path = explode('/', $path);
$length = count($path);
$result = $path[$length-1];
$id;
$query = new WP_Query( array('meta_key' => 'QRCODEVALUE', 'meta_value' => $result) );
if($query->have_posts()): while($query->have_posts()): $query->the_post();
$id = $post->ID;
endwhile; endif;
wp_redirect( get_permalink( $id ) );
exit;
}
add_action('template_redirect', 'my_redirect');
Reference: Get Everything After Domain Name into a string
Is domain.com/QRCODEVALUE a page? If so, you could do something like this:
function my_redirect() {
if ( is_page( 'QRCODEVALUE' ) ) {
// Get the ID of the post with the corresponding QR code here
// Save the post ID to $post_id
wp_redirect( get_permalink( $post_id ) );
exit;
}
}
add_action( 'template_redirect', 'my_redirect' );

Woocommerce: Get current product category

How do I get the current product category that the user is browsing through?
I am trying to use get_the_terms($post->ID, 'product_cat'); but this is giving me the categories for each product listed on the page. I would like to get the current category user is browsing through, the current product listing page.
Here is a one liner:
$wp_query->get_queried_object()->term_id;
or
$wp_query->get_queried_object()->name;
or
...
To get the current category ID.
you have to use
get_queried_object();
The proper way for doing this is..
$cate = get_queried_object();
$cateID = $cate->term_id;
echo $cateID;
try this :
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
$nterms = get_the_terms( $post->ID, 'product_tag' );
foreach ($terms as $term ) {
$product_cat_id = $term->term_id;
$product_cat_name = $term->name;
break;
}
echo $product_cat_name;
By the way, you can create shortcode [show_product_category_id], that will show product's category id. For example, while browsing a category of products, it will show this id. You can open functions.php of the theme and add this:
add_shortcode( 'show_product_category_id', 'show_product_category_id' );
function show_product_category_id() {
$cat = get_queried_object();
$catID = $cat->term_id;
echo $catID;
}

Resources