• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

My Monkey Do

A Log of Coding Solutions

  • Home
  • Web Hosts
  • Tools
  • About

php

PHP – ‘anything’ in_array 0 is true

June 27, 2019 by Webhead

$orderby = 'something';
$column_names = array( 0, 1, 2, 3, 'acolumn' );
if ( in_array( $orderby, $column_names ) ) {
    $orderby = 'got here';
}
else {
    $orderby = '';
}

// shows 'got here'
echo $orderby;

To my surprise an any array with value of 0 will cause the in_array function to be true. This is because PHP converts the string being compared to a number. ‘something’ == 0 is true because ‘something’ gets converted to 0 as a number.

The same case is true for the opposite case, which is asked and answered on stack overflow (you’ll find a more detailed explanation there).

$orderby = 0;
$column_names = array( 1, 2, 3, 'acolumn' );
if ( in_array( $orderby, $column_names ) ) {
    $orderby = 'got here';
}
else {
    $orderby = '';
}

// shows 'got here'
echo $orderby;

The solution is really to almost always set the third parameter to TRUE. This puts in_array into strict mode which compares the type of the needle.

$orderby = 'something';
$column_names = array( 0, 1, 2, 3, 'acolumn' );
if ( in_array( $orderby, $column_names, true ) ) {
    $orderby = 'will not get here';
}
else {
    $orderby = '';
}

// shows ''
echo $orderby;

Filed Under: Coding Tagged With: php

Shorthand for array

May 11, 2019 by Webhead

Recently I jumped on a project that uses the shorthand to declare an array by using the brackets []. I wondered why I have been using “array()” this whole time besides it being common in WordPress. Only to realize after deploying a plugin to a very old and out of date site that “[]” is not valid syntax in PHP 5.3.

Filed Under: Coding Tagged With: php, php 5.3

PHP 7.2 and the 502 error

March 11, 2019 by Webhead

My site was running fine and dandy for a few years.  I made the necessary adjustments to make it compatible with PHP 7.2 and upgraded the server.  A few days later I started receiving random 502 errors.

The problem apparently was a simple else if statement within a WordPress filter.  Here is the important parts of the code:

function php72_killer() {
  for($hour = 8; $hour < 17; $hour++) :
    $display_hour = $hour;
    $ampm = 'am';
    if ( $hour == 0 ) {
      $display_hour = 12;
    }
    else if ( $hour > 12 ) {
      $display_hour = $hour - 12;
      $ampm = 'pm';
    }
    else if ($hour==12) {
      $ampm = 'pm';
    }
  endfor;
}
add_meta_box( 'some_id', 'Section Title', 'php72_killer', 'post_type', 'normal' );

That last else if turned out to be the problem?  Why?  I don’t fully understand, but I found PHP 7.2 introduced optimized else ifs:  https://derickrethans.nl/php7.2-switch.html

It’s important to note that this for loop with if-statements outside of the WordPress callback doesn’t cause random 502 errors.

So what fixed my 502 errors?  not having a 2nd else if:

function php72_killer() {
  for($hour = 8; $hour < 17; $hour++) :
    $display_hour = $hour;
    $ampm = 'am';
    if ( $hour == 0 ) {
      $display_hour = 12;
    }
    else if ( $hour > 12 ) {
      $display_hour = $hour - 12;
      $ampm = 'pm';
    }
    if ($hour==12) {
      $ampm = 'pm';
    }
  endfor;
}
add_meta_box( 'some_id', 'Section Title', 'php72_killer', 'post_type', 'normal' );

Go figure.

Filed Under: Coding, Server Stuff Tagged With: 502, php, php 7.2

Where is wp_capabilities?

October 5, 2017 by Webhead

Problem

After creating a user with a certain role I need to do a custom sql query where it only selected users of a specific role.  This failed to find anything:

select u.ID, u.display_name, u.user_login
from $wpdb->users as u
inner join $wpdb->usermeta as umr
on ( umr.user_id = u.ID )
and ( umr.meta_key = 'wp_capabilities' )

$user = get_userinfo( $user_id );
var_dump( $user->roles );

