• Plans & Pricing
  • Log in
  • Get started
  • WordPress Hosting
  • WordPress for Agencies
  • Become an Affiliate
  • Domain Names
  • AI Website Builder
  • Website Builder
  • Create a Blog
  • Newsletter
  • Professional Email
  • Website Design Services
  • Commerce
  • WordPress Studio 
  • Enterprise WordPress 
  • Overview
  • WordPress Themes
  • WordPress Plugins
  • WordPress Patterns
  • WordPress AI Features
  • Google Apps
  • Support Center
  • WordPress News
  • Business Name Generator
  • Logo Maker
  • Discover New Posts
  • Popular Tags
  • Blog Search
Get started
  • Sign up
  • Log in
About
  • Plans & Pricing
Products
  • WordPress Hosting
  • WordPress for Agencies
  • Become an Affiliate
  • Domain Names
  • AI Website Builder
  • Website Builder
  • Create a Blog
  • Newsletter
  • Professional Email
  • Website Design Services
  • Commerce
  • WordPress Studio  
  • Enterprise WordPress  
Features
  • Overview
  • WordPress Themes
  • WordPress Plugins
  • WordPress Patterns
  • WordPress AI Features
  • Google Apps
Resources
  • Support Center
  • WordPress News
  • Business Name Generator
  • Logo Maker
  • Discover New Posts
  • Popular Tags
  • Blog Search
Jetpack App
  • Learn more
  • Support Center
  • Guides
  • Courses
  • Forums
  • Contact
Search
  • Support Center
  • Guides
  • Courses
  • Forums
  • Contact
Forums / How can I fix not replacing content in another wordpress website

How can I fix not replacing content in another wordpress website

  • Unknown's avatar
    szyscintilla · Member · Oct 20, 2023 at 9:33 am
    • Copy link Copy link
    • Add topic to favorites Add topic to favorites

    I’m developing search and replace plugin for WordPress, I setup a multisite of my WordPress locally in same account. It should search page content in the 1ST WEBSITE then target the 2ND WEBSITE for the replacement of keyword that been found in the 1ST WEBSITE.

    // 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
        );
    }
     // Function to render the admin page
    function page_search_page() {
        $jsonResult = null;
        // Check if the form is submitted
        if (isset($_POST['search_keyword']) && isset($_POST['replace_keyword'])) {
            $search_keyword = sanitize_text_field($_POST['search_keyword']);
            $replace_keyword = sanitize_text_field($_POST['replace_keyword']);
        
            // Call search_keyword_on_website1 with the search and replace keywords
            $search_result = search_keyword_on_website1($search_keyword, $replace_keyword);
        
            if ($search_result) {
                // Update the content on the second website using the post ID where the keyword was found
                update_content_on_website2($search_result['content'], $replace_keyword, $search_result['post_id']);
            } else {
                // Handle the case where the keyword was not found in Website 1's content
                echo 'Keyword not found in the content from Website 1.';
            }
        }
            echo '<div class="wrap">';
            echo '<h2>Page Search and Replace</h2>';
            echo '<form method="post">';
            echo '<input type="text" name="search_keyword" placeholder="Enter a keyword">';
            echo '<input type="text" name="replace_keyword" placeholder="Enter a replacement keyword">';
            echo '<input type="submit" value="Search and Replace">';
            echo '</form>';
            echo '</div>';
    
        // Display the JSON result, if available
        if ($jsonResult) {
            echo '<div class="wrap">';
            echo '<pre>' . $jsonResult . '</pre>';
            echo '</div>';
        }
    }
    
    function search_keyword_on_website1($search_keyword, $replace_keyword) {
        // URL for the REST API endpoint of the first website
        $website1_url = 'http://localhost/wordpress-sample-website/wp-json/wp/v2/pages';
    
        // Define your authentication credentials
        $username = 'admin'; // Replace with your actual username
        $password = 'Administrator123!'; // Replace with your actual password
    
        // Create the authorization header
        $auth_header = 'Basic ' . base64_encode($username . ':' . $password);
    
        // Create the headers array with the authorization header
        $headers = array(
            'Authorization' => $auth_header,
        );
    
        // Make a GET request with the authorization header
        $website1_response = wp_remote_get($website1_url, array(
            'headers' => $headers,
        ));
    
        if (is_array($website1_response) && !is_wp_error($website1_response)) {
            // Get the body of the response
            $website1_content = wp_remote_retrieve_body($website1_response);
    
            // Decode the JSON response into an array
            $pages_data = json_decode($website1_content, true);
    
            if (is_array($pages_data)) {
                foreach ($pages_data as $page) {
                    // Check if the keyword is found in the content of this page
                    if (stripos($page['content']['rendered'], $search_keyword) !== false) {
                        // If keyword is found, perform content replacement
                        $content_from_website1 = str_ireplace($search_keyword, $replace_keyword, $page['content']['rendered']);
                        
                        // Return the content and the post ID
                        return array(
                            'content' => $content_from_website1,
                            'post_id' => $page['id']
                        );
                    }
                }
            }
        }
    
        // Keyword not found in the content or an error occurred
        return false;
    }
    
    function update_content_on_website2($content_from_website1, $replace_keyword, $post_id) {
        // URL for the REST API endpoint of the second website (Website 2)
        $website2_url = 'https://localhost/wordpress-sample-website/website2/wp-json/wp/v2/pages/' . $post_id;
    
        // Authentication credentials
        $username = 'admin'; // Replace with your actual username
        $password = 'Administrator123!'; // Replace with your actual password
    
        // Create an array with the content to update
        $content_to_update = array(
            'content' => $content_from_website1,  // Use the content from Website 1
            'status' => 'publish', // Set the status to 'publish'
        );
    
        // Convert the array to JSON
        $content_json = json_encode($content_to_update);
    
        // Make a POST request to update the content on Website 2 with basic authentication
        $website2_response = wp_remote_post($website2_url, array(
            'body' => $content_json,
            'headers' => array(
                'Content-Type' => 'application/json', // Use JSON content type
                'Authorization' => 'Basic ' . base64_encode($username . ':' . $password)
            )
        ));
    
        if (is_array($website2_response) && !is_wp_error($website2_response)) {
            echo 'Successfully replace content!!';
            // Content has been successfully replaced in Website 2
            // You can add further error handling or success messages as needed
        } else {
            // Handle errors with the POST request, if any
            echo 'Error updating content on Website 2.';
        }
    }
    
    function search_and_replace_content($search_keyword, $replace_keyword) {
        $pages = get_pages();
    
        foreach ($pages as $page) {
            $page_content = $page->post_content;
    
            if (stripos($page_content, $search_keyword) !== false) {
                $updated_content = str_ireplace($search_keyword, $replace_keyword, $page_content);
    
                // Update the page with the replaced content
                wp_update_post(array(
                    'ID' => $page->ID,
                    'post_content' => $updated_content,
                    'status' => 'publish'
                ));
            }
        }
    
        // Display a success message or any additional feedback
        echo 'Content replacement completed.';
    }
    

    I tried fixing but still I can’t figure out what’s the problem in my code.

  • Unknown's avatar
    staartmees · Member · Oct 20, 2023 at 10:46 am
    • Copy link Copy link

    We can’t help as your sites aren’t running on the wordpress.com platform nor can we help with the development of plugins. You could try to get help in the forums at https://wordpress.org/support/forums/

  • The topic ‘How can I fix not replacing content in another wordpress website’ is closed to new replies.

