wp_insert_post()在循环时不起作用

发布于 2025-01-30 16:55:26 字数 1757 浏览 1 评论 0原文

我有一个脚本,可以将文章从网络上刮下来,并将URL保存到.txt文件中。我创建了一个自定义插件,该插件在激活中,通过URL循环并创建草稿帖子,其中内容为嵌入式文章。我在插件中使用以下代码

<?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); 
}

listofurls.txt 文件只是同一目录中的文本文件,并在每行上的文章链接。目前,我只使用5个链接进行测试。问题当前是脚本在尝试使用循环时不会创建任何帖子。当我手动编码该方法调用时,PostCreator()方法确实有效(如评论所示)。

激活插件时,我还会遇到以下错误。不确定它是否相关,因为在硬编码方法调用时仍显示它。

插件在激活过程中生成了205个意外输出字符。如果您注意到“已经发送”消息,联合提要或其他问题的问题,请尝试停用或删除此插件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文