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 badges2 Answers
Reset to default 15Some 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
orcurl
), 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
版权声明:本文标题:plugins - Uncaught Error: Call to undefined function wp_generate_attachment_metadata() @ wp-cron 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742344510a2457254.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论