please advise how can I output the values of user meta stored in wp database using php code

please advise how can I output the values of user meta stored in wp database using php code

  • Adam Czajczyk

    Hello Jimmy,

    I hope you're well today and thank you for your question!

    Fortunately, there's a template tag (template functions) already that you can use for that. Take a look here please:

    https://codex.wordpress.org/Function_Reference/get_user_meta

    There's also an example code in that article that you can adjust to your needs and use on your site.

    If you need any further assistance with that or have additional questions, let me know please and I'll be happy to assist.

    Best regards,
    Adam

  • Jimmy
    <?php
    /**
     * Single Post
     *
     * Learn more: http://codex.wordpress.org/Template_Hierarchy
     *
     * @package Jobify
     * @since Jobify 1.0
     */
    
    get_header(); ?>
    
    <body>
    <div id="mainwrapper">
      <header>
        <!--**************************************************************************
        Header starts here. It contains Logo and 3 navigation links.
        ****************************************************************************-->
        <div id="logo">
    											<?php the_company_logo( 'medium' ); ?>
    
    </div>
        <nav><?php printf( __( '%s', 'jobify' ), get_the_company_name() ); ?>
    
    </nav>
    
      </header>
    <form class="job-manager-application-form job-manager-form" method="post" enctype="multipart/form-data"  action="<?php echo esc_url( get_permalink() );?>">
    
    	<?php do_action( 'job_application_form_fields_start' ); ?>
    
    	<?php foreach ( $application_fields as $key => $field ) : ?>
    		<?php if ( 'output-content' === $field['type'] ) : ?>
    			<div class="form-content">
    
    				<?php if ( ! empty( $field['description'] ) ) : ?><?php echo wpautop( wp_kses_post( $field['description'] ) ); ?><?php endif; ?>
    			</div>
    		<?php else : ?>
    			<fieldset class="fieldset-<?php esc_attr_e( $key ); ?>">
    				<label for="<?php esc_attr_e( $key ); ?>"><?php echo __( $field['label'] ) . apply_filters( 'submit_job_form_required_label', $field['required'] ? '' : ' <small>' . __( '(optional)', 'wp-job-manager' ) . '</small>', $field ); ?></label>
    							</fieldset>
    <div class="field " <?php echo $field['required'] ? 'required-field' : ''; ?>>
    					<?php $class->get_field_template( $key, $field ); ?>
    				</div>
    
    		<?php endif; ?>
    	<?php endforeach; ?>
    
    	<?php do_action( 'job_application_form_fields_end' ); ?>
    
    			<input type="submit" class="button wp_job_manager_send_application_button" value="<?php esc_attr_e( 'Apply Now', 'wp-job-manager-applications' ); ?>" />
    		<input type="hidden" name="wp_job_manager_send_application" value="1" />
    		<input type="hidden" name="job_id" value="<?php echo absint( $post->ID ); ?>" />
    	</p>
    </form>
    
      <div id="content">
        <div class="notOnDesktop">
          <!-- This search box is displayed only in mobile and tablet laouts and not in desktop layouts -->
          <input type="text" placeholder="Search">
        </div>
    
        <section id="mainContent"> <div id="desc">
          <!--************************************************************************
        Main Blog content starts here
        ****************************************************************************-->
          <h1><?php printf( __( 'About %s', 'jobify' ), get_the_company_name() ); ?></h1></div>
          <h3> <? the_company_field('company_description')?> </h3>
          <div id="bannerImage"><?php echo jobify_get_the_featured_image(); ?>
    
    </div>
      <div id="employ">
    <aside id="authorInfo">
     <h3><b>Employment Details</b></h3>
    
                        </aside>
    
          <footer>
          <!--************************************************************************
        Footer starts here
        ****************************************************************************-->
          <article>
     <b>Available Positions:</b><?php
    $usermeta = get_user_meta($current_user_id, 'company_name', true);?>
    
    <?php
    // Use shortcodes in form like Landing Page Template.
    echo do_shortcode( '[pp-tabs field=apt]' );
    ?>
    
    <?php get_user_meta('zip'); ?>
    
    <? the_company_field( 'equipment_type_hired');?>
    
    	<?php echo do_shortcode( '[pp-tabs field=company_name label=1]' );?>
    
    <?php echo do_shortcode("[user_email]");?>
    <?php
    // Use shortcodes in form like Landing Page Template.
    
    echo do_shortcode( '[pp-tabs field="logo"]' );
    php?>
    <?php
    $display_name=um_user('display_name');
     echo $display_name;
     ?><hr color="white">
    <b>Type of Employment Offered:</b> <? the_custom_field('employment_offered')?><hr color="white">
    <b>Required Endorsements</b><? the_custom_field('required_endorsements')?><hr color="white">
    <b>Min. age of driver :</b><? the_company_field('driver_age')?><hr color="white">
    <b> Equipment Experience Accepted :  </b><? the_custom_field('equipment_experience_accepted')?><hr color="white">
    <b> Min. experience required (months):  </b><? the_custom_field('driver_experience')?>
    <hr color="white">
    <b> CDL Class type :  </b><? the_custom_field('cdl_class_type')?><hr color="white">
    
     </article>
          <article  <b> Will you hire a driver who has had their license suspended or revoked? :  </b>  <? the_custom_field('license_revoked')?>
    <hr color="white">
    <b> Will you hire a driver who has received a DWI or DUI? :  </b><? the_custom_field('dui')?>
    <hr color="white">
    <b> Will you hire a driver who has been convicted of a felony? : </b><? the_custom_field('convicted')?>
    <hr color="white">
    <b> Paid orientation :  </b><? the_custom_field('paid_orientation')?>
    <hr color="white">
    <b> Spouse rider program :  </b><? the_custom_field('spouse_rider_program')?>
    <hr color="white">
    <b> Child rider program :  </b><? the_custom_field('child_rider_program')?>
    <hr color="white">
    <b> Pets:  </b><? the_custom_field('pets')?>
    <hr color="white">
    
     </article>
    <div id="footerbar"><!-- Small footerbar at the bottom --></div>
    
        </footer>
    </div>
    <footer>
          <!--************************************************************************
        Footer starts here
        ****************************************************************************-->
          <article>
    
            <h3>Pay Information for Company Drivers</h3>
    
     </article>
          <article>
    
            <h3>Pay Information for Owner Operators</h3>
    
     </article>
    
        </footer>
    
        </section>
    
        <section id="sidebar"> <div id="address">
          <!--************************************************************************
        Sidebar starts here. It contains a searchbox, sample ad image and 6 links
        ****************************************************************************-->
    <p><? the_custom_field('street')?></p>
    <? $street=um_user($street);?>
    <p><? the_custom_field('state')?></p>
    <p><? the_custom_field('apt')?> </p>
    <p><? the_custom_field('city')?></p>
    <p><? the_custom_field('state')?></p>
    <p><? the_custom_field('zip')?></p>
    <p><? the_custom_field('user_email')?></p>
    <p><? the_custom_field('office_number')?></p>
    <p><? the_custom_field('fax')?></p>
    <p><? the_widget( 'Jobify_Widget_Job_Company_Social',$args );?></p>
    </div>
    
          <div id="adimage">
    
    <img src="BlogPostAssets/images/300x250.gif" alt=""/></div>
          <nav>
            <ul>
              <li><a href="#" title="Link">Link1</a></li>
              <li><a href="#" title="Link">Link2</a></li>
              <li><a href="#" title="Link">Link3</a></li>
              <li><a href="#" title="Link">Link4</a></li>
              <li><a href="#" title="Link">Link5</a></li>
              <li><a href="#" title="Link">Link6</a></li>
     <li><a href="#" title="Link">Link1</a></li>
              <li><a href="#" title="Link">Link2</a></li>
              <li><a href="#" title="Link">Link3</a></li>
              <li><a href="#" title="Link">Link4</a></li>
              <li><a href="#" title="Link">Link5</a></li>
              <li><a href="#" title="Link">Link6</a></li>
    
            </ul>
          </nav>
        </section>
         </div>
    
    </div>
     <footer>
          <!--************************************************************************
        Footer starts here
        ****************************************************************************-->
          <article>
            <h3>Footer1</h3>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
          </article>
          <article>
            <h3>Footer2</h3>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
          </article>
    
        </footer>
      </div>
      <div id="footerbar"><!-- Small footerbar at the bottom --></div>
    </div>
    
    </body>
    </html>
    
    	<?php the_post(); ?>">
    	<header class="page-header">
    <h1 class="page-title"><?php printf( __( 'Jobs at %s', 'jobify' ), esc_attr( urldecode( get_query_var( apply_filters( 'wp_job_manager_companies_company_slug', 'company' ) ) ) ) ); ?></h1>
    
    		<h2 class="page-subtitle"><?php printf( _n( '%d Job Available', '%d Jobs Available', $wp_query->found_posts, 'jobify' ), $wp_query->found_posts ); ?> </h2>
    	</header>
    	<?php rewind_posts(); ?>
    
    	<div id="primary" class="content-area">
    		<div id="content" class="container" role="main">
    
    			<div class="company-profile row">
    				<?php if ( jobify_get_the_company_description() ) : ?>
    				<div itemscope itemtype="http://data-vocabulary.org/Organization" class="col-xs-12 job-company-about">
    					<h2 class="widget-title widget-title--job_listing-top" itemprop="name"><?php printf( __( 'About %s', 'jobify' ), get_the_company_name() ); ?></h2>
    
    					<?php jobify_the_company_description(); ?>
    				</div>
    				<?php endif; ?>
    
    				<div class="company-profile-jobs col-md-10 col-sm-8 col-xs-12">
    					<?php if ( have_posts() ) : ?>
    					<div class="job_listings">
    						<ul class="job_listings">
    							<?php while ( have_posts() ) : the_post(); ?>
    								<?php get_job_manager_template_part( 'content', 'job_listing' ); ?>
    							<?php endwhile; ?>
    						</ul>
    					</div>
    					<?php else : ?>
    						<?php get_template_part( 'content', 'none' ); ?>
    					<?php endif; ?>
    				</div>
    
    				<div class="company-profile-info job-meta col-md-2 col-sm-4 col-xs-4">
    
    					<article class="widget widget--job_listing">
    						<?php the_company_logo( 'fullsize' ); ?>
    					</article>
    
    					<article class="widget widget--job_listing">
    
    						<h3 class="widget-title widget-title--job_listing"><?php _e( 'Company Details', 'jobify' ); ?></h3>
    
    						<ul class="job_listing-company-social company-social">
    							<?php do_action( 'job_listing_company_social_before' ); ?>
    
    							<?php if ( get_the_company_website() ) : ?>
    							<li><a href="<?php echo esc_url( get_the_company_website() ); ?>" target="_blank" itemprop="url" class="job_listing-website">
    								<?php _e( 'Website', 'jobify' ); ?>
    							</a></li>
    							<?php endif; ?>
    
    							<?php if ( get_the_company_twitter() ) : ?>
    							<li><a href="<?php echo esc_url( jobify_get_the_company_twitter() ); ?>" target="_blank" class="job_listing-twitter">
    								<?php _e( 'Twitter', 'jobify' ); ?>
    							</a></li>
    							<?php endif; ?>
    
    							<?php if ( jobify_get_the_company_facebook() ) : ?>
    							<li><a href="<?php echo esc_url( jobify_get_the_company_facebook() ); ?>" target="_blank" class="job_listing-facebook">
    								<?php _e( 'Facebook', 'jobify' ); ?>
    							</a></li>
    							<?php endif; ?>
    
    							<?php if ( jobify_get_the_company_gplus() ) : ?>
    							<li><a href="<?php echo esc_url( jobify_get_the_company_gplus() ); ?>" target="_blank" class="job_listing-googleplus">
    								<?php _e( 'Google+', 'jobify' ); ?>
    							</a></li>
    							<?php endif; ?>
    
    							<?php if ( jobify_get_the_company_linkedin() ) : ?>
    							<li><a href="<?php echo esc_url( jobify_get_the_company_linkedin() ); ?>" target="_blank" class="job_listing-linkedin">
    								<?php _e( 'LinkedIn', 'jobify' ); ?>
    							</a></li>
    							<?php endif; ?>
    
    							<?php do_action( 'job_listing_company_social_after' ); ?>
    						</ul>
    					</article>
    
    				</div>
    
    			</div>
    		</div><!-- #content -->
    
    		<?php do_action( 'jobify_loop_after' ); ?>
    
    	</div><!-- #primary -->
    
    <?php get_footer(); ?>
  • Adam Czajczyk

    Hello Jimmy!

    Thank you for posting template code. Could you please also explain what "the issue" is? Initially you asked about how to get/output values of user meta fields and I see that you are using the function in your template.

    Since the template seems to belong to the premium theme that I don't own and also uses a lot of custom data/functions, I'm not able to test it on my end so it would be great if you could share some more details on what's happening here :slight_smile:

    Granting a support access to the site in addition could also be helpful, so I'd appreciate that very much. Here's a guide on enabling support access:

    https://premium.wpmudev.org/manuals/wpmu-dev-dashboard-enabling-staff-login/

    Best regards,
    Adam

  • Jimmy

    Its for wp job manager company profile
    I want to show the people who sign up form their value is stored in meta value, they all gets a page so i want these values to show on their page visible to everyone
    How can i do that?
    this pages are run by single-company.php
    What code i enter there
    I want meta value from meta key to diaplayed in their profile pages, what code i enter in that php file

  • Adam Czajczyk

    Hello Jimmy,

    Thanks for this additional explanation.

    I installed both plugins and enabled Twenty Fifteen theme but so far I couldn't get it to work properly. I'll work on it more though.

    I did however took a look at your site and at the code again. Some issues that I think of now are these:

    1. This part of code in your template file:

    <b>Available Positions:</b><?php
    $usermeta = get_user_meta($current_user_id, 'company_name', true);?>

    That is used when you enter a company page (as the template is called out for company profile).

    It does not display anything because the "get_user_meta()" output is returned to the variable ($usermeta) so in order to display the value of a "company_name" meta that should rather be

    <b>Available Positions:</b><?php
    $usermeta = get_user_meta($current_user_id, 'company_name', true);
    echo $usermeta; ?>

    That will work only if a currently logged in user that is viewing that page will have a "company_name" field filled in in his profile.

    2. This part of the code:

    <?php get_user_meta('zip'); ?>

    It's not displayed to the screen either and also it doesn't include user ID which is a required field. The better use of that would be like this:

    <?php
    $user_zip = get_user_meta($current_user_id, 'zip', true);
    echo $user_zip;
    ?>

    3. You are using "$current_user_id" variable but it doesn't seem to be initiated anywhere with a user ID, for example right after "get_header(); ?>":

    <?php $current_user_id = get_current_user_id(); ?>

    I think these are the three most important aspects to address here first and then see what happens. Could you give it a try and let me know if/what that caused? Meanwhile I'll try to make both plugins work as expected on my end and give it another try there.

    Best regards,
    Adam

  • Jimmy

    http://protruckdriverjobs.com/companies/ shows list of companies, when you click on any company

    single-company.php open page like this one http://protruckdriverjobs.com/company/nnnjjkbjb/

    here as you see every title has certain value , this value belong to only this company
    this value is different if you click any other company, everyone including non logged in user can see this value

    the code you gave shows value of only logged inn user himself
    I want to display values of each company taken from their meta key seen by all
    how can I do that

  • Adam Czajczyk

    Hello Jimmy!

    Thanks for explanation!

    In my previous post I only corrected the code regarding "get_user_meta()" field and I also suggested using this function because you initially asked about getting the value of user meta.

    The company data however is stored in a different table. A job listing is a custom post and the associated company data are post meta fields, not user meta fields. That's why this function will not work here. I'm sorry I didn't state that clearly right from the beginning but up until now I wasn't quite sure what are you wishing to achieve.

    Therefore we need to use the "get_post_meta();" function here instead. The usage is pretty much the same here except that ID is a post ID of a current post:

    $my_value = get_post_meta($post->ID, 'meta_key', $single)

    where $single may be true (to return single, string value) or false (to return array if available).

    This is the first part of the issue. The second one though is that you got there some additional fields. I checked your site and they were created with WP Job Manager Field Editor plugin which is a premium plugin and I don't have access to it to review its code. From what I can see in your site, it does also use the same structure though.

    If you go to "Job Listings -> Company Fields" page in your site's dashboard and hover over the field, a popup will appear. Inside these popups you will find a "_meta_key" and/or "populate_meta_key" names. This is what you would like to fetch from the database in relation to the ID of current post (not current user).

    That being said, I think the "get_post_meta()" or similar core function should work for that but there's most likely some "twist" with "meta_key" name values and since I do not have access to the code of WP Job Manager Field Editor plugin I'm not able to provide you with a working code for the template.

    If you are familiar with using phpMyAdmin you could review the database against existing data (for an example company) to find out how the "meta_key" for _postmeta table in this case are populated. I could also take a look at that but I would need a direct access to your site (including FTP to review WP Job Manager Field Editor code and phpMyAdmin to check data in database) to find out how to populate these fields.

    You can provide my with necessary credentials by sending in:

    Subject: "Attn: Adam Czajczyk"

    - Mark to my attention, the subject line should contain only: ATTN: Adam Czajczyk
    - Do not include anything else in the subject line, doing so may delay our response due to how email filtering works.
    - Link back to this thread
    - login URL and admin account login credentials (may be a temporary admin account) data
    - Include FTP log-in details (hostname, username & password)
    - Include hosting control panel access details (login address, username & password) - cPanel's usually the control panel used for this, but your provider may use something else; I'll need this for accessing your site's database, preferably via phpMyAdmin
    - Include any relevant URLs for your site

    Please use our contact form here http://premium.wpmudev.org/contact/

    Select "I have a different question" from the drop-down list. 

    Please be sure to make a full backup of your entire site first!

    Kind regards,
    Adam

  • Adam Czajczyk

    Hello Jimmy!

    That changed the point quite a lot. The Ultimate Member plugin includes and "um_user()" function to retrieve any user profile data and this is what would let you fetch data for employers profiles. You only need to provide data field name (similar to get_usermeta(); ) like and "echo" it like this:

    <?php
    echo um_user('zip');
    ?>

    The field name to use: when you edit your form and click on "edit" for a field there's a "Meta Key" value shown and this is the one.

    Before calling this function you should however first call "um_fetch_user();" function so a code would be like this:

    <?php
    
    $user_id = X; // where X is your user (employer in this case) ID
    
    um_featch_user($user_id);
    echo um_user('zip');
    
    ?>

    This is documented here:
    http://docs.ultimatemember.com/article/158-umuser

    and here's more developer docs for Ultimate Member:

    http://docs.ultimatemember.com/
    k

    In case you still had troubles handling that data, it may be worth to ask on Ultimate Members community forum as there may be more developers well familiar with that plugin there. You will find these forums here:

    https://wordpress.org/support/plugin/ultimate-member

    https://ultimatemember.com/support/

    Kind regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.