Making Wordpress user metadata available to Rest-API - wordpress

I am building an Ionic app that ties into my Wordpress website Rest API. While I have figured out how to populate post and page data, I am having a very difficult time populating user data.
Specifically, I am trying to access user data stored as usermeta by a third party plugin called UPME (User Profiles Made Easy). It is stored as user metadata in the database.
Here is an image of what I envision pulling from the API into the app.
For example, user_id: 43 meta_key: linkedin within the database.
Can someone please give me an example of what I can do in my functions.php to be able to populate every user with {...} meta_keys into the API for outside things to access?
EDIT: This is my understanding of what it should do
I need some function within Wordpress that makes the following available to my Rest-API:
• Get list of all users
• For all users {
• If user upme_user_profile_status: ACTIVE
• Get the following usermeta: [
first_name
last_name
company_name
company_title
user_email
business_phone
facebook
twitter
linkedin
]
}
This would be publicly accessible within the API.

I think this should help you. As well as WP user query for reference.
Example to display all subscribers in an unordered list.
<?php
$blogusers = get_users( 'blog_id=1&orderby=nicename&role=subscriber' );
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
echo '<span>' . esc_html( $user->user_email ) . '</span>';
}
Example of querying by a specific field.
<?php
$blogusers = get_users( array( 'fields' => array( 'display_name' ) ) );
// Array of stdClass objects.
foreach ( $blogusers as $user ) {
echo '<span>' . esc_html( $user->display_name ) . '</span>';
}

Related

Woocommerce Product Vendor extension - Loading ACF fields into a vendor's store front

I am using ACF to add fields to my vendors' dashboard profile pages. I currently have a test ACF field loading the field from only the WP Admin profile page on all the vendors' product listing page using this simple hook in my child theme's functions.php:
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() { ?>
<?php if(get_field('founded_on')) { ?>
<?php the_field('founded_on'); ?>
<?php }
}
Perhaps I'm pulling the wrong hook, but I can't seem to find the right hook in the Product Vendor frontend page.
I need help customizing it so that when you are on a vendor's product page, it pulls the ACF fields from that particular vendor's profile. Currently, it partially works; however it only pulls the WP main admin's data for all the different vendors'.
I know there is a way to pull the vendor's ID for each particular vendor's page and have it load their data for their page, but my php knowledge is very limited. I usually just hunt for existing code and tweak it. Unfortunately I haven't found any solutions that have worked, and this is the closest I've come to getting custom fields to work on a vendor's page.
Or if anyone can point me to a better solution to allow me to create customer fields for a vendor to fill out that will be loaded on their front end page, that would be great. I've tried Nicola Mustone's solution ( here ), which would have been perfect, except I couldn't get it to load the new custom fields on the vendor's store profile form page, nor have it load the fields into that vendor's storefront page. Based on comments, it only shows up for the site's Admin and only they can edit it. There's no visible way to have it load on the storefront, which defeats the purpose.
I imagine that the providers are users with a certain level within the WordPress system?
Considering that this is your case, the ACF fields need some additional parameters to become visible:
$post_id = false; // current post
$post_id = 1; // post ID = 1
$post_id = "user_2"; // user ID = 2
$post_id = "category_3"; // category term ID = 3
$post_id = "event_4"; // event (custom taxonomy) term ID = 4
$post_id = "option"; // options page
$post_id = "options"; // same as above
$value1 = the_field( 'my_field', $post_id );
$value2 = get_field( 'my_field', $post_id );
take some examples found in the ACF documentation, but in your particular case you have to pass the user's ID
the_field('founded_on', 'user_' . $user->ID );
echo get_field('founded_on', 'user_' . $user->ID );
function documentation the_field()
You need to pull the user ID of the user and then use the format user_{$user->ID} for the post ID as the second parameter of the ACF field.
If I understand your question, then this should work.
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() {
$user = wp_get_current_user();
if ( get_field( 'founded_on', 'user_' . $user->ID ) ) {
the_field( 'founded_on', 'user_' . $user->ID );
}
}

Wordpress create lists of favorites for users

I have to create lists of favorites products in woocommerce.
First I've created a new custom post type to manage my products list.
Now I need to add my products list to my custom type but I don't know how to do this.
The final result will be a page listing all my lists for a user contains all added products by user.
Can anyone help ?
I found a pretty comprehensive (paid) plugin by WooCommerce that does exactly this: WooCommerce Wishlists. I haven't tried it.
WooCommerce Wishlists allows guests and customers to create and add products to an unlimited number of Wishlists. From birthdays to weddings and everything in between, WooCommerce Wishlists is a welcome addition to any WooCommerce store.
Plugin link: https://woocommerce.com/products/woocommerce-wishlists/
Thanks for your answer but i've needed to make my own plugin.
WC Wishlist allow you to make only one list ;)
After some works i've found a solution with meta_user_data to manage my lists.
public function action_create_favorite_list(){
if( ! is_user_logged_in() ) :
return;
endif;
$list = new stdClass();
$list->name = $_POST["list_name"];
$list->items = array();
$fav = get_user_meta( get_current_user_id(), 'user_'.get_current_user_id().'_favlist', true);
if(empty($fav))
{
add_user_meta( get_current_user_id(), 'user_'.get_current_user_id().'_favlist', array());
$fav = get_user_meta( get_current_user_id(), 'user_'.get_current_user_id().'_favlist', true);
}
array_push($fav,$list);
update_user_meta( get_current_user_id(), 'user_'.get_current_user_id().'_favlist', $fav);
wp_send_json($fav);
wp_die();
}

