• 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

Coding

Sorry, This Content Could Not Be Embedded

September 9, 2020 by Webhead

I was trying to embed a youtube video in a post and it said “Sorry, this content could not be embedded”. The console showed a 404 error trying to connect to wp-json/oembed/1.0/embed.

I tried another video and it worked. According to this stack exchange post, the first video I was trying to embed probably didn’t allow for websites to embed their content.

Filed Under: Coding Tagged With: embed, wordpress, youtube

Javascript Date parsing UTC or Local?

June 29, 2020 by Webhead

When parsing a string in a javascript Date object, if you don’t set a time, the string is taken in as UTC. Then when retrieving it, it displays as local time.

new Date('2020-01-01');

will return “Tue Dec 31 2019 14:00:00 GMT-1000 (Hawaii-Aleutian Standard Time) {}” when the current time is after 10am HST.

However,

new Date('2020-01-01 00:00:00');

will return “Wed Jan 01 2020 00:00:00 GMT-1000 (Hawaii-Aleutian Standard Time) {}” anytime, even after 10am HST.

Obviously, this is due to timezones. This stack overflow gets into the nitty gritty details:

https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript

Filed Under: Coding Tagged With: javascript

Notice: wp_enqueue_style was called incorrectly

June 18, 2020 by Webhead

Problem

Notice: wp_enqueue_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information

Solution

This notice is not very helpful in tracking down the actual code causing the problem. Most solutions suggest deactivating all your plugins and switching to the default theme and then slowly reactivate each one until you find the problem. Who has time for that? Just install the Query Monitor plugin. Once the error shows go to the PHP Errors tab, expand the Location section and you’ll get a trace on the calling plugin. In my case this was the Slimbox plugin which fortunately I wasn’t using.

Filed Under: Coding Tagged With: debug, wordpress

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

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

Primary Sidebar

Topics

502 apache apple bluehost bootstrap buddypress chrome cloudways cms css debug drupal eCommerce firebug firefox git goDaddy google google analytics google maps hacked hosting htaccess html html 5 icons IE crap image iPad iPhone javascript jquery linux localization mac os x ms sql mysql open source optimize php tinymce wordpress wpengine yii youtube




Categories

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