salvatorericcardi
Forum Replies Created
-
Forum: Plugins
In reply to: [YITH WooCommerce Recover Abandoned Cart] Manual e-mail hasn’t arrived yetWhen I click on send email it isn’t sent also if I have an abandoned cart.
Update: I used
var_dump()
function on$wpdb
global variable and it returnsnull
. Why?I’m sorry I did a lot of confusion, the error is: Call to a member function insert() on null that the function
$wpdb->insert();
returns.Now, this is my array:
Array ( [title] => Huqvarna 610 cross blocco motore [description] => Vendo blocco motore husqvarna 610 cross tenuto di scorta sempre in magazzino usato pochissimo perfetto [locale] => Riccione [timestamp] => Oggi, 19:15 [price] => 500 € [ad_type] => 1 )
and this is my code:<?php class The_Crawler { public function __construct() { } public static function get_classifieds( $urls ) { global $wpdb; $dom_document = new DOMDocument(); foreach($urls as $url) { if( ! function_exists( 'wp_remote_request' ) ) { $html = file_get_contents( $url ); $dom_document->loadHTML( $html ); } else { $response = wp_remote_request( $url ); $body = wp_remote_retrieve_body( $response ); $dom_document->loadHTML( $body ); } $li_elements = $dom_document->getElementsByTagName( 'li' ); foreach( $li_elements as $li_element ) { $pattern = '/item\sresult/'; $class = $li_element->getAttribute( 'class' ); if( preg_match( $pattern, $class ) ) { $classified = []; $img_elements = $li_element->getElementsByTagName( 'img' ); $h3_elements = $li_element->getElementsByTagName( 'h3' ); $h4_elements = $li_element->getElementsByTagName( 'h4' ); $p_elements = $li_element->getElementsByTagName( 'p' ); foreach( $h3_elements as $h3_element) { $h3_class = $h3_element->getAttribute( 'class' ); if( $h3_class == 'title' ) { $classified['title'] = $h3_element->nodeValue; // echo "<h3>$h3_element->nodeValue</h3>"; } } foreach( $img_elements as $img_element) { $src = $img_element->getAttribute( 'src' ); if( strpos( $src, 'img' ) && ! strpos( $src, 'no-img' ) ) { $classified['src'] = $src; // echo "<img src='$src' />"; } } foreach( $p_elements as $p_element) { $p_class = $p_element->getAttribute( 'class' ); if( $p_class == 'description' ) { $classified['description'] = $p_element->nodeValue; // echo "<p>$p_element->nodeValue</p>"; } elseif( $p_class == 'locale' ) { $classified['locale'] = $p_element->nodeValue; // echo "<p><strong>$p_element->nodeValue</strong></p>"; } elseif( $p_class == 'timestamp' ) { $classified['timestamp'] = $p_element->nodeValue; // echo "<p>$p_element->nodeValue</p>"; } } foreach( $h4_elements as $h4_element) { $h4_class = $h4_element->getAttribute( 'class' ); if( $h4_class == 'price' ) { $classified['price'] = $h4_element->nodeValue; // echo "<h4>$h4_element->nodeValue</h4>"; } } $classified['ad_type'] = 1; print_r($classified); $query = $wpdb->insert( 'wp_classifieds', $classified ); echo $query; } } } } } $urls = [ 'https://www.kijiji.it/motori/moto-e-scooter/', 'https://www.kijiji.it/elettronica/' ]; The_Crawler::get_classifieds( $urls );
The query passed to insert function isn’t null how can you see in the array result above, but the error says that a member passed to the function is null: why? Didn’t I understand something?
Ok, I repeat myself until we get to the solution: above there is the code that it must call $wpdb global variable to do a query into the database. I can do it also with PHP function but if I’m using WordPress I want to use it for coding. Now, if I call the $wpdb variable like global into my custom class of the boilerplate plugin, PHP functions work but not the same of WordPress because it isn’t loaded by the plugin. So, how can the plugin load WordPress core? Just so I can use the WordPress function and in another solution.
Finally, if you give me an email I can share to you my plugin code so it’s easier. Thanks ??
Ok, I understand. How can you see I use more WP Functions, but none of this works because WP isn’t loaded. So I have to include or require wp-load.php, but how the other plugins do?
Ok I understand just so far that I use this global variable out of WordPress environment so it returns null. However now the question is: how can I use this global variable out of WordPress environment? Have I to include my function into an init hook? Or is there a ways to include $wpdb? I’m searching on the internet but probably my search is wrong.
Forum: Plugins
In reply to: [YITH WooCommerce Recover Abandoned Cart] Manual e-mail hasn’t arrived yetNo, any error message. I would like to use your plugin but so it’s very difficult.
Ok @diondesigns and @joyously can you give me an email to add you to my github repository so I can share with you my code?
I try to explain better myself: I got Devin Vinson boilerplate plugin and I customized it with my code that you can see above. Now, my code returns this error because $wpdb global variable or ABSPATH constant isn’t visible or declared. But how can I include it in my custom code? Excuse me if I can’t be able to explain me.
Updating: I tried to require wp-db.php with ABSPATH constant but this results undefined. It’s the same result of global variable $wpdb as null: why this?
I’m sorry I answer instantly. It’s a function of a custom class into the boilerplate plugin of Devin Vinson. In the activator function the global variable works instead into the custom class it doesn’t work.
This is a plugin that I’m developing and I called this global variable also in another file of this plugin and it work but in this case it returns null and it doesn’t work so why it doesn’t work?
- This reply was modified 5 years, 10 months ago by salvatorericcardi.
Forum: Developing with WordPress
In reply to: Problem with HTTP APIOk thanks. I solved with the PHP method file_get_contents().
Forum: Developing with WordPress
In reply to: Problem with HTTP APICall to undefined function wp_remote_get(). I loaded WordPress: why do I receive this error?
@vanyukov I’m using the bulk smush with “automatic smush” and “metadata” options active in the settings. Why has GTmetrix served me “optimize images” voice yet?