3

I found this guide here, showing how to add a meta box in WordPress.

In there I can see this code:

/* Save the meta box’s post metadata. */
function smashing_save_post_class_meta( $post_id, $post ) {

  /* Verify the nonce before proceeding. */
  if ( !isset( $_POST['smashing_post_class_nonce'] ) || !wp_verify_nonce( $_POST['smashing_post_class_nonce'], basename( __FILE__ ) ) )
    return $post_id;

  /* Get the post type object. */
  $post_type = get_post_type_object( $post->post_type );

  /* Check if the current user has permission to edit the post. */
  if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
    return $post_id;

  /* Get the posted data and sanitize it for use as an HTML class. */
  $new_meta_value = ( isset( $_POST['smashing-post-class'] ) ? sanitize_html_class( $_POST['smashing-post-class'] ) : ’ );

  /* Get the meta key. */
  $meta_key = 'smashing_post_class';

  /* Get the meta value of the custom field key. */
  $meta_value = get_post_meta( $post_id, $meta_key, true );

  /* If a new meta value was added and there was no previous value, add it. */
  if ( $new_meta_value && ’ == $meta_value )
    add_post_meta( $post_id, $meta_key, $new_meta_value, true );

  /* If the new meta value does not match the old value, update it. */
  elseif ( $new_meta_value && $new_meta_value != $meta_value )
    update_post_meta( $post_id, $meta_key, $new_meta_value );

  /* If there is no new meta value but an old value exists, delete it. */
  elseif ( ’ == $new_meta_value && $meta_value )
    delete_post_meta( $post_id, $meta_key, $meta_value );
}

What I don't get is the line using the tick ? Is that some kind of new notation or something smart?

It's setting $new_meta_value to have the value - and then later on checks if the value is equal to it. I mean... Why not using null or false or something?

Usually I'd just frown and move on. But this code seems pretty carefully crafted.

Zeth
  • 2,273
  • 4
  • 43
  • 91
  • 6
    `’` has no special meaning in PHP. Using it like this will trigger a "use of undefined constant" warning and will use the literal value `"’"` in PHP 7 and lower. In PHP 8, this undefined constant issue will be a fatal error ([example](https://3v4l.org/G5vdX)). However, `’` is a valid identifier for a constant, so there may be another part of the code that calls `define("’", null);` and then [this would work fine](https://3v4l.org/3mYTg). But I don't see that in the tutorial you linked, nor is it something that WordPress does (currently, anyway. Maybe they did when the tutorial was written). – rickdenhaan Apr 17 '21 at 16:56
  • 1
    Those ticks are *not* backticks! Backticks are what you used to put some of the text in monospace and with a grey background. BTW: That code violates accepted coding guidelines in multiple ways concerning the formatting alone, don't expect too much from it! Also, it's ten years old and PHP changed substantially since then. – Ulrich Eckhardt Apr 17 '21 at 17:09
  • 1
    Looking at the example, I suspect this is supposed to be `''` - i.e. an empty string - but got picked up by some markdown-like parser, and then nobody noticed because the PHP code runs (until PHP 8). – IMSoP Apr 18 '21 at 10:16

0 Answers0