How to get all admins of multisite in wordpress - 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();

Related

Add custom content after redirect wordpress

I've created a function redirecting all my logged-out users to the membership page.
I want them to buy a package plan before they can view the page they tried to visit before being redirected.
If logged-in -> view the download page.
If logged-out -> redirect to the membership page -> purchase a package -> view the download page.
I want to add content to the membership page, something like:
'The product that you are trying to download cannot be purchased individually, but it's part of our membership package'
The problem is that I couldn't find a way to execute any function after the wp_redirect() function. So I don't know how to display the content only to those redirected.
Is there a way to achieve it?
in your header.php:
if( !is_user_logged_in() ){
nocache_headers();
wp_safe_redirect( get_permalink( 'YOUR_MEMBERSHIP_PAGE_ID' ) . '?message=true' );
exit;
}
in your functions.php:
function add_query_vars_filter( $vars ){
$vars[] = "message";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
in your membership page:
if( get_query_var('message') && !is_user_logged_in() ){
echo 'The product that you are trying to download cannot be purchased individually, but it\'s part of our membership package';
}

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 );
}
}

Restrict WordPress Rest API requests to my domain

I have a WordPress website which I use just to populate blog posts and some private posts under custom post types. In another website, I am using the REST API to display the posts. If I use software like Postman, I can display data from the REST API.
How can I prevent any unauthorized REST API requests to domain www.example.com ? so if the request is not coming from www.mysite.com, it is blocked?
Basically prevent my custom post types (example.com) to be visible to the rest api if it is not coming from mysite.com
You can Disable External request by adding this in your wp-config.php ( Also, you can specify domain which you don't want to block like this).
define( 'WP_HTTP_BLOCK_EXTERNAL', TRUE );
define( 'WP_ACCESSIBLE_HOSTS', 'example.com, domain.com' );
apply_filters( 'rest_authentication_errors', WP_Error|null|bool )
Filters REST authentication errors.Put code in functions.php in your theme directory.
Complete description : https://developer.wordpress.org/reference/hooks/rest_authentication_errors/
add_filter( 'rest_authentication_errors', 'wpse150207_filter_incoming_connections' );
function wpse150207_filter_incoming_connections( $errors ){
$allowed_ips = array( '127.0.0.1' );
$request_server = $_SERVER['REMOTE_ADDR'];
if( ! in_array( $request_server, $allowed_ips ) )
return new WP_Error( 'forbidden_access', 'Access denied', array( 'status' => 403 ) );
return $errors;
}
One way to restrict REST requests is to hook at rest_api_init with priority 1, and whitelist the IP's you want. In this example, I restrict REST access to the server itself only:
/**
* Disables WordPress Rest API for external requests
*/
add_action('rest_api_init', function() {
$whitelist = ['127.0.0.1', "::1"];
if(!in_array($_SERVER['REMOTE_ADDR'], $whitelist)){
die('REST API is disabled.');
}
}, 1);

Making Wordpress user metadata available to Rest-API

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>';
}

Automatically create site in multisite Wordpress installation

I have a multisite Wordpress installation that I want to connect to a different application via webservices. I'm getting a list of users and I need to create sites automatically. I'm not sure how exactly to do that. Any tips would be appreciated. Thanks.
This is handled internally by WordPress using a file called /wp-admin/network/site-new.php by calling the wpmu_create_blog() function.
global $wpdb;
$domain = 'example.com'; // your domain (or subdomain)
$path = '/blog'; // path to your site
$title = 'My Site'; // site title
$user_id = get_current_user_id(); // the user id that owns this site
// hide db errors
$wpdb->hide_errors();
// create the new site
$id = wpmu_create_blog( $domain, $path, $title, $user_id , array( 'public' => true ) );
// enable db errors
$wpdb->show_errors();

Resources