Support » Fixing WordPress » Need help with fixing core bug

  • Hi,

    I filed a ticket already, but until it gets a fix I need to implement workaround. One member sugested solution, another suggested improvement, and it works without error, I just don’t know what is the difference with implementing this line:

    <?php
    $classes = 'attachment-' . $size . ' size-' . $size;

    versus:

    <?php
    $classes = 'attachment-' . $size[0] . ' size-' . $size[1];

    I’m not good at PHP, so I don’t want to push theme update without being guaranteed that it will not cause any issues to users.

    Please see full blob of code: https://core.trac.wordpress.org/ticket/36996#comment:6

    Thank you!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    According to the inline docs of the filter, $size could be either a string or an array of sizes, so the best code needs to account for either possibility. I suggest this:

    if ( is_array( $size )) $size = "$size[0]x$size[1]";
    $classes = "attachment-$size size-$size";

    Thank you! So full code looks like:

    if ( is_array( $size ) ) {
       $size    = '$size[0]x$size[1]';
       $classes = 'attachment-$size size-$size';
    } else {
       $classes = 'attachment-' . $size . ' size-' . $size;
    }

    Didn’t get any errors and classes were generated.

    Thank you again!

    Moderator bcworkz

    (@bcworkz)

    Yes, that’ll work just fine (almost*), though you’ll find my snippet and your snippet in their entirety are completely interchangeable. I didn’t use {curly braces} in my code so the if() conditional only applies to the following statement, which in my code is on the same line. The line after that that assigns $classes applies in all cases. You’ll find my $classes assignment and yours will both return the exact same result (once the double quote thing is fixed as follows).

    *in my code it’s critical you use double quotes. In PHP single and double quotes are interpreted differently.

    Thank you for this explanation!

    I rewrote IF because I think curly braces are encouraged in the WP PHP syntax guidelines.

    In terms of quote style, I still have to wrap my head around, but it looks to me that in this particular case single quotes should be good, as we will not have anything unusual in the class name that should be expanded?

    Moderator bcworkz

    (@bcworkz)

    You’re right, curly braces are preferred, I was being lazy concise 🙂 It’s good to recognize how deviant code works though.

    In the case of a size array, unless you use double quotes the output will be attachment-$size size-$size instead of something like attachment-400×300 size-400×300. If you prefer single quotes and concatenation, that’s fine. Then use this for the array condition:

    $size    = '$size[0]x$size[1]';
    $classes = 'attachment-' . $size . ' size-' . $size;

    I’m sure you recognize that last line, so there’s an opportunity for somewhat more efficient code. This time conciseness would be better IMO. This covers both conditions:

    if ( is_array( $size )) {
       $size = "$size[0]x$size[1]";
    }
    $classes = 'attachment-' . $size . ' size-' . $size;

    As long as it works it’s all good, but stylistically we can go round and round 🙂

    • This reply was modified 5 months, 1 week ago by bcworkz. Reason: typo

    Thank you!

    I was trying to find out what is the X operator you’re using in $size[0]x$size[1], but in PHP docs cannot find anything similar. Is it some trick?

    Sorry about the X – I just realized that it is not operator, but part of the string… 🙂

    Moderator bcworkz

    (@bcworkz)

    🙂 No worries. That’s part of the drawback of the double quote approach. If it were the single quotes concatenated approach that would have been clear. All the double quotes do is expand variables. Any other operators, statements, etc, don’t work. If it’s not a variable it’s a string literal.

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.