'trying to save text editor field in options page to add to post content multisite wordpress

I made a quite simple options page on the dashboard of a multisite. It involves a text editor field and a list of sites on the multisite. The idea is that a site administrator can add content to the front pages of each multisite, by selecting the specific multisite.

So i created the options page like this:

function new_menu_item() {

    add_menu_page(
        'Add content Page',
        'Add content',
        'manage_options',
        'content-page',
        'content_page_1',
        'dashicons-welcome-add-page',
        99,
    );

}
add_action("network_admin_menu", "new_menu_item");

//add content field to the settings page

function content_page_1() {
    $html = '';
    $content = '';
    $sites = get_sites();
    foreach($sites as $site){
    
    $site_id = $site->blog_id;
    $site_name = get_blog_details( array( 'blog_id' => $site_id ) )->blogname;
    $html .= '<tr><td><input name="siteid[]" type="checkbox" value="' . $site_id . '" action="options.php"> ' . $site_name . '</label></td></tr>';
    }

    echo '<div class="wrap">
        <h1>Add content to websites of the multisite</h1>
        <form method="post">';
            echo '
            <h2>Step 1</h2>
            <table class="form-table">
                <tr>
                    <th scope="row"><label for="some_field">Add content</label></th>
                    <td>';
                        wp_editor( $content, 'site_texteditor',array('textarea-name'=>'Text editor'));
                    echo '
                    </td>
                </tr>
            </table>
            <h2>Select sites from the network</h2>
            <table class="form-table">
                <tr>
                    <th scope="row">Sites</th>
                </tr>
                    ' . $html . '
            </table>';
            submit_button();
        echo '</form></div>';

}

Then the second step is to save the input to the existing front page. For testing purposes, i have replaced it by an insert post function with a non-existing id. So i can see wether it works before erasing an existing front page.

function save_posts_websites(){
    
    if (!empty($_POST['siteid']) ) {
    $blog_ids = $_POST['siteid'];
    }
    if (!empty ($_POST['site_texteditor'])){
    $new_content = $_POST['site_texteditor'];
    }
    // let's get this post data as an array

    foreach( $blog_ids as $blog_id ) {
        
        switch_to_blog( $blog_id );
        $post = get_post( 2 );
        $old_content = $post->post_content;
        $updated_content = $old_content . $new_content;
        $my_post = array(
            'ID'            => 444,
            'post_name'     => 'tryout',
            'post_title'    => 'testpage',
            'post_content'  => $updated_content,
            'post_status'   => 'publish',
            'post_type'     => 'page'
        );
        
        wp_insert_post( $my_post );
        restore_current_blog();
    }
}
add_action( 'save_post', 'save_posts_websites'); 

However, it is not working. I tried a lot, including changing the action to admin_init, init, print_r all variables (they are correct). So i cant figure out what is wrong...

Any ideas?



Solution 1:[1]

I made a mistake in the second part (save post). I changed it to init. I made some smaller changes too, as the plugin is ment to update the existing content of the front page.

function save_posts_websites(){
    
    if (!empty($_POST['siteid']) ) {
    $blog_ids = $_POST['siteid'];
    if (!empty ($_POST['site_texteditor'])){
    $new_content = $_POST['site_texteditor'];
    }

    foreach( $blog_ids as $blog_id ) {
        
        switch_to_blog( $blog_id );
        $page_id = get_option('page_on_front');
        $post = get_post( $page_id );
        $old_content = $post->post_content;
        $updated_content = $old_content . '<p>' . $new_content;
        $my_post = array(
            'ID'            => $page_id,
            'post_content'  => $updated_content,
            'post_status'   => 'publish',
            'post_type'     => 'page',
            'post_author'   => 1
        );
        wp_update_post( $my_post,true );
        restore_current_blog();
        }
    }
}
add_action( 'init', 'save_posts_websites' );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kees