Tags

  • .org
  • account

About this topic

  • In: Support
  • 2 participants
  • 1 reply
  • Last activity 3 years
  • Latest reply from szyscintilla

Have a question?

Get in touch
Back to Top

Couldn't find what you needed?

Contact us

Get answers from our AI assistant, with access to 24/7 expert human support on paid plans.

Browse our guides

Find step-by-step solutions to common questions in our comprehensive guides.

WordPress.com

Products
  • WordPress Hosting
  • WordPress for Agencies
  • Become an Affiliate
  • Domain Names
  • AI Website Builder
  • Website Builder
  • Create a Blog
  • Professional Email
  • Website Design Services
  • WordPress Studio
  • Enterprise WordPress
Features
  • Overview
  • WordPress Themes
  • WordPress Plugins
  • WordPress Patterns
  • WordPress AI Features
  • Google Apps
Resources
  • WordPress.com Blog
  • Business Name Generator
  • Logo Maker
  • WordPress.com Reader
  • Accessibility
  • Remove Subscriptions
Help
  • Support Center
  • Guides
  • Courses
  • Forums
  • Contact
  • Developer Resources
Company
  • About
  • Press
  • Terms of Service
  • Privacy Policy
  • Do Not Sell or Share My Personal Information
  • Privacy Notice for California Users
DeutschEspañolFrançaisBahasa IndonesiaItalianoNederlandsPortuguês do BrasilSvenskaTürkçeРусскийالعربيةעִבְרִית日本語한국어简体中文繁體中文English

Mobile Apps

  • Download on the App Store
  • Get it on Google Play

Social Media

  • WordPress.com on Facebook
  • WordPress.com on X (Twitter)
  • WordPress.com on Instagram
  • WordPress.com on YouTube

Automattic

Automattic
Work With Us
    • WordPress.com Forums
    • Sign up
    • Log in
    • Copy shortlink
    • Report this content
    • Manage subscriptions