Estoy desarrollando un complemento de búsqueda y reemplazo para WordPress, configuro un sitio múltiple de mi WordPress localmente en la misma cuenta. Debe buscar el contenido de la página en el PRIMER SITIO WEB y luego apuntar al SEGUNDO SITIO WEB para reemplazar la palabra clave que se encontró en el PRIMER SITIO WEB.
<php
// Hook to load the admin page
add_action('admin_menu', 'add_page_search_admin_page');
// Function to add the admin page
function add_page_search_admin_page() {
add_menu_page(
'Page Search', // Page Title
'Page Search', // Menu Title
'manage_options', // Capability
'page-search', // Menu Slug
'page_search_page' // Callback function to display the page
);
}
// Callback function to display the admin page
function page_search_page() {
?>
<div class="wrap">
<h2>Search and Replace</h2>
<form method="post">
<label for="search_text">Search Text: </label>
<input type="text" name="search_text" id="search_text">
<label for="replace_text">Replace Text: </label>
<input type="text" name="replace_text" id="replace_text">
<input type="submit" name="submit" class="button-primary" value="Search and Replace">
</form>
<?php
if (isset($_POST['submit'])) {
$search_text = sanitize_text_field($_POST['search_text']);
$replace_text = sanitize_text_field($_POST['replace_text']);
perform_search_and_replace($search_text, $replace_text);
}
?>
</div>
<?php
}
// Function to make a remote request to Website1 and search for text
function website1_request() {
$url_website1 = 'http://localhost/Website1/wp-json/wp/v2/pages';
$username1 = 'admin';
$password1 = 'Administrator123!';
$response = wp_remote_get($url_website1, array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode($username1 . ':' . $password1)
)
));
if (is_wp_error($response)) {
// Handle the error
$error_message = wp_remote_retrieve_response_message($response);
echo "Website1 Request Error: $error_message";
return null;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body);
return $data;
}
// Function to perform the search and replace
function perform_search_and_replace($search_text, $replace_text) {
$data = website1_request();
if ($data) {
foreach ($data as $page) {
if (isset($page->content->rendered) && strpos($page->content->rendered, $search_text) !== false) {
$updated_content = str_replace($search_text, $replace_text, $page->content->rendered);
if (update_website2($updated_content)) {
echo "Page updated and published on Website2.";
} else {
echo "Failed to update and publish the page on Website2.";
}
}
}
echo "Search and replace completed.";
} else {
echo "Failed to retrieve data from Website1.";
}
}
// Function to retrieve all pages from Website2
function website2_request_all_pages($url, $username, $password) {
$response = wp_remote_get($url, array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode($username . ':' . $password)
)
));
if (is_wp_error($response)) {
// Handle the error
$error_message = wp_remote_retrieve_response_message($response);
echo "Website2 Request Error: $error_message";
return null;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body);
return $data;
}
// Function to update Website2 with modified content
function update_website2($content) {
$url_website2 = 'http://localhost/Website2/wp-json/wp/v2/pages';
$username2 = 'admin';
$password2 = 'Administrator123!';
// Retrieve all pages from Website2
$pages = website2_request_all_pages($url_website2, $username2, $password2);
var_dump($pages);
if ($pages) {
// Search for the matching page on Website2 based on content
foreach ($pages as $page) {
if (isset($page->content->rendered) && $page->content->rendered === $content) {
// Matching content found, update the page on Website2
$page_id = $page->id;
var_dump($page_id);
$update_url="http://localhost/Website2/wp-json/wp/v2/pages/" . $page_id;
$update = wp_remote_post($update_url, array(
"headers" => array(
'Authorization' => 'Basic ' . base64_encode($username2 . ':' . $password2)
),
"body" => array(
"content" => $content,
"status" => "publish"
)
));
if (is_wp_error($update)) {
// Handle the error
$error_message = wp_remote_retrieve_response_message($update);
echo "Website2 Update Error: $error_message";
return false;
}
echo "Page on Website2 updated with ID {$page_id}.";
return true;
}
}
echo "No matching page found on Website2 for the provided content.";
} else {
echo "Failed to retrieve pages from Website2.";
}
}