0

I have a script which scrapes articles from the web and saves the url to a .txt file. I've created a custom plugin which on activation, loops through the urls and creates a draft post with the content being an embedded article. I'm using the following code in my plugin

<?php
register_activation_hook(__FILE__, 'my_plugin_activate');
function my_plugin_activate(){
    my_plugin_install_site();
}

function my_plugin_install_site(){
    global $user_ID;
    $handle = fopen("listOfURLs.txt", "r");
    if ($handle) {
        $count = 0;
        while (($line = fgets($handle)) !== false) {
            // process the line read.
            PostCreator('test', 'post', '<iframe src="' . trim($line) . '" class="alumni-embeded-page"></iframe>');
        }
        fclose($handle);
    }
    // PostCreator( 'test1', 'post', '<iframe src="[link]" class="alumni-embeded-page"></iframe>' );
    // PostCreator( 'test2', 'post', '<iframe src="[link]" class="alumni-embeded-page"></iframe>' );
}

function PostCreator(
    $name      = 'AUTO POST',
    $type      = 'post',
    $content   = 'DUMMY CONTENT',
    $category  = array(1,2),
    $template  = NULL,
    $author_id = '1',
    $status    = 'draft'
) {
    $post_data = array(
        'post_title'    => wp_strip_all_tags( $name ),
        'post_content'  => $content,
        'post_status'   => $status,
        'post_type'     => $type,
        'post_author'   => $author_id,
        'post_category' => $category,
        'page_template' => $template
    );
    $post_id = wp_insert_post($post_data); 
}

The listOfURLs.txt file is simply a text file in the same directory with a link to an article on every line. I'm only using 5 links to test at the moment. The problem is currently the script does not create any posts when trying to use the loop. The PostCreator() method did work when I coded the method calls manually(as shown by the comments).

I'm also getting the following error when activating the plugin. Not sure it's related as it was still displayed when hard coding the method calls.

The plugin generated 205 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

4
  • Enable debug logging in your wp-config.php i.e. define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true );, re-activate your plugin, then check wp-content/debug.log to find out what the cause of the unexpected output is. Commented May 20, 2022 at 14:35
  • I had tried that but the 'debug.log' file is never created Commented May 20, 2022 at 14:43
  • Possibly an environment configuration issue. Sometimes manually creating the debug.log file will work.
    – bosco
    Commented May 22, 2022 at 16:50
  • I ran the plugin on a local host and was able to debug with no problems, the way I was referencing the .txt file was incorrect. Still not sure why the debugging doesn't work on the remote server. It's hosted by my uni and I don't have access to the .ini files, so might be an issue with how the php is set up. Commented May 23, 2022 at 18:40

1 Answer 1

0

I think you get errors because you create duplicated posts named as "test". Postcreator function's 1rst argument should be dynamic, hot hardcoded. When you activate the plugin the loop is executed and the "generated 205 characters of unexpected ..." probably is the database error message of duplicate post

1
  • I just tried to wp_insert_post() 3 posts with the post_title "Title" on my local installation, and WP didn't bat an eye. I don't think the titles are the problem.
    – Pat J
    Commented Nov 16, 2023 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.