0

I'm sorry, I speak a little English.

Why not works?

My plugin: ~/wp-content/plugins/mypluggg/mypluggg.php

<?php
/**
 * Plugin Name: mypluggg
 */
    function videos() {
        // file_get_content()
        // $http_response_header
        $postarr = array();
        foreach( $videos as $key=> $video ) {
            $postarr [ $key ] = array( // etc.
            wp_insert_post( $postarr [ $key ], $wp_error );
        }
    }
    videos();
?>

Add 1 post and then error screen:

https://i.sstatic.net/JQAh4.png

There has been a critical error on this website. Learn more about troubleshooting WordPress.

Why?

UPDATE(1):

foreach( $videos as $key=> $video ) {

    $postarr [ $key ] = array(

        'post_title'=> $videos [ 'title' ] [ $key ],
        'post_content'=> '<iframe src="https://*.com/' . $videos [ 'id' ] [ $key ] . '" frameborder=0 width=510 height=400 scrolling=no allowfullscreen=allowfullscreen></iframe>',
        'post_status'=> 'publish',
        'post_author'=> 1,

        'meta_input'=> array(

            'site'=> '*.com',
            'video_id'=> $videos [ 'id' ] [ $key ],
            'thumbnail'=> $thumbnail,
            'video_url'=> $video_url,
            'iframe_src_start'=> 'https://*.com/embedframe/',
            'iframe_src_end'=> '',
            'v_tag'=> $tag

        )

    );

}

UPDATE(2):

My ~/error_log file (I don't call is_user_logged_in function):

[31-Aug-2021 15:16:22 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function is_user_logged_in() in /home/x/public_html2/y/public_html/wp-includes/post.php:2793
Stack trace:
#0 /home/x/public_html2/y/public_html/wp-includes/post.php(7359): _count_posts_cache_key('post', 'readable')
#1 /home/x/public_html2/y/public_html/wp-includes/class-wp-hook.php(303): _transition_post_status('publish', 'new', Object(WP_Post))
#2 /home/x/public_html2/y/public_html/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters('', Array)
#3 /home/x/public_html2/y/public_html/wp-includes/plugin.php(470): WP_Hook->do_action(Array)
#4 /home/x/public_html2/y/public_html/wp-includes/post.php(5098): do_action('transition_post...', 'publish', 'new', Object(WP_Post))
#5 /home/x/public_html2/y/public_html/wp-includes/post.php(4368): wp_transition_post_status('publish', 'new', Object(WP_Post))
#6 /home/x/public_html2/y/public_html in /home/x/public_html2/y/public_html/wp-includes/post.php on line 2793

UPDATE(3):

Is not working, I see the error screen (https://i.sstatic.net/JQAh4.png):

$ID = wp_insert_post( $postarr [ $key ], $wp_error );
if( ! is_wp_error( $ID ) ) {} else {
    echo $ID-> get_error_message();
} else {}

UPDATE(4):

I try create my new test custom plugin, but the problem is the same. The full code:

~/wp-content/plugins/mypluk/mypluk.php

<?php

/**
 * Plugin Name: mypluk
 */

    function videoss() {

        $postarr = array();

        foreach( array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 ) as $key ) {

            $postarr [ $key ] = array(

                'post_title'=> 'title' . $key

            );

            $ID = wp_insert_post( $postarr [ $key ], $wp_error );

        }

    }

    videoss();

?>
15
  • Can you fix your code example to include the missing sections? The values in $postarr are important. also notice the code uses a $wp_error variable, but this variable isn't declared or defined. There is also no error checking. If wp_insert_post fails it returns an error object explaining why, but there is no check to see if this happens. Where does $videos come from?
    – Tom J Nowell
    Commented Aug 31, 2021 at 15:05
  • @TomJNowell please see the UPDATE(1). If commented // wp_insert_post(); and I check the $postarr array, this array is fine.
    – jozs2021
    Commented Aug 31, 2021 at 15:14
  • have you made any modifications to the files in wp-includes?
    – Tom J Nowell
    Commented Aug 31, 2021 at 15:24
  • Also, expect iframe tags to be stripped by security filters, you should use a shortcode instead or oembed of some kind
    – Tom J Nowell
    Commented Aug 31, 2021 at 15:26
  • 1
    The first $postarr array wp_insert_post is okay, but need more 26 posts in foreach loop. Why only the first? First post added, and see the error screen.
    – jozs2021
    Commented Aug 31, 2021 at 15:43

1 Answer 1

0

The problem as I could see it from your code and the error logs, is that your plugin is calling wp_insert_post() too early, which is before functions like is_user_logged_in() is defined or before the file it's defined is loaded, which by the time you call videos() or videoss() (which calls wp_insert_post()), that file has not yet been loaded and thus, PHP threw the fatal error which says "Call to undefined function is_user_logged_in()".

My ~/error_log file (I don't call is_user_logged_in function):

Yes maybe you didn't, but then, wp_insert_post() called wp_transition_post_status() and if you examine the stack trace in the error log entry, what happened is:

  1. wp_transition_post_status() fired the transition_post_status hook.

  2. Then that hook called one of its registered callbacks, which is _transition_post_status().

  3. And finally, that function called _count_posts_cache_key() which is the one that called is_user_logged_in().

And in response to your comment: (* I formatted it for brevity)

The first $postarr array wp_insert_post is okay, but need more 26 posts in foreach loop. Why only the first? First post added, and see the error screen.

The reason why the first one worked is because the post was already added to the database before the post status is transitioned, but the other (26) posts could not be created because after wp_transition_post_status() is called, PHP threw the above fatal error and therefore WordPress displayed the error screen.

So how can you solve the problem?

It's basically simple: Use a hook like plugins_loaded, init or wp_loaded to run your videos() or videoss() function (you got two plugins in the question) and don't simply call the function which would be the same as you calling wp_insert_post() directly from the root in the main plugin file.

So for example using the init hook, in mypluggg.php, call videos() like so:

add_action( 'init', 'mypluggg_create_posts' );
function mypluggg_create_posts() {
    videos();
}
// or yes, this would work as well..
//add_action( 'init', 'videos' );

However, note that init (and the other 2 hooks mentioned above) runs on each page load, so you might better off look for another approach to create the posts you wanted to create, e.g. you could use a cron job or WP-CLI, but that's another story. :)

Additionally, if you want wp_insert_post() to return a WP_Error instance/object if the post creation/update didn't succeed, then set the 2nd parameter to true like so: wp_insert_post( $postarr[ $key ], true ), or you could use $wp_error, but make sure that variable is defined. E.g.

$wp_error = true; // here, define it

foreach ( $videos as $key => $video ) {
    $postarr[ $key ] = array( ... );

    // pass $wp_error as the 2nd parameter
    $id = wp_insert_post( $postarr[ $key ], $wp_error );

    if ( is_wp_error( $id ) ) {
        error_log( 'Error creating post! Message: ' . $id->get_error_message() );
    }
}

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.