Send post to remote wp site from post edit window with all data excerpt image

From the code you’ve provided, it seems like you’re on the right track. However, there are a few potential issues that might be causing problems:

  1. Form Submission: The form submission might not be working as expected because WordPress admin area already has a form for post editing. When you add another form inside it, it might not work correctly. Instead of creating a new form, you can add a custom submit button to the existing post edit form.

  2. Data Preparation: The REST API expects the data in a specific format. For example, categories and tags should be arrays of term IDs, not names. Also, the custom fields should be an array of meta keys and values.

  3. Error Handling: Your error handling seems to be reversed. is_wp_error($response) will return true if there was an error, not if the request was successful.

  4. Authentication: The Basic Auth method you’re using might not be enabled on the remote site. You need to ensure that the Basic Auth plugin is installed and activated on the remote site.

Here’s a revised version of your code:

function render_custom_meta_box() {
    // Add a custom submit button to the existing post edit form
    echo '<input type="submit" name="push_to_remote_site" value="Push to Remote Site">';

    if (isset($_POST['push_to_remote_site'])) {
        // Code for pushing data to the remote site when the button is clicked

        $post_id = get_the_ID();

        // Retrieve post data, build the array
        $post_title = get_the_title($post_id);
        $content = get_post_field('post_content', $post_id);
        $categories = wp_get_post_categories($post_id);
        $tags = wp_get_post_tags($post_id, array('fields' => 'ids'));
        $custom_fields = get_post_custom($post_id);

        $post_data = array(
            'title' => $post_title,
            'content' => $content,
            'categories' => $categories,
            'tags' => $tags,
            'status' => 'draft',
            'meta' => $custom_fields, // Include all custom fields.
        );

        // Convert the post data to JSON format
        $post_data = wp_json_encode($post_data);

        // Define your remote website URL and authentication credentials
        $remote_url="https://www.ex2.com/wp-json/wp/v2/posts";
        $login = 'username';
        $password = 'password';

        // Send the post data to the remote website
        $response = wp_remote_post(
            $remote_url,
            array(
                'headers' => array(
                    'Authorization' => 'Basic ' . base64_encode("$login:$password"),
                    'Content-Type' => 'application/json',
                ),
                'body' => $post_data,
            )
        );

        if (is_wp_error($response)) {
            echo "Error!";
        } else {
            echo "Success";
        }
    }
}

add_action('add_meta_boxes', 'add_custom_meta_box');

Remember to replace ‘username’ and ‘password’ with your actual username and password. Also, ensure that the REST API is enabled on the remote site and that it allows creating posts.

techhipbettruvabetnorabahisbahis forumuedusedueduedusedueduseduedusedusedu