How to use on multisite pulling jobs from different subsites

Einfach Dinge, die nichts mit XAMPP, Apache Friends, Apache, MySQL, PHP und alle dem zu tun haben. Allerlei halt. ;)

How to use on multisite pulling jobs from different subsites

Postby unleash_it » 19. December 2020 19:55

here i am back with some more ideas, insights and musings

above all – i guess that many many wp-job-manager-user would like to go this way.

i am planning to set up a wp-site where a wordpress plugin (wp-job-manager) runs.
i want to connect two other sites to that: well – We would like to create a dropdown menu that selects data-items from different sites on a WordPress-multisite installation.

Guess i can do this with the wp-function “connect wpdb” to connect to another database. one method that comes to mind is to create the instance and pass it the database name/username/password.

scenario and concrete plan: when on one site, I would like to be able to select a site from a dropdown menu and have it populate with that other subsite’s listings of database-entries from the wordpress plugin (wp-job-manager).

Background: So there would be a central db where all the records are stored and which would support the three entities of sites:

see how this architecture would look like – i would have three sites:

jobs1.mysite.com
jobs2.mysite.com
jobs3.mysite.com


note – you see this could be a combination of three job sites – running the wp-plugin wp-job-manager.

Question: is this doable – how to create the subsites – how to work on only one Database with the records? Any advice on the best way to accomplish this?

some musings about the setup: First thing that comes up to mind is the question, how to allow one site to get database-data from another site. This i guess can be a little bit tricky, this is not something that is normally done in a wordpress-setup.
But there are certainly approaches to accomplish: The easy way would be, if the three sites are all on the same server under the same hosting account, that is helpful. In a case like that, we surely could write a little and tiny custom code-script to access the other site’s database directly from one or the other site. Wouldn´t this be applicable!?`What do you think!?

Hmm – i guess that this is possible: we can get a interaction and interoperation between two databasese – if the both were residing on the same server. to spell it out in other words: the wpdb can be instantiated to access any database and query any table.
How it would work: The wpdb object should be used to support the access of any database and and support any query of tables. The great benefit of this method is the ability to use all the general wpdb-classes and subsequently also the functions like get_results and others more.

well i guess that we can do this probably like so:

Code: Select all
$mydb = new wpdb('username','password','database','localhost');
$rows = $mydb->get_results("select Name from my_table");
echo "<ul>";
foreach ($rows as $obj) :
   echo "<li>".$obj->Name."</li>";
endforeach;
echo "</ul>";


well – i guess that the connecting to a second wordpress-database should be not that difficult in WordPress. <strong>Imagine </strong>- we would be able to setup the accessibility and interoperation between the db. That would be just great. i guess that i just would have to create a new instance of the WPDB class and then i should make use it the same way i would use the general standard $wpdb instance. What do you think about this idea?!

Well i am assuming the second database has the same login information as the main WP (let us call this the WP-DB-One) then we could even use the predefined-constants that are written down in the wp-config.php to avoid any issues with the hardcoding of the database-login information of the site.

i guess that we can work with the following piece of code:

Code: Select all

/**
 * we can do the Instantiate of the the wpdb class. This could be done easily: in order to connect to the second database, $database_name
 */
$second_db = new wpdb(DB_USER, DB_PASSWORD, $database_name, DB_HOST);
/**
 * What do you think abou this approach: here we Use the new database object just like we would use the first one - the $wpdb
 */
$results = $second_db->get_results($your_query);



Well – how about this approach: guess that we can go like this: And thirdly – using a third approach additional this can be done like so:
if the additional databases (in other words those we also want to access) has the same db-access-credentials (in other words the user/pass details to access’) as our main wordpress database we can use the database name before the table name like this

$query = $wpdb->prepare('SELECT * FROM dbname.dbtable WHERE 1');
$result = $wpdb->get_results($query);


While these probably will be able to work, one eventually will lose the ability to use the “other” custom features such as the following – eg. get_post_custom and other wordpress queries. The simple solution is

$wpdb->select('database_name');
which changes the database system-wide (a mysql select_db).
<strong>imagine</strong>


this approach is very interesting since the database.table method works if we just want to make a simple query, but if we will want to access another wordpress site

note – the <strong>architecture</strong> looks like so:


Code: Select all
jobs1.mysite.com
jobs2.mysite.com
jobs3.mysite.com


we can use the select-option. We just need to change it back when we re done.
Interessen: Bikes & steel frames: Linux & SBC https://www.allaboutcircuits.com :: die neuen Knowledge-Base: AFFiNE: There can be more than Notion and Miro. auf affine.pro :: WordPress Entwicklung - sic: make.wordpress.org/core/
User avatar
unleash_it
 
Posts: 786
Joined: 10. December 2011 18:32
Operating System: linux opensuse 12.1

Return to Allerlei

Who is online

Users browsing this forum: No registered users and 62 guests