admin管理员组

文章数量:1334826

Devs

I have a WP-cronjob running that is supposed to import some pictures and set them as attachments for a specific post. But I'm apparently unable to do so while running it as a WP-cronjob - it works just fine when i run it on "init" for exampel. But as soon as i try to run it within a WP-cronjob it fails.

 register_activation_hook( __FILE__, 'OA_FeedManager_activated' );
 function importpicture_activated()
 {
    if (! wp_next_scheduled( 'import_feed' ) ) 
    {
       wp_schedule_event( time(), 'hourly', 'import' );
    }
 }

  add_action( 'import', 'call_import' );
  function call_import() 
  {
        //lots of code, where i get data from a XML feed, and creates a new post. no need to include it.           

        foreach($oDataXML->Media->Fotos->Foto as $key => $value)
        {
            $filename = wp_upload_dir()['path'].'/'.$oDataXML->SagsNr->__toString().'_'. $value->Checksum->__toString() . '.jpg';  
            $sPicture = $value->URL->__toString();
            copy($sPicture, $filename);
            $filetype = wp_check_filetype( basename( $filename ), null );

            $wp_upload_dir = wp_upload_dir();
            $attachment = array(
               'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ),
               'post_mime_type' => $filetype['type'],
               'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
               'post_content'   => '',
               'post_status'    => 'inherit'
            );
            $attach_id = wp_insert_attachment( $attachment, $filename, $iPost_ID );
            $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
            wp_update_attachment_metadata( $attach_id, $attach_data );           
        }
  } 

As I have been able to read, its because that the wp_generate_attachment_metadata() function is not currently included when doing a wp-cronjob, and I should require_once('wp-load.php').

But sadly I'm not able to require that while running a cron job (I tried both wp-load.php & wp-includes/post.php) - but neither does the trick.

So maybe there is a wise man on WPSE that can enlighten me how I'm suppose to get my WP-cronjob running correctly.

Devs

I have a WP-cronjob running that is supposed to import some pictures and set them as attachments for a specific post. But I'm apparently unable to do so while running it as a WP-cronjob - it works just fine when i run it on "init" for exampel. But as soon as i try to run it within a WP-cronjob it fails.

 register_activation_hook( __FILE__, 'OA_FeedManager_activated' );
 function importpicture_activated()
 {
    if (! wp_next_scheduled( 'import_feed' ) ) 
    {
       wp_schedule_event( time(), 'hourly', 'import' );
    }
 }

  add_action( 'import', 'call_import' );
  function call_import() 
  {
        //lots of code, where i get data from a XML feed, and creates a new post. no need to include it.           

        foreach($oDataXML->Media->Fotos->Foto as $key => $value)
        {
            $filename = wp_upload_dir()['path'].'/'.$oDataXML->SagsNr->__toString().'_'. $value->Checksum->__toString() . '.jpg';  
            $sPicture = $value->URL->__toString();
            copy($sPicture, $filename);
            $filetype = wp_check_filetype( basename( $filename ), null );

            $wp_upload_dir = wp_upload_dir();
            $attachment = array(
               'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ),
               'post_mime_type' => $filetype['type'],
               'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
               'post_content'   => '',
               'post_status'    => 'inherit'
            );
            $attach_id = wp_insert_attachment( $attachment, $filename, $iPost_ID );
            $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
            wp_update_attachment_metadata( $attach_id, $attach_data );           
        }
  } 

As I have been able to read, its because that the wp_generate_attachment_metadata() function is not currently included when doing a wp-cronjob, and I should require_once('wp-load.php').

But sadly I'm not able to require that while running a cron job (I tried both wp-load.php & wp-includes/post.php) - but neither does the trick.

So maybe there is a wise man on WPSE that can enlighten me how I'm suppose to get my WP-cronjob running correctly.

Share Improve this question edited Mar 24, 2017 at 8:17 Mac asked Mar 23, 2017 at 14:54 MacMac 3131 gold badge3 silver badges14 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

Some of what is usually admin side functionality is not included as part of the "main" wordpress bootstrap, files containing uploaded file manipulation functions are one of them and you need to explicitly include them by adding

include_once( ABSPATH . 'wp-admin/includes/image.php' );

into your call_import function.

At the top of your cronjob script (for example: my-cron.php), do this:

if ( ! defined('ABSPATH') ) {
    /** Set up WordPress environment */
    require_once( dirname( __FILE__ ) . '/wp-load.php' );
}

Then setup cron like this in your server:

5 * * * * wget -q -O - http://your-domain/my-cron.php

Note: perhaps you were trying to run cron as PHP Command Line (CLI), that'll not work. You need to run cron as HTTP request (with wget or curl), as shown above.

For more information read this official WordPress document.

Update:

Based on newly added CODE, I can see this CODE is wrong:

register_activation_hook( __FILE__, 'OA_FeedManager_activated' );
function importpicture_activated() {
    if ( ! wp_next_scheduled( 'import_feed' ) )  {
        wp_schedule_event( time(), 'hourly', 'import' );
    }
 }

 add_action( 'import', 'call_import' );
 function call_import() {
     // lots of code
 }

You checked if ( ! wp_next_scheduled( 'import_feed' ) ) but you are scheduling add_action( 'import', 'call_import' );. For cron to work properly you must register the same action import. Also, your activation hook is OA_FeedManager_activated, make sure it runs importpicture_activated function. So the CODE should be like:

register_activation_hook( __FILE__, 'OA_FeedManager_activated' );

function OA_FeedManager_activated() {
    importpicture_activated();
}

function importpicture_activated() {
    if ( ! wp_next_scheduled( 'import' ) ) {
        wp_schedule_event( time(), 'hourly', 'import' );
    }
 }

 add_action( 'import', 'call_import' );
 function call_import() {
     // lots of code
 }

To check if your cron is registered properly, you may use the control plugin. Also, activate WP debugging to see what error your CODE is generating.

Note: For undefined function wp_generate_attachment_metadata() error check Mark's answer.

Also, since you've scheduled the cron in plugin's activation hook, you must deactivate and then activate the plugin again if you change activation hook function. Using Crontrol Plugin, make sure there is not unnecessary cron registered in the backend.

Finally, check if in wp-config.php you have define( 'DISABLE_WP_CRON', true );. You must remove it (if it is there) or set it to false, if you want WP cron to run on normal WP load. Otherwise, you'll need to set cron with OS crontab (like shown at the beginning of my answer.)

本文标签: pluginsUncaught Error Call to undefined function wpgenerateattachmentmetadata()wpcron