If user meta is blank echo something? - php

I'm trying to add a custom profile avatar to Wordpress. I have the field added to the profile and it saves everything fine, but I'm trying to make a conditional that if the user does not upload a custom file, it defaults to the gravatar.
I have this so far:
<?php if(get_the_author_meta('da_avatar') != ''): ?>
<p>Exists!</p>
<?php else: ?>
<p>Does not exist!</p>
<?php endif; ?>
da_avatar is the ID of the custom field within the profile. This function always yields "DOES NOT EXIST" even if the field is not blank.
Any idea on why it is not working and how I can make it so that if the custom avatar field is empty, it will display "does not exist" and if it is not empty, have it display "Exists!"

I'm guessing it's because of where you're running this code. Try adding the User ID as the second parameter. I've also updated your code slightly to make it easier to use 'da_avatar' once you've performed the check.
My suggestion would be to do the following:
<?php if ( $da_avatar = get_the_author_meta( 'da_avatar', $user_id ) ) : ?>
<p>Exists!</p>
<?php else : ?>
<p>Does not exist!</p>
<?php endif; ?>
Make sure you replace $user_id with a valid user ID. If that works let me know where you're running this code and I'll talk you through setting it up properly.

Related

Wordpress - Access a custom field value with PHP

I am having trouble accessing a custom field on my Wordpress page.
I have a field for a user to upload an image file. The theme that I am using then displays the value of that image as just the image ID, instead of an image link. I display it using {CUSTOM_FIELD_TestFile} in the page editor.
I want it to display the actual image though, not the ID.
So, I created a PHP snippet that runs on the page, to do the converting.
If I use <img src=" <?php echo wp_get_attachment_url( 1013 )?> "> and I have the image ID hard coded (1013), it works perfectly.
However, I have had no luck getting the value of 'TestFile' into the PHP snippet.
I double checked the user_meta table and the meta_key for this file is indeed TestFile.
The viewing of the page is done usually when a user is not logged in. So, get_current_user_id() isn't an option, which means, I also can't use get_user_meta().
If I use:
<?php
$postId = get_the_ID();
echo $postId;
$fileLink = get_metadata( "user", $postId, "TestFile");
echo $fileLink;
?>
I get "array" as a response. If I change it to get_metadata( "user", $postId, "TestFile", true); I get nothing.
The exact same results if I use get_post_meta($postId, 'TestFile'); or get_post_meta($postId, 'TestFile', true);.
Am I on the completely wrong track? Is there an easier way to do this? Or is there something that I should be doing with that "array" value that is being returned.
Thanks so much for any help you can offer.
If you have added a custom field called TestFile in the editor, you should be able to get that in your template with:
$value = get_post_meta($postId, 'TestFile', true);
I'm not sure what you mean about the user being logged in or out, because custom fields belong to the post and have nothing to do with the user.
Assuming that works, you should be able to add the ID to your image element:
<img src="<?= wp_get_attachment_url($value); ?>">

Show or Hide content user role wise

I'm developing a website in wordpress using Woocommerce and WC Vendor Marketplace plugin. I want to show content on certain page user role wise in wordpress website.
For example.
If i'm login as a vendor then it shows content "A" and if i'm login as a customer then it hides content "A".
Here i'm customize my own code for this but not work.
<?php
$userss =get_user_meta( $vendor_id, 'wp_capabilities', true );
if ($userss='vendor') {
echo'<li id="menuuu">Back To Dashboard';
echo'</li>';
}
?>
<?php endif; ?>
In above code 'wp_capabilities' is for user role in wordpress. In my code, user roles are 'vendor' and 'customer'. I'm really weak in php. So please help me.
In wp_capabilities data store as serilized array of user role instead of this you can use current_user_can for more information you can refer to this.
You can change your code as below:
if (current_user_can('vendor')) {
echo'<li id="menuuu">Back To Dashboard';
echo'</li>';
}
?>
<?php endif; ?>
I know this has been marked answered, however using current_user_can() with roles is unreliable and even says as much in the codex.
While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.
It's better to use our built in methods to determine if the current user is a vendor.
<?php if ( WCV_Vendors::is_vendor( $vendor_id ) ) : ?>
<li id="menuuu">Back To Dashboard</li>
<?php endif; ?>

