Trying to use a Get_user_meta variable breaks my site - wordpress

I am having a weird problem. Anytime I try to use a variable I get from the WordPress function get_user_meta, it breaks the site. It is weird because every other variable works just fine and is coded exactly the same. Wether they are empty or not.
$is_optionalcopd_completed = get_user_meta( $user_id, 'is_optionalcopd_completed' );
$is_optionalasthma_completed = get_user_meta( $user_id, 'is_optionalasthma_completed' );
the bottom one works just fine, but if i even try to echo out $is_optionalcopd_completed, i just get a white screen
Any ideas what the problem may be?

Do you get any php errors (WP_DEBUG enabled)?
Try to use var_dump() instead of echo, because get_user_meta() returns default a array.
Like the notes state: make sure the meta value exists https://codex.wordpress.org/Function_Reference/get_user_meta#Notes.

Even if the user meta doesn't exists, it will not returns a blank screen, it returns an empty string.
Does your code use it after you want to echo it ?
I suggest you to verify if there is no typo error in the code.
Do you see this user meta with a var_dump(get_user_meta($user_id)); ?
That's only hints, waiting for more element to help ;-)

Related

What is different between post_navigation() and get_the_post_navigation();?

What is difference between these WordPress functions, and how to implement it?
the_post_navigation(); get_the_post_navigation();
the_archive_title(); get_the_archive_title();
the_post_pagination(); get_the_post_navigation();
the_archive_description(); get_the_archive_description();
I already googled for this, but I'm still not getting it right.
All the functions that starts with get_ are only returning the "result" of the function : If you put this function in a php page and watch this page in a browser, nothing will be displayed.
If you want the result to be displayed, you have to add echo before the function and this is exactly what the function that starts with the_ are doing.
You may ask yourself why sometimes we want only the result to be returned by the function and not displayed. It's because sometime, we have to do some additional operations with the result before displaying it.
Example:
$content = get_the_content();
$content = str_replace('Hello', 'Bye', $content);
echo $content;
If not operation are needed so you only need to do:
the_content();
You also ask "How to implemenent it ?". To implement the function, you will have to add it in some specific php files. For example, for the get_the_post_navigation() function you will have to add it in the single.php file in your theme folder. You will need basics on php.

Woocommerce Display Category Image - get_woocommerce_term_meta deprecated

I'm in need of some help.
I want to display the category image on the current category page, and I have googled this, and each answer I find uses the same code.
They all use get_woocommerce_term_meta to retrieve the ID of the thumbnail used so that you can then use wp_get_attachment_url to get the image address.
All sounds great, but whenever I try this code it returns nothing, and I think that it is because get_woocommerce_term_meta is deprecated.
Does anyone know of a way round this, so I can get the image address when I have the category ID?
This is the code that I have in place:
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta($cat->term_id, 'thumbnail_id', true);
echo $cat->term_id."<br />";
echo $thumbnail_id;
$image_src = wp_get_attachment_url($thumbnail_id);
$cat->term_id returns the correct ID of the category, but $thumbnail_id returns 0.
The code is in header.php.
get_woocommerce_term_meta() may be deprecated however it hasn't yet been removed. The issue is elsewhere in your setup.
With that said we can resolve the deprecation issue quite easily. Simply replace usages of get_woocommerce_term_meta() with the new native WordPress function get_term_meta().
get_woocommerce_term_meta() will simply pass its arguments on to the new function anyway so we can be sure the problem isn't with the deprecated function.
Likely causes of the issue:
Key used to save the image isn't the same as the key being used to retrieve
Not passing in correct term ID
No image set

WP Post Meta Tags not working as expected

I have been trying to associate CUSTOM data to each of my WP POSTS using the following piece of code :
if($condition === true){
if ( ! update_post_meta ($post_id, '_someData', $someVariable) ) {
add_post_meta($post_id, '_someData', $someVariable);
}
}
However, seems like the META VALUE is RESET to default i.e. Zero OR Blank. Our WordPress website has around 40 plugins, and I think one of these WordPress plugins, is coming in my way of doing things. All of my logic works fine, on a demo WordPres website. Is there a way for me to have total control to set the META Value for a given POST ? Also, is there a way where I can be notified that the META Key is about to change and then I can decide whether OR not to change the Meta Value ?
Any pointers or reference URL's can be of great help. Thanks !
You only need to call update_post_meta in either scenario. Calling add_post_meta() is not necessary and could be causing this problem.
From the codex:
The function update_post_meta() updates the value of an existing meta key (custom field) for the specified post.
This may be used in place of add_post_meta() function. The first thing this function will do is make sure that $meta_key already exists on $post_id. If it does not, add_post_meta($post_id, $meta_key, $meta_value) is called instead and its result is returned.

Passing arguments to WordPress feed URL

As we all know the WordPress' feed url is www.mysite.com/feed.
I have edited the feed-rss2.php file to show thumbnails if a certain GET parameter is passed. See the code below:
<?php if($_GET['c'] == 'detailswiththumb') echo the_post_thumbnail( array(100,100) ); ?>
But when I open the feed address like this:
www.mysite.com/feed?c=detailswiththumb
The code doesn't work. Can the arguments be passed this way? Am I missing something? Please help.
Firstly, the function is get_the_post_thumbnail() not the_post_thumbnail().
Then, their is one more problem in your code, that you have to pass the post id to get its thumbnail (for more info see http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail).
So, first you will have to extract the post id from somewhere and then only you would be able to get the thumbnail. But, I think it would be very tough, so try giving up this thought, for it would take you a lot of time and no living being is going to access that path.
There were browser cache issues. Even with Google Chrome's incognito window. Had to test it with passing fake arguments like...
www.mysite.com/feed?c=detailswiththumb&fakearguments=123
...to clear the cache. And the code is fine.
Sorry for wasting your time guys.

wordpress plugins - handling variables

I wrote a simple twitter connect plugin which needs to display the 'logged in' user's name in the header after he logs in. The function is working properly but I am unable to display the $user variable in my header or anywhere outside the function even though it is assigned global.
Here is the end of the login function:
$user= $Twitter->get_accountVerify_credentials();
print_r($user);
// show screen name (not real name)
$twitter_user = $user->screen_name;
// show profile image url
$twitter_image = $user->profile_image_url;
I can see that it is successful because the $user gets printed, but when I call it in my header.php file the same way I can an error: Notice: Undefined variable: user
Any suggestions?
As opposed to getting wrapped up in the scope of the variables, I'd write a function to get this information. Something like:
function get_twitter_user_name(){
$user= $Twitter->get_accountVerify_credentials();
return $user->screen_name;
}
Then, in the header, where I want to display the name, I'd call the function like so:
<?php echo get_twitter_user_name(); ?>
Depending on the structure of your code, this may look a little different than what I have here, but hopefully this will give you another way of tackling this problem.

Resources