WordpressPlugin Developers - Help!

Jondoe0069

New member
Mar 21, 2007
1,317
19
0
Estados Unidos
I created a script to clean up some stuff in tables for a plugin (WPAmaniche) and I want to use wp_delete_post() to delete the related posts when the products are deleted.

I kept getting the following:

Fatal error: Call to undefined function wp_delete_post() in

So I decided to just activate my script as a plugin. I created a folder for it in the plugins directory and created a file with the proper header to activate it. Then I put the script in a separate file (to be called and run by a cron job). When I run the script, I still get the same error.

If I include the script in with the plugin header file, wp_delete_post() works and the script executes properly. The only problem with that is it runs every time I open my plugins admin screen in WordPress.

I know I have to tie the other file in with WordPress somehow to allow it to use wp_delete_post(), but how?

I've been googling all day. Help! Here is the script:

PHP:
<?php
$host="localhost";
$user="";
$password="";
$database="";
mysql_connect($host,$user,$password) or die ("Can not connect to mysql server");
mysql_select_db($database) or die("Can not select working database");

$q="select distinct post_id from wp_amazon_products where (product_image='' or product_image is null)
    or (price=0 or price is null)  or datediff(curdate(),updated)>30";
$rs=mysql_query($q);
while ($r=mysql_fetch_row($rs)) {
 echo "\n<br>Delete post: $r[0]";
 wp_delete_post($r[0]);
} 

$q="delete from wp_amazon_products where product_image='' or product_image is null";
mysql_query($q) or die(mysql_error());
echo "\n<br>image deleted: ".mysql_affected_rows();

$q="delete from wp_amazon_products where price=0 or price is null";
mysql_query($q) or die(mysql_error());
echo "\n<br>price deleted: ".mysql_affected_rows();

$q="delete from wp_amazon_products where datediff(curdate(),updated)>30 ";
mysql_query($q) or die(mysql_error());
echo "\n<br>date deleted: ".mysql_affected_rows();

$q="delete from wp_amazon_products_attributes where product_id not in (select id from wp_amazon_products) ";
mysql_query($q) or die(mysql_error());
echo "\n<br>attributes deleted: ".mysql_affected_rows();

$q="delete from wp_amazon_products_related where product_id not in (select id from wp_amazon_products) ";
mysql_query($q) or die(mysql_error());
echo "\n<br>related deleted: ".mysql_affected_rows();
?>

Thanks!
 


I see why it doesn't work, but can't figure out how to make it work -

I don't have any hooks in the actual script file so wp_delete_posts can't be called. Does anyone know of a hook I can use to hook into WP only when the actual script file is loaded?
 
Fixed! I didn't have to run the script as a plugin or hook it anywhere to use the global functions in WP - only include the following in the script:

include('../wp-config.php');
global $wpdb, $table_prefix;

It might be ugly, but it worked.
 
I see why it doesn't work, but can't figure out how to make it work -

I don't have any hooks in the actual script file so wp_delete_posts can't be called. Does anyone know of a hook I can use to hook into WP only when the actual script file is loaded?
The easiest way to do this is to add a menu, but I see you got it figured out by calling WordPress in to your script, which is a better solution anyway.
 
The easiest way to do this is to add a menu, but I see you got it figured out by calling WordPress in to your script, which is a better solution anyway.

Thanks for replying. I was stupid though. I made it way harder than it had to be. I just wanted the script to run and have it be able to use wp_delete_post. It was worth wasting an afternoon though because it will clean up some things that will allow me to use iMacro to do everything I do manually and will save me a ton of time. Automation is the best thing that's ever happened to the world.