Store and Echo PHP with Wordpress

I'm using the Advanced Custom Fields plugin for WP which I really like, because it gives me the ability to add a whole bunch of custom meta boxes to individual pages within my site, and implementation within page templates is very straightforward. This is generally how I would use it with a template:
<?php if( get_field('FIELD-NAME-HERE') ): ?>
<h6><?php the_field('FIELD-NAME-HERE'); ?></h6>
<?php endif; ?>
In this case, I have a field for my Contact Form 7 Shortcode, and I've turned formatting off for this field, so it should return exactly what is entered (ie. [contact-form-7 title="Contact Form"] ).
I know that I can use the do_shortcode(); to pull this shortcode into my page template, but I want to be able to populate that do_shortcode(); with the_field(); from above.
I've tried this:
<?php if( get_field('form_shortcode') ): ?>
<?php echo do_shortcode("<?php the_field('form_shortcode'); ?>");?>
<?php endif; ?>
And this:
<?php
var formCode = the_field('form_shortcode');
echo do_shortcode(formCode);
?>
I'm still pretty new to PHP, but I feel like I'm not too far off. Any help would be greatly appreciated!
You're not far off. You need to use get_field() rather than the_field(): the_field() actually echoes out the content, so that's why it's not working as currently using it. It's the equivalent of trying to echo it twice.
Another thing to look out for is that you need to include the square brackets when using do_shortcode() but since your code already includes them you don't need to worry about it in this instance.
<?php if( get_field('form_shortcode') ): ?>
<?php echo do_shortcode( get_field('form_shortcode') );?>
<?php endif; ?>

Adjust Wordpress function to grab author ID of post

I use a point system plugin for Wordpress. By adding this code to the author.php page:
<?php cp_displayPoints($authordata->ID); ?>
It will echo X Points. This is the points of that respective author. When I add the same code to single.php (post page), it echos the logged in user's points, and if not logged in, it returns blank.
How can I alter this code so that it will function properly on the single.php page too? This would mean that it would echo the points of the author of that post.
Just call get_the_author_meta from within the loop.
So, you just need to test if you have a currently signed in user, if not use the post author instead. Something like this.
<?php
if(!$authordata->ID)
cp_displayPoints(get_the_author_meta('ID'));
else
cp_displayPoints($authordata->ID);
?>
EDIT:
To display only the post author's ID, just use
<?php cp_displayPoints(get_the_author_meta('ID')); ?>

Checking if Custom Fields exist in Wordpress

This is my first question on Stack Overflow so bear with me.
I have a WordPress site with the Advanced Custom Fields plugin installed. The site has a custom post type that has various ACF custom fields attached to it.
Not all of the custom fields are required, also some of the custom fields are grouped into their own tabular structure.
I need to check if either of 2 custom fields have content, and if they do display the table.
My PHP is fairly limited but I did some research and it should work, but its not.
The code is as follows:
<?php if (get_field( 'meeting_documents_agendas' && 'meeting_documents_minutes')) : ?>
<?php if ( get_field( 'meeting_documents_agendas' ) ) : ?>
Download file
<? endif ?>
<?php if ( get_field( 'meeting_documents_minutes' ) ) : ?>
Download file
<? endif ?>
<? endif ?>
Basically nothing is displaying, even if I have content in those custom fields.
Is the code right? Could it be a WordPress bug?
Try
<? if (get_field('meeting_documents_agendas') || get_field('meeting_documents_minutes')): ?>
This means: if one or both of the fields are set, display the table.
Don't use && because this means that both fields must be set. Use get_field() twice because you want to logically link the results of get_fields(), not the parameters themselves.
This was helpful for me as well. Though my issue was a bit different since I was using sub_fields. I needed the code to check if both sub_fields had data.
Here's my code.
<?php if (get_sub_field( 'additional_link_url' ) && get_sub_field('additional_link_text') ) : ?>
<br /><a target="_blank" href="<?php echo get_sub_field('additional_link_url')['url']; ?>"><?php echo get_sub_field('additional_link_text'); ?></a>
<?php endif; ?>

Categories