WORDPRESS User specific access via ID to specific contact forms CFDB7

I'm trying to implement in the code a way to show specific contact forms data to a particular user via ID but I'm having difficulty finding which part of the code to add it to.
$user_ID = get_current_user_id();
if ( $user_ID == ('2') ) {
I'm currently inside the admin-mainpage.php within the plugin files.
It's not as simple as I thought as it doesn't store the form names via db.
Any help would be appreciated.
Forms-list
It looks like Contact Forms 7 is your plugin for the forms portion while CFDB7 is an accompanying plugin that writes the form submissions to the WP database.
Get the Form ID
There are a couple ways to get the form ID. It looks like the easiest path is to look at the shortcode that the CF7 creates. You can see it in their documentation here. The code is something like [contact-form-7 id="4" title="Foo"] with id being the form ID (4 in this case).
Show the Right Form to the Right Person
Assuming you know the user ids and the related form ids you want to show them, you could write a very simple shortcode plugin to display the right forms for the right people. It'd be something like this untested code below.
//[user-form-display]
function user_form_display( $atts ){
$user_id = get_current_user_id();
if ($user_id == 2){
echo do_shortcode('[contact-form-7 id="4" title="Foo"]');
} else if ($user_id == 4){
echo do_shortcode('[contact-form-7 id="88" title="Bar"]');
}
}
add_shortcode( 'user-form-display', 'user_form_display' );
You could then put the shortcode in the regular post field and not edit either the CF7 plugin nor mess with the theme files.
You could also make the shortcode fancier and tie user ids to form ids directly in the shortcode arguments. That would take a bit more effort but is probably worth it.
Getting the Form Data
You could modify the $args to include form ids based on an association with a user id or multiple user ids. The form ids should be a field in that table. That's the example indicated below.
Alternately you could modify how the information is returned based on the same relationships by setting up the if/then statements in the $data_value lines. This is easier probably but messier in the long run.
function specfic_table_data()
{
global $wpdb;
$user_id = get_current_user_id();
if($user_id == 1){
$form_ids = array(4,6);//only returns forms with id 4 and 6 when it's user w id 1
}
$cfdb = apply_filters( 'cfdb7_database', $wpdb );
$data = array();
$table_name = $cfdb->prefix.'db7_forms';
$args = array(
'post_type'=> 'wpcf7_contact_form',
'order' => 'ASC',
'posts_per_page' => 10,
'post__in' => $form_ids,
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$form_post_id = get_the_id();
$totalItems = $cfdb->get_var("SELECT COUNT(*) FROM $table_name WHERE form_post_id = $form_post_id");
$title = get_the_title();
$link = "<a class='row-title' href=admin.php?page=cfdb7-list.php&fid=$form_post_id>%s</a>";
$data_value['name'] = sprintf( $link, $title );
$data_value['count'] = sprintf( $link, $totalItems );
$data[] = $data_value;
endwhile;
var_dump($data);
}

How to get all admins of multisite in wordpress

How to get all admins of multisite in wordpress.
I am create custom plugin for this
I am stuck in custom code for get all the admins of multisite in main website.
for ex. my main website is : wyz.com and my second site is : xyz.com/demo.
main website admin is "abc" and for " xyz.com/demo" site admin is "abcde".
Now how i get "abcde" admin in my main website .
In my live site i have currently 6k admins. So i difficult to get this . I am new in multisite wordpress.
To get the data from any of the sub site, you first need to switch to that site using switch_to_blog() function. Then whatever the query you fire, it will give records from that site only. Don't forget to restore it to current site, once you get the data from sub-site. You can restore it using restore_current_blog() function.
To get the all admin users of all the sites, you need to perform the followings:
1) Use wp_get_sites() function to get the blog_id of all the sites.
2) Once you get the blog_id, You need to perform the following loop to get the admin user of each the sites.
Assume that you get $blog as array of blog_ids from wp_get_sites() function
foreach ($blogs as $blog)
{
switch_to_blog( $blog->blog_id ); // blog id which u got from wp_get_sites() function
$users_query = new WP_User_Query( array(
'role' => 'administrator',
'orderby' => 'display_name'
) ); // query to get admin users
$results = $users_query->get_results();
$site_admins .= 'Blog ID: ' . $blog->blog_id . '<pre>' . print_r($results,true) . '</pre>';
}
restore_current_blog();

Extra Profile Field Not Showing In Wordpress User Profile

I have created a an extra profile field on Buddypress registration page for users to select their native language using the profile fields area on Buddypress.
Once the user has registered and made this selection I want it their input to show on user-edit.php in wordpress backend so the admin of the site can see what native language that user is.
My extra profile field id is 136
How would I get this to show on the user-edit.php page?
Put this function in your theme/functions.php or in plugins/bp-custom.php
It should appear at the bottom of user-edit.php
function show_136_field ( $user ) {
$field_value = xprofile_get_field_data( 136, $user->ID, $multi_format = 'comma' );
echo "<br />Language: " . $field_value . "<br />";
}
add_action( 'edit_user_profile', 'show_136_field' );
You can also get the field value with the field name rather than id.
$field_value = xprofile_get_field_data( 'Language', $user->ID, $multi_format = 'comma' );
If the $field_value is an array and $multi_format = 'comma', a csv string will be returned.

Resources