1

I have a quick question about upgrading my theme file to PHP 7.1. I know it's easy to do, but I don't understand PHP.

It is currently in an old compatibility of 5.3 and this is the only error in 2 parts of my theme: "ERROR | Using a call-time pass-by-reference is deprecated since PHP 5.3 and prohibited since PHP 5.4."

I would ask my theme developer, but last time they did something they deleted all of my custom CSS code. Very awful support from them and I can't trust them anymore.

Both problem lines use this code setup_postdata( $GLOBALS['post'] =& $post_object );

THEME FILE #1:

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
if ( $upsells ) : ?>
<div class="ps-section pb-50">
    <div class="ps-container">
        <div class="ps-section__header text-center">
            <h2 class="ps-section__title"><?php esc_html_e('You may also like&hellip;', 'xuper');?></h2>
        </div>

        <div class="ps-section__content">
            <div class="ps-slider--center owl-slider products" data-owl-auto="true" data-owl-loop="true" data-owl-speed="5000" data-owl-gap="30" data-owl-nav="true" data-owl-dots="false" data-owl-item="4" data-owl-item-xs="1" data-owl-item-sm="2" data-owl-item-md="3" data-owl-item-lg="4" data-owl-duration="1000" data-owl-mousedrag="on" data-owl-nav-left="&lt;i class='ps-icon-arrow-left'&gt;&lt;/i&gt;" data-owl-nav-right="&lt;i class='ps-icon-arrow-right'&gt;&lt;/i&gt;">

                <?php foreach ( $upsells as $upsell ) : ?>

                    <?php
                        $post_object = get_post( $upsell->get_id() );

                        setup_postdata( $GLOBALS['post'] =& $post_object );

                        wc_get_template_part( 'content', 'product' ); ?>

                <?php endforeach; ?>
            </div>
        </div>
    </div>
</div>      
` `
                    <?php
                        $post_object = get_post( $related_product->get_id() );

                        `setup_postdata( $GLOBALS['post'] =& $post_object );

                        wc_get_template_part( 'content', 'product' ); ?>

                <?php endforeach; ?>
            </div>
        </div>
    </div>
</div>  

wp_reset_postdata();

Thanks for any help, I really appreciate it!

Sage Suede
  • 13
  • 2
  • 2
    Hi. If this is wordpress related, please add the wordpress tag to your question. – ryantxr Sep 13 '18 at 17:32
  • 1
    I'm going to guess that it might be the `=&` in your `setup_postdata` call. Not sure how to fix it yet, though, other than `$GLOBALS['post'] =& $post_object; setup_postdata($GLOBALS['post']);` maybe. – aynber Sep 13 '18 at 17:37
  • Hi. The error message you've posted doesn't look like a standard PHP error message. Is this the exact text which results from running this code? Or is this the output of a tool you are running to check the compatibility of the code? – IMSoP Sep 13 '18 at 17:40
  • 2
    Possible duplicate of [Workaround for call time pass by reference deprecation in PHP?](https://stackoverflow.com/questions/32738948/workaround-for-call-time-pass-by-reference-deprecation-in-php) – aynber Sep 13 '18 at 17:41
  • Better duplicates would be https://stackoverflow.com/questions/8971261/php-5-4-call-time-pass-by-reference-easy-fix-available or https://stackoverflow.com/questions/4665782/php-warning-call-time-pass-by-reference-has-been-deprecated but the code in the question doesn't actually include a pass-by-reference, which is why I wonder if this is a checking tool giving an incorrect message. – IMSoP Sep 13 '18 at 17:43

2 Answers2

-1

This & character before variable $post_object forcing assignment by reference.

You dont need & when you passing function arguments. Objects are passed by reference by default sice PHP5. Official WP documents says that you must pass a refrence to the global post variable. You could fill both requirements this way:

$GLOBALS['post'] =& $post_object;
setup_postdata($post_object);
Arek Kostrzeba
  • 551
  • 1
  • 7
  • 21
  • There is [no problem with assignment by reference in this position](https://3v4l.org/N5gWp) in any version of PHP, and just removing this might well cause problems elsewhere in the application. – IMSoP Sep 13 '18 at 17:45
  • @IMSoP ok it really wasn't good advice. What you think about it now? – Arek Kostrzeba Sep 13 '18 at 19:17
  • The page you link to includes *the exact line of code allegedly causing the error* as the *right* way to use this function, and I don't know any reason it shouldn't work in PHP 7.1. As I commented on the question, I think the whole problem is actually somewhere else, and we don't have the full story of where this error message came from. (Incidentally, it is also not true that "objects are passed by reference", although [the distinction between an object pointer and a reference is a subtle one](http://php.net/manual/en/language.oop5.references.php).) – IMSoP Sep 13 '18 at 19:47
-1

setup_postdata( $GLOBALS['post'] =& $post_object ); is doing an assignment by reference, which is a strange way to do this.

In this line of code, you're referencing WordPress' setup_postdata function, which basically populates a bunch of WP-centric variables behind the scenes. Typically you just invoke this with: setup_postdata($post_object) and you're good to go.

And don't forget to call wp_reset_postdata() when you're loop is closed, which resets the variables for WP other loops you may run elsewhere on the site.

serraosays
  • 7,163
  • 3
  • 35
  • 60
  • My wordpress said it glitched while making the changes, but they stuck and I was able to update the PHP. Thank you! – Sage Suede Sep 13 '18 at 19:28
  • It may be a strange way to do this, but it shouldn't cause any errors, and [is actually shown as an example in the Wordpress manual](https://codex.wordpress.org/Function_Reference/setup_postdata#Parameters). – IMSoP Sep 13 '18 at 19:48