get_userinfo got the right role, so saving was correct.

$caps = get_user_meta( $user_id, 'wp_capabilities' );
var_dump( $caps );

$caps was null.  weird.

 

Solution

You learn something new everyday.  After looking through the WordPress core code I found that the meta key is actually composed of the table prefix and ‘capabilities’.  So the correct query would be:

select u.ID, u.display_name, u.user_login
from $wpdb->users as u
inner join $wpdb->usermeta as umr
on ( umr.user_id = u.ID )
and ( umr.meta_key = '" . $wpdb->prefix . "capabilities' )

Filed Under: Coding Tagged With: mysql, php, wordpress

Order Non-hierarchical Custom Post Types

May 15, 2015 by Webhead

Non-hierarchical post types are like regular posts which normally do not have a menu_order attribute.  Hierarchical post type are like pages which allows a parent/child and is normally ordered by the menu_order attribute.  What if you have a custom post type that shouldn’t have a parent/child, but you still want to order it by something other than the post date?  This code does the trick on the admin side.

https://wordpress.org/support/topic/sorting-by-menu_order-in-admin-screen-on-non-hierarchical-custom-post-types

Filed Under: Coding Tagged With: php, wordpress

WordPress Heartbeat API

April 29, 2014 by Webhead

I could not find any extensive documentation on the WordPress Heartbeat API, so I am writing here to remember what I have learned through various tutorials throughout the web.

The Heartbeat API is written in Javascript.  Think of it like a javascript function that gets run every 60 seconds (depending on the interval) which sends data via POST to the server and then the server returns a response.  All of which you can easily hook into.  It was introduced in WordPress 3.6.

Quick Examples

PHP

// server received whatever parameters from when javascript triggered the "heartbeat-send" event.
function myplugin_heartbeat_received( $response, $data ) {
if ( !empty( $data['myplugin_id'] ) ) {
$response['myplugin_got_it'] = true;
}
return $response;
}
add_filter( 'heartbeat_received', 'myplugin_heartbeat_received', 10, 2 );

Javascript

jQuery(document).ready(function($) {
wp.heartbeat.interval= 15;
// set some parameters when it's time to send data to the server
$(document).on('heartbeat-send.myplugin', function(e, data){
data['myplugin_id'] = my_id;
});

// do something when your page gets a response.
$(document).on('heartbeat-tick.myplugin', function(e, data){

if ( data.hasOwnProperty( 'myplugin_got_it' ) ) {
refreshPage();
}
});

});

Heartbeat Pulse Interval

The ‘heartbeat_settings’ filter gets applied in the ‘init’ hook, priority 0 in wp_default_scripts in wp-includes/script-loader.php.  So if you do use the hook on the server-side and want to only run it on certain pages, be prepared to parse the URL because a lot of WP functions aren’t available at that time.  Alternatively you can set the interval in javascript with wp.heartbeat.interval(15) where 15 is seconds.  Note some other tutorials say to use ‘fast’, ‘standard’, ‘slow’, but that didn’t work for me for some reason.

 

Tutorials

I found these tutorials to be the most helpful.

http://code.tutsplus.com/tutorials/the-heartbeat-api-getting-started–wp-32446

http://code.tutsplus.com/tutorials/the-heartbeat-api-changing-the-pulse–wp-32462

http://code.tutsplus.com/tutorials/heartbeat-api-using-heartbeat-in-a-plugin–wp-32496

 

Filed Under: Coding Tagged With: javascript, php, wordpress

  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Interim pages omitted …
  • Go to page 8
  • Go to Next Page »

Primary Sidebar

Topics

apache apple block editor chrome cms css debug eCommerce embed firebug firefox git gmail goDaddy google hosting htaccess html html 5 IE crap image iPad iPhone javascript jquery linux localization mac os x ms sql mysql open source optimize php php 5.3 responsive rest api seo svg tinymce woocommerce wordpress wpengine xss yii youtube




Categories

  • Coding
  • Off the Shelf
  • Plugins
  • Random Thoughts
  • Server Stuff
  • Tools