• 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

Workaround to WordPress Custom HTML block error

March 21, 2022 by Webhead

Problem

Sometimes you just want to write part of an HTML code block to maybe wrap the next block in. Or some small part of HTML that may not make sense to the editor. It doesn’t matter though. The block editor will throw an error like “This block contains unexpected or invalid content”. Irritating.

Solution

One solution is to create a simple shortcode and return the html you want to write out. Shortcodes let you return whatever HTML you want and the block editor won’t know about it.

Filed Under: Coding Tagged With: block editor, wordpress

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

  • 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

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