WordPress – Curl on page returns 200 but browser returns 404

I had a weird issue on a PHP script. I was returning an error 404 on a WordPress page the following way :

// At the beginning of a template page
global $wp_query;

  if ( $posts_query->post_count == 0 ) {
    $wp_query->set_404();
    status_header(404);
    get_template_part( 404 ); die();
  }

When I was accessing the error page on the browser, It worked well, I had a 404 error page displayed + the correct status code when I inspected the page (Network tab).

However, when I was “curl-ing” the same error page, it didn’t work. It didn’t work in the shell, or in the PHP script.

The Shell command I did :

$ curl -I http://myerrorpage.dev 
HTTP/1.1 200 OK
Date: Tue, 13 Mar 2018 16:24:08 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.22
Expires: Thu, 19 Nov 2018 08:52:00 GMT
Cache-control: max-age=0, no-store
Set-Cookie: wordpress_fd4fa864532af79923254c1877e41f25=+; expires=Mon, 13-Mar-2017 16:24:09 GMT; Max-Age=-31536000;

The buggy PHP Script

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,            $url);
curl_setopt($ch, CURLOPT_HEADER,         true);
curl_setopt($ch, CURLOPT_NOBODY,         true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT,        1);
$headers[] = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($status);

Solution

I had to remove the “CURLOPT_NOBODY” to get the correct status code.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,            $url);
curl_setopt($ch, CURLOPT_HEADER,         true);
// curl_setopt($ch, CURLOPT_NOBODY,         true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT,        1);
$headers[] = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($status);

 





How to add a new stock option in Woocommerce

Sometimes, clients can come up with some pretty creative ways to handle their inventory … It was the case for me with a customer who was using WooCommerce.

I had to add a third option but the stock options was not customizable. I ended up deleting the native dropdown of stock options and I implemented a custom field.

In functions.php

add_action('woocommerce_product_options_stock_status', 'add_custom_stock_type');    
function add_custom_stock_type() {
        // Stock status - We remove the default one
        ?>
        <script type="text/javascript">
            jQuery('_stock_status').remove();
        </script>
        <?php   
    }

And then I followed this tutorial to create a custom field : http://www.remicorson.com/mastering-woocommerce-products-custom-fields/

Someone on SO proposed an other solution based on my approach but my contract was already done with that client by the time he answered, didn’t get the chance to test it out.

 


Folders not appearing in the tree on the left panel – PHP Storm

At some point, my folders were not appearing anymore in my Tree on the left Panel like this fellow below.

https://devnet.jetbrains.com/message/5448105?tstart=0

I ended up reading online that it occurs when you hit “Cancel” during the load of a project. I restarted PHPStorm and let it load all the components of the project and it worked.

 


Extract characters after the last dash of a String – RegExp

I wanted to extract characters after the last dash of a string. Regular expression are such a powerful tool to use in these cases. Here’s the one I needed for my problem :

(\w+)[^-]*$

Please note that with PHP, you need to use the preg_match method and surround the regexp with slashes.

preg_match ('/(\w+)[^-]*$/', 'cgo3020-red-xs', $results); // returns xs in $results[0]

Here’s a pretty convenient tool to test your regexp:  https://regex101.com/


PHP / Magento – Direct SQL Queries

Not the cleanest but here’s a quick solution to do a direct query on Magento. Might be faster than calling Magento’s objects ?

 /**
   * Get the resource model
   */
$resource = Mage::getSingleton('core/resource');
     
 /**
  * Acccess to the database in read only
  */
$readConnection = $resource->getConnection('core_read');
     
$query = 'SELECT * FROM ' . $resource->getTableName('catalog/product');
     
/**
 * Execute the query  
 */
$results = $readConnection->fetchAll($query);

Thank you DevProblems !


Sessions keep refreshing – CodeIgniter

I had a bug once, the sessions kept expiring even though the token was still valid.

I found out that CI_sessions were refreshing the session_id all the time. This was a database issue, be careful on the length of the columns in the database. The column that stored the session_id was too short and the token kept being re-generated since the old one couldn’t be recognized.

This was a tricky bug because it didn’t raise any obvious PHP error… Be careful with the schema of your